Magento2 – Not Minifying JS, CSS or HTML in Production Mode

magento2magento2.2.2

I am running a magento 2.2.5 CE in production mode with two storefronts setup.

The settings in admin are as follows:

  • Minify Html: Yes

  • Merge JavaScript Files: Yes

  • Enable JavaScript Bundling: Yes
  • Minify JavaScript Files: Yes

  • Merge CSS Files: Yes

  • Minify CSS Files: Yes

Nothing is being minified, not the CSS, JS or HTML.
The JS and CSS are atleast bundled into a single merged file, but with no minification.

All caches have been cleared and static files deployed probably a 1000 times since we've set the minification to true.

I've tried setting everything VIA CLI also and we are running in production so everything should be minified, but is not.

Please let me know if there's anything else I can try to get the default magento 2 minification to work.

Best Answer

After a lot of struggling with this, i've finally found the issue.

I dived into the magento 2 source code to try and decipher how it decides when to minify files.

I discovered that even though our magento mode was set to production in the ENV file, the mode inside the vendor/magento/framework/App/State.php file still returned developer for some reason.

After going through the stack trace i finally found that we have a MAGE_MODE declaration set to developer inside our .htaccess file. As soon as I changed this to production, the minification started working (remember to restart apache to read the modified .htaccess file).

In conclusion, if anyone else faces this issue, make sure you are not setting the MAGE_MODE variable to developer inside your .htaccess file. Even if you override this value in all other sections of magento 2, some parts of magento 2 will still initialize under developer mode.

NOTE for NGINX users

You'll need to add it to your config as NGINX doesn't read .htaccess files. fastcgi_param MAGE_MODE production; Or, better yet, omit it entirely and set it via bin/magento deploy:mode:set production.

Thanks @kirkmadera for the NGINX comment.

Hope this saves someone out there some time.