Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Minification issue & requirejs loading in production mode in 2.4.7 #38829

Open
2 of 5 tasks
KrzysztofWisniewskiAcc opened this issue Jun 14, 2024 · 35 comments
Open
2 of 5 tasks
Assignees
Labels
Issue: needs update Additional information is require, waiting for response Reported on 2.4.7 Indicates original Magento version for the Issue report.

Comments

@KrzysztofWisniewskiAcc
Copy link

KrzysztofWisniewskiAcc commented Jun 14, 2024

Preconditions and environment

  1. Magento community/commerce version: 2.4.7
  2. Firefox browser (I was able to reproduce this on other browsers, but in firefox, the issue occurs almost all the time)

Steps to reproduce

  1. Install fresh Magento version of 2.4.7
  2. Enable js minification (there are few ways to do it, I edit the config.php/env.php file)
    ...
'system' => [
        'default' => [
            'dev' => [
                'template' => [
                    'minify_html' => '1'
                ],
                'js' => [
                    'merge_files' => '0',
                    'minify_files' => '1',
                    'enable_js_bundling' => '0'
                ],
                'css' => [
                    'minify_files' => '1'
                ],
                'grid' => [
                    'async_indexing' => '1'
                ]
            ],
        ]
    ]
  1. Run:
bin/magento setup:upgrade
bin/magento deploy:mode:set production
  1. Now go to front and open the console, errors should be there
  2. If errors are not there refresh the page few times,

Best is to use Firefox, as the errors there appear almost always.
The errors appear rarely in other browsers.

image

The first error:
The resource from “http://magento.clean/static/version1718361386/frontend/Magento/luma/en_US/mage/common.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).

These errors do not occur on magento 2.4.5

I did not test this on magento 2.4.6 so it may be that the issue also occurs there.

Expected result

There are no errors in console.
The files are not loaded twice.
Only minified files are loaded.

Actual result

