2

What is javascript minification and bundling of files in Magento?
Should I enable javascript minfication and bundling for my Magento 2 website ?

1
  • Minification is recommended. Don't use bundling.
    – Sohel Rana
    Jul 13, 2021 at 10:01

2 Answers 2

3

Check this link Configuration best practices

  • Magento recommends that you use third-party tools for minification and bundling (like r.js). Magento built-in mechanisms are not optimal and are shipped as fallback alternatives.
  • Activating the HTTP2 protocol can be a good alternative to using JS bundling. The protocol provides pretty much the same benefits.
  • We do not recommend using deprecated settings like merging JS and CSS files, as they were designed only for synchronously-loaded JS in the HEAD section of the page. Using this technique can cause bundling and requireJS logic to work incorrectly.
2

As per Sohel's comment, enable the minification however bundling is best left disabled in it's current form.

It puts together all files needed to all pages and so causes a huge JS file which is inefficient. RequireJS will serve just the required JS files reasonably quickly (especially if serving via HTTP2), so a lot of requests but still less to serve to clients in total.

Even the Dev docs recommend against bundling I believe for now:

"Magento bundling reduces the number of connections per page, but for each page request it loads all bundles, even when the requested page may only depend on files within one or two of the bundles. Performance improves after the browser caches the bundles. But, because the browser loads these bundles synchronously, the user’s first visit to a Magento storefront could take a while to render and hurt the user experience."

https://devdocs.magento.com/guides/v2.4/performance-best-practices/advanced-js-bundling.html

This is from the Performance best practice section of Dev Docs and discusses the advanced options for bundling using RequireJS optimiser etc. These work quite well at improving performance however take a bit more work. Easiest option I have seen is a module called MagePack however other options are available in the form of premium modules I believe.

https://github.com/magesuite/magepack

The other options for bundling is Magento Dev Tools which basically makes the Dev Docs manual bundling easier generating the necessary build.js files.

https://github.com/magento/m2-devtools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.