Magento 2 Optimization: Merging and Minifying CSS and JS (with a Known Bug in 2.4.8)
One of the first steps to optimize the performance of a Magento 2 installation is enabling merge and minify for CSS and JavaScript. This helps reduce the number of HTTP requests and the overall size of resources, improving page load times.
Developer vs Production Mode
The options to enable these features from the admin panel are only visible if Magento is in developer mode. However, on production environments it is correct and recommended to work in production mode, so it is preferable to configure everything via CLI or before the final deployment.
How to Enable Merge and Minify
If you are in developer mode, you can enable them from:
Admin Panel → Stores → Configuration → Advanced → Developer
Under JavaScript Settings and CSS Settings you will find:
- Merge CSS Files
- Minify CSS Files
- Merge JavaScript Files
- Minify JavaScript Files
Alternatively, use the terminal:
bin/magento config:set dev/css/merge_css_files 1
bin/magento config:set dev/css/minify_files 1
bin/magento config:set dev/js/merge_files 1
bin/magento config:set dev/js/minify_files 1
bin/magento cache:flushKnown Bug in Version 2.4.8 (with a Patch Available)
Starting with Magento 2.4.8 there is a known bug that makes CSS and JS minification ineffective. Even with the settings enabled, Magento keeps serving unminified files.
The good news: a patch is available to fix the problem.
🔧 You can apply one of the following:
- Mage-OS patch: [Link to the Mage-OS patch]
- Official Magento patch (Adobe upstream): [Link to the Magento Open Source patch]
Conclusion
Enabling merge and minify is a fundamental step to improve performance on Magento 2. If you are using version 2.4.8, make sure you apply the correct patch so the bug does not undo the optimization.