magento2: jQuery Widget or UiComponent mixin don't loading or loading randomized

Maybe need additional fixes on #25587 request.

Preconditions (*)

  1. Magento 2.4.x
  2. Google Chrome 92 (cache disabled on devtools)

Steps to reproduce (*)

  1. Fresh install m2 2.4.4 (also problem with all 2.4.x)
  2. Dev mode on
  3. Cache settings (all on, except for layout, block_html, full_page)
  4. Create new theme (luma parent)

theme structure

requirejs-config.js

var config = {
    map: {
        '*': {
            myWidget: 'Magento_Theme/js/my-widget'
        }
   },
   config: {
        mixins: {
            'Magento_Theme/js/my-widget': {
                'Magento_Theme/js/my-widget-mixin': true
            }
        }
    }
};

my-widget.js

define([
    'jquery',
    'jquery-ui-modules/widget'
], function ($) {
    'use strict';

    $.widget('mage.myWidget', {
        options: {
            color: 'red'
        },
        _create: function () {
            $('body').css('background-color', this.options.color);
        }
    });

    return $.mage.myWidget;
});

my-widget-mixin.js

define([
    'jquery',
    'jquery-ui-modules/widget'
], function ($) {
    'use strict';

    return function (widget) {
        $.widget('mage.myWidget', widget, {
            options: {
                color: 'green'
            }
        });

        return $.mage.myWidget;
    }
});

Expected result (*)

Mixin load and background is GREEN 🟢

Actual result (*)

Results depend on cache, opened devtools.

  1. Init widget on js file – mixin don’t load (or randomized loading, also loading when devtools opened) Background is RED 🔴 or GREEN 🟢
define([
    'jquery',
    'myWidget'
], function ($, myWidget) {
    'use strict';

    myWidget();
});
  1. Init widget on phtml file – mixin don’t load (or randomized loading, also loading when devtools opened) Background is RED 🔴 or GREEN 🟢
require([
    'jquery',
    'myWidget'
], function ($, myWidget) {
    'use strict';

    myWidget();
});
  1. Init widget on js file (with mage) – mixin load (or randomized don’t loading) Background is GREEN 🟢 or RED 🔴
require([
    'jquery',
    'mage/mage'
], function ($) {
    'use strict';

    $('body').mage('myWidget', {});
});
  1. Init widget on js file (without alias) – mixin load (or randomized don’t loading when devtools opened) Background is GREEN 🟢 or RED 🔴
define([
    'jquery',
    'Magento_Theme/js/my-widget'
], function ($, myWidget) {
    'use strict';

    myWidget();
});
  1. ALSO, when widget don’t load and when call widget again (click event, etc) – mixin load Background is RED 🔴, after event is GREEN 🟢
require([
    'jquery',
    'myWidget'
], function ($, myWidget) {
    'use strict';

    myWidget();

    $('.action.mixin').on('click', function () {
        myWidget();
    });
});
  1. Init widget on phtml file with data-mage-init – mixin load (works perfectly, without background flashing and mixin preloading) Background is GREEN 🟢
<div data-mage-init='{"myWidget": {}}'></div>
  1. Init widget on phtml file with js x-magento-init – mixin load (works perfectly, without background flashing and mixin preloading) Background is GREEN 🟢
<script type="text/x-magento-init">
    {
        "*": {
            "myWidget": {}
        }
    }
</script>
  1. Init widget on js file (with mixins!) – mixin load (works perfectly, without background flashing and mixin preloading) Background is GREEN 🟢
define([
    'jquery',
    'mixins!myWidget'
], function ($, myWidget) {
    'use strict';

    myWidget();
});

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 9
  • Comments: 24 (16 by maintainers)

Most upvoted comments

@denis-g Your workaround 8. saved my day, but probably this should have a completely fix for all.

+1. We also randomly having similar issue. I think it’s not related to random file loading, as we’re using file bundling and both widget and mixin are inside single bundle. Almost all the time at first page view mixin doesn’t apply, but after page reload - it applies

Hi @ihor-sviziev, Hmm, I’ve tested clean theme and widget from 1st post on 2.4.1 (original resolver.js with patch and original resolver.js from 2.4.3 with patch, and file from patch), also tested on 2.4.3 (original resolver.js with patch, and file from patch) - and mixins did not apply.

On @Kemexyz mixin used a component. Is it probably a different func call there? 🤔 Or patch works with component only… Or something else affects.

Additional testing is needed. Maybe someone has a working result with a patch?

Hi @denis-g,

Thank you for reporting the issue with detailed description. Verified the issue on 2.4-develop and issue is reproducible.

Here is what I did to reproduce:

  1. As per reporter’s issue description creates new theme with parent luma. Created all files with provided code in newly created theme.
  2. Disabled Cache in Dev Tools.
  3. Tried with multiple scenarios as provided by the reporter in Actual Result section.