There are errors in console.
Files are loaded twice
.min files & their corresponding 'not minified' are being requested (which fails to load as not minified files, don't exists in pub directory after setup:static-content:deploy) .

image

image

Additional information

I noticed the issue when upgrading from 2.4.4-p8 to 2.4.7 so not quite sure if this started to happen with 2.4.7 release or 2.4.6

Release note

No response

Triage and priority

  • 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”.
Copy link

m2-assistant bot commented Jun 14, 2024

Hi @KrzysztofWisniewskiAcc. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

@engcom-Bravo engcom-Bravo added the Reported on 2.4.7 Indicates original Magento version for the Issue report. label Jun 17, 2024
@engcom-Delta engcom-Delta self-assigned this Jun 17, 2024
Copy link

m2-assistant bot commented Jun 17, 2024

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • 5. Add label Issue: Confirmed once verification is complete.
  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Delta
Copy link
Contributor

Hi @KrzysztofWisniewskiAcc ,

Verified the issue on 2.4.7 community and commerce edition and we are not able to reproduce this issue.

Steps to reproduce:-

1.Enable js minification
2.Run bin/magento setup:upgrade
bin/magento deploy:mode:set production
3.Go to front and open the console, check errors
4.Refresh the page few times

For magento community

Screenshot 2024-06-18 at 5 28 45 PM Screenshot 2024-06-18 at 5 29 00 PM Screenshot 2024-06-18 at 5 29 23 PM Screenshot 2024-06-18 at 5 29 54 PM Screenshot 2024-06-18 at 5 30 04 PM

For magento commerce

Screenshot 2024-06-18 at 6 05 45 PM Screenshot 2024-06-18 at 6 05 56 PM

Please refer the attached screenshots and let us know if we missed anything.

@engcom-Delta engcom-Delta added the Issue: needs update Additional information is require, waiting for response label Jun 18, 2024
@KrzysztofWisniewskiAcc
Copy link
Author

KrzysztofWisniewskiAcc commented Jun 21, 2024

Hi @KrzysztofWisniewskiAcc ,

Verified the issue on 2.4.7 community and commerce edition and we are not able to reproduce this issue.

Steps to reproduce:-

1.Enable js minification 2.Run bin/magento setup:upgrade bin/magento deploy:mode:set production 3.Go to front and open the console, check errors 4.Refresh the page few times

For magento community

...

I was able to reproduce the issue in the way you described.
When I checked 'disable cache' in firefox, after few refreshes I was able to reproduce it.
It was more rare to encounter the issue if 'disable cache' was not checked.

image

2
3

@engcom-Delta
Copy link
Contributor

Hi @KrzysztofWisniewskiAcc ,

We Verified the issue on 2.4.7 and 2.4-develop again and we are not able to reproduce this.

Steps to reproduce:-

1.Enable js minification
2.Run bin/magento setup:upgrade
bin/magento deploy:mode:set production
3.Go to front and open the console, check errors
4.Disable cache
5.Refresh the page few times

For 2.4.7

Screenshot 2024-06-24 at 7 59 28 PM Screenshot 2024-06-24 at 7 59 28 PM

For 2.4-develop

Screenshot 2024-06-24 at 7 59 34 PM Screenshot 2024-06-24 at 7 59 34 PM

@engcom-Bravo
Copy link
Contributor

Hi @KrzysztofWisniewskiAcc,

We have noticed that this issue has not been updated since long time.
Hence we assume that this issue is fixed now, so we are closing it. Please feel to raise a fresh ticket or reopen this ticket if you need more assistance on this.

Thanks.

@KarolLitman
Copy link

We are experiencing the same issues after upgrading from version 2.4.6 to 2.4.7-p1. @KrzysztofWisniewskiAcc, have you managed to resolve this problem?

@vandijkstef
Copy link

I have the same issue; and also have the same problem creating a 100% reproducible case. Not sure how exactly; But I think this is happening when caches are expired and/or recompiling on that specific request - possibly enforced by the Cache-Control: no-cache & Pragma: no-cache request headers, with a flair of race-conditions?

Sorry I have nothing decent to add to re-open this ticket.

@tstamplis
Copy link

Did anyone find a solution? I have the same problem with 2.4.7-p1 but on mobile view only. In desktop, only minified js files are loaded by the browser. But in mobile view, it tries to load both minified and unminified js files in production mode with minify js turned on.

@KrzysztofWisniewskiAcc
Copy link
Author

KarolLitman

We are experiencing the same issues after upgrading from version 2.4.6 to 2.4.7-p1. @KrzysztofWisniewskiAcc, have you managed to resolve this problem?

I was not able to solve the issue,

Not sure on what layer is it lying, is there some kind of external dependency that is pulled and tries to load things different way or was the loading mechanism messed?

@tstamplis
Copy link

I was able to resolve it, though I don't know how it resolved it. Normally, I put the site in production mode and let Magento compile and deploy static content. This is how I've always done it. To fix this though, I have to go to production mode, then clear generated, view-preprocessed, deployed version, static content and cache then while in production mode I compile code, force static content deployment and this resolved the issue. Again, I don't know why setting production mode is different than a manual di compile and content deploy, but it makes the difference on my site.

@dandrikop
Copy link

dandrikop commented Sep 7, 2024

My live environment is on Magento 2.4.6-p7 and it does not have the problem mentioned here. However, I have recently upgraded my test environment to Magento 2.4.7-p2 and it has this problem.

The solution of @tstamplis did not work for me. To be honest I don't see why compiling and creating the static files two consecutive times would solve this problem.

@praveenpaliya
Copy link

We are also facing this issue. We recently upgraded our Magento cloud instance from 2.4.6-p5 to 2.4.7-p2 and we can see the minify JS are not loading and it is braking the material icons loading and stopping our checkout process as well.
What I noticed that, JS files are failed in loading which we have added in app/design/frontend/themename/web/js using requirejs and facing the problem all the JS which are loading through this.

@superdav42
Copy link

We're encountering the same issue as described on our site running 2.4.7-p2. JS is minified but it trieds to load the unminified versions. It doesn't happen every time and seems to happen more often when cache is disabled. It's probably some race condition. Putting breakpoints in requirejs-min-resolver.min.js would help debug the problem but it doesn't happen when the breakpoints are set. This file is responsible for rewritting the url to the minified version so somehow it isn't being executed before the other code. Our site is goodsalt.com. Our build process always manually compiles the static assets before they are deployed. Is there any solution or debug steps we can perform?

@drabikowy
Copy link

We face exactly the same issue on 2.4.7-p3
image

@phes71
Copy link

phes71 commented Nov 13, 2024

we have the same issue on 2.4.7-p2. I have disabled minification and merge for .js files and the errors have gone

we need a fix so we can minify js again

@domeglic
Copy link

Same here, 2.4.7-p3. It happens randomly but if I refresh any page a few times then one of them will fail.

@phes71
Copy link

phes71 commented Nov 28, 2024

Same here, 2.4.7-p3. It happens randomly but if I refresh any page a few times then one of them will fail.

I found changing the .htaccess file in /pub/static by uncommenting
#RewriteBase /pub/static/ to
RewriteBase /pub/static/
This initially made the situation worse, but after I redeployed, it worked as expected

@domeglic
Copy link

This seems like a race condition bug. Looking into it, the url is generated in lib/web/requirejs/require.js, method nameToUrl.

When minification is enabled, \Magento\Framework\RequireJs\Config::getMinResolverCode class adds a file requirejs-min-resolver.min.js which overrides that method and appends min.js. It's a generated file because it can exclude certain scripts and that is configurable.

Putting breakpoint on it as well as on the require.js (each module is added using head.appendChild(node) ), it didn't always go through the minify resolver.

I guess temporary fixes could be patching the require.js file to contain the minify logic, but not sure how to know if minify is enabled or not. Or adding an apache/nginx rule to append the min.js on 404s.

I am using magepack for bundling but even without it the problem appears although less often.

@phes71
Copy link

phes71 commented Nov 28, 2024

domeglic

I found the errors always showed in the console on the Firefox browser, and Chrome was a bit hit-and-miss.

the errors were also more obvious in the admin, but as I said, the errors went away after changing the .htaccess file as I mentioned earlier

@magecommerce
Copy link

have the same issue

@KarolLitman
Copy link

The old mixins.js file from this commit helped me:
bf705b0

Try it yourselves to see if it helps you.

@VitaliyBoyko
Copy link
Contributor

This issue is not fixed. Reopening.

@VitaliyBoyko VitaliyBoyko reopened this Jan 7, 2025
@m2-community-project m2-community-project bot added Issue: ready for confirmation and removed Issue: needs update Additional information is require, waiting for response labels Jan 7, 2025
@github-project-automation github-project-automation bot moved this to Ready for Confirmation in Issue Confirmation and Triage Board Jan 7, 2025
@engcom-Delta
Copy link
Contributor

Hi @KrzysztofWisniewskiAcc @VitaliyBoyko ,

We Verified the issue on 2.4.7-p3 and we are not able to reproduce this.

Steps to reproduce:-

1.Enable js minification
2.Run bin/magento setup:upgrade
bin/magento deploy:mode:set production
3.Go to front and open the console, check errors
4.Disable cache
5.Refresh the page few times

Can you please provide additional testing steps.

@engcom-Delta engcom-Delta added Issue: needs update Additional information is require, waiting for response and removed Issue: ready for confirmation labels Jan 8, 2025
@engcom-Bravo engcom-Bravo moved this from Ready for Confirmation to Needs Update in Issue Confirmation and Triage Board Jan 8, 2025
@VitaliyBoyko
Copy link
Contributor

VitaliyBoyko commented Jan 10, 2025

@engcom-Delta thank you for looking into this!

This issue indeed appears to be tricky to reproduce consistently—a bit of a Heisenbug scenario.

The only pattern we've observed is that the system requires particularly fast memory and a high-performance environment to manifest the issue. For instance:

On our Magento staging instance, we’ve been unable to reproduce it under normal conditions.
However, locally, with a fast SSD, an i9 processor, DDR 5, and running automated tests (e.g., Cypress), the issue surfaces approximately 1 out of 10 tries.

Given the reports from multiple users encountering the issue, it’s clear that it does exist. I recommend escalating it to your most experienced JS engineers for further analysis.
Please don’t close the ticket 🙌

@Echron
Copy link
Member

Echron commented Feb 10, 2025

I've noticed the same issue and can reproduce it but as said above, not consistently. When the page produces the error the script tags are put 2x in the DOM:

Image

One time with data-requirecontext="_" (minified) and a second time with data-requirecontext="$" (not minified/wrong). On a normal page load only the first data-requirecontext, with the underscore is added to the page. This not a solution, but just in case anyone wants to debug this further.

@magenizr
Copy link

magenizr commented Feb 21, 2025

We're experiencing the same issue as mentioned in this comment #38829 (comment) after upgrading from 2.4.6-p5 to 2.4.6-p9, but only on /checkout.

Uncaught Error: Script error for "mage/bootstrap"
Uncaught Error: Script error for "mage/dataPost"
etc.
  • Mode: developer
  • Enable JavaScript Bundling: No
  • Merge JavaScript Files: No
  • lib folder updated
  • No JS/CSS minification
  • csp_whitelist.xml from custom modules disabled ( removed )
  • .htaccess reverted back to default

Please, let's keep this discussion going. I've already spent a lot of time on this and happy to buy someone a few coffees for the ultimate advice, no matter how simple it may be.

Note:
This environment has undergone multiple upgrades over the past three years, and we've never encountered issues this challenging. We're very experienced in performing Magento upgrades; however, this one is a chestnut.

@domeglic
Copy link

@magenizr I didn't fix the issue but just made a workaround to rewrite any not found JS file to min.js:

location ~ ^(.+)\.(js)$ {
    try_files $1.$2 $1.min.$2 $uri =404;
}

I don't remember details anymore but I think it is a race condition bug.

@engcom-Delta
Copy link
Contributor

Hi @Echron @magenizr ,

Thanks for your reporting and collaboration.
Could you please share the system configuration and hardware details where you were able to reproduce the issue, similar to what @VitaliyBoyko shared? This will help us investigate further.

Looking forward to your response.

Thanks.

@magenizr
Copy link

magenizr commented Mar 7, 2025

Hi folks,

I finally had some time to dig into it and fixed errors like Uncaught Error: Script error for "mage/bootstrap" as follows.

  1. When you add debug output to load:, you'll see that the URL appears as a relative path (./Magento_Translation/js/mage-translation-dictionary.min.js) rather than the expected absolute URL (https://{base_url}/static/version1741308158/frontend/{Theme}/{Folder}/en_AU/Magento_Translation/js/mage-translation-dictionary.min.js).
            //Delegates to req.load. Broken out as a separate function to
            //allow overriding in the optimizer.
            load: function (id, url) {

                console.log(id);
                console.log(url);

                req.load(context, id, url);
            },
  1. I added additional debug output to inspect the baseUrl configuration. The output clearly indicates that the checkout is using ./ for some reason, even though var require is correctly defined in the DOM.
            /**
             * Set a configuration for the context.
             * @param {Object} cfg config object to integrate.
             */
            configure: function (cfg) {
                //Make sure the baseUrl ends in a slash.
                if (cfg.baseUrl) {
                    if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== '/') {
                        cfg.baseUrl += '/';
                    }
                }

                console.log('cfg');
                console.log(cfg);

Long story short: In my case it was a custom require_js.phtml that needed to be parsed through $secureRenderer->renderTag just like in https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Theme/view/frontend/templates/page/js/require_js.phtml

@domeglic @Echron @drabikowy If you follow the same debugging steps, you might get an idea what is missing.

@Echron
Copy link
Member

Echron commented Mar 10, 2025

@magenizr, I followed these steps, but I can't reproduce the output you are getting. The URL remains the absolute URL.

I will follow your thought process and continue with debugging the require.js file. I've added a bit logging around the "enable" method for a module. As when enabled the dependencies get loaded as well.
(file: vendor/magento/magento2-base/lib/web/requirejs/require.js - around line 1120)

enable: function () {
    if(this.map.url && this.map.url.endsWith('.min.js'))
        {
            console.log(this);
        }

This is the expected data for a module to load:

Image
Where I expect the map.url to be the URL to the module being loaded and the depMaps the URLs for the dependencies.

This is the output of the first module where the map.url does not end with .min.js:

Image

As you see the dependencies are not using the minified version either. In require.js in the method "makeModuleMap", I find that filenames starting with "_@r" are generated: "If no name, then it means it is a require call, generate an internal name.".

This is how far I got for now, I'll debug this further and share my findings.

@m08110071
Copy link

@Echron @engcom-Delta

You can try to edit any category and open Content group, you may see error from console log

Image

@Marwaa92
Copy link

Marwaa92 commented Mar 17, 2025

hello ,

After upgrading Magento from 2.4.6-p9 to 2.4.7-p4, we encountered the mentionned issue with JavaScript files that are loaded with both .js and .min.js extensions.

Is there a solution or fix available for this problem please?

Image

Image

@phes71
Copy link

phes71 commented Mar 17, 2025

for us, we found it was a cache issue

flush js css cache, flush magento cache, flush Cloudflare cache

Check the console for errors again

@m08110071
Copy link

m08110071 commented Mar 18, 2025

Hello all, you can try a workaround solution:

diff --git a/lib/web/mage/requirejs/mixins.js b/lib/web/mage/requirejs/mixins.js
--- a/lib/web/mage/requirejs/mixins.js
+++ b/lib/web/mage/requirejs/mixins.js	(date 1742184984114)
@@ -21,6 +21,8 @@
         },
         rjsMixins;

+    unbundledContext.nameToUrl = defContext.nameToUrl;
+
     /**
      * Prepare a separate context where modules are not assigned to bundles
      * so we are able to get their true path and corresponding mixins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: needs update Additional information is require, waiting for response Reported on 2.4.7 Indicates original Magento version for the Issue report.
Projects
Development

No branches or pull requests