Issue: In first 5 scenarios, result appeared first to be loaded with background RED that means mixin didn’t load properly but after reloading the page background became GREEN. In last 3 scenarios, result appeared correct with GREEN background means mixin was loading properly in the first attempt itself.

So based on the above results, confirming the issue.

FYI we just tried to debug this issue - that’s definitely a very random issue, and it looks like it is related to async js execution/script loading. When the issue reproducing, the following core return false because in the RequireJS config, we don’t have all maps and mixins yet. https://github.com/magento/magento2/blob/97f0a1f34efc7c40c97014800cc876e9aedcdc0f/lib/web/mage/requirejs/mixins.js#L161

It looks like the issue coming from using wrapping

(function() {
 ... requirejs-config.js content here
require.config(config);
})();

Or maybe it is related to require.config that probably works in an async way.

https://github.com/magento/magento2/blob/7c6b6365a3c099509d6f6e6c306cb1821910aab0/lib/internal/Magento/Framework/RequireJs/Config.php#L79-L85

So far, fixing this issue won’t be really easy, so I stopped the research.

BTW, if you’ll try setting a breakpoint to that line, or even adding console.log - the issue won’t reproduce OR will reproduce very-very rarely that makes debugging really hard.

Summary: the issue happening because of the missing the mixins! prefix. This prefix isn’t getting added because of not fully loaded RequireJS config.

I even thought it might be related to https://github.com/magento/magento2/blob/7c6b6365a3c099509d6f6e6c306cb1821910aab0/lib/web/mage/bootstrap.js#L21

And, issue reproducing both with enabled and disabled

PS: @krzksz, I think now is your turn. We need your knowledge and skills to resolve such hard frontend issues.

faced the same issue with unapplied mixins on storefront. the issue is reproducible when requirejs-config.js executing after mage/requirejs/mixins.js.

As mentioned previously, https://github.com/magento/magento2/blob/4c36116dcf878e127059d9be9566a119783583f2/lib/web/mage/requirejs/mixins.js#L139 can’t find mixins due to empty requirejs config.

Seems like the issue introduced with this commit https://github.com/magento/magento2/commit/540b1340275b001aca44ce3d889a039614167a8e

After this changes, mixins use custom context of require, configured on first load of mixins.js using config from default requirejs context. As result - if mixins.js loaded before requirejs-config empty config will be used to build custom context. This custom “empty” context will be used as closure during all next mixins applying

https://github.com/magento/magento2/blob/4c36116dcf878e127059d9be9566a119783583f2/lib/web/mage/requirejs/mixins.js#L10-L28

One of possible solutions - change sequence of assets in head (swap requirejs-config and mixins assets).

https://github.com/magento/magento2/blob/4c36116dcf878e127059d9be9566a119783583f2/app/code/Magento/RequireJs/Block/Html/Head/Config.php#L126-L137

Moreover, looks like this files should be swapped initially, because inserting using correct sequence, but mixed due to the same $after value.

        $requireJsConfig = $this->fileManager->createRequireJsConfigAsset();
        $assetCollection->insert(
            $requireJsConfig->getFilePath(),
            $requireJsConfig,
            $after
        );
        $requireJsMixinsConfig = $this->fileManager->createRequireJsMixinsAsset();
        $assetCollection->insert(
            $requireJsMixinsConfig->getFilePath(),
            $requireJsMixinsConfig,
            $requireJsConfig->getFilePath() // <------  insert after $requireJsConfig asset
        );

Can you please test following changes? as fixes may related for issue 2.4-develop…mrtuvn:requirejs-resolver-mixins-patch

Using your changes I made a module work. I noticed mixins form a marketplace module were never called and did not even show up in frontend. Came here after a google search.

Module (free): https://marketplace.magento.com/sparsh-magento-2-order-comments-extension.html I’m currently on Magento ver. 2.4.2-p1

This module has two mixins:

var config = {
config: {
    mixins: {
        'Magento_Checkout/js/action/place-order': {
            'Sparsh_OrderComments/js/order/place-order-mixin': true
        },
        'Magento_Checkout/js/action/set-payment-information': {
            'Sparsh_OrderComments/js/order/set-payment-information-mixin': true
        }
    }
}
};

With your changes to lib/web/mage/requirejs/resolver.js the mixins execute and the comment is saved to the order like expected, without your changes the comment is never saved to the order because the mixins will never execute.

Between each test I cleared the static files.

I have not yet tested if this change will affect anything else.

The reproduction path looks quite clear and I would start by attaching a logger/debugger in https://github.com/magento/magento2/blob/2.4-develop/lib/web/mage/requirejs/mixins.js#L161 to see if mixins are not loaded because they are not seen by the plugin or the whole mechanism is completely ignored.

good call! Thanks you denis for some use-cases cc: tag @krzksz here maybe he will give us more information

@ihor-sviziev yep. On bundle, merged, or single it doesn’t matter. Core problem on matching mixin with widget/component and random apply.

Also similar issue #22338