{"id":757,"date":"2020-01-09T09:57:21","date_gmt":"2020-01-09T09:57:21","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/01\/09\/exclude-custom-module-js-from-minification-of-js-magento-2\/"},"modified":"2025-05-21T17:56:21","modified_gmt":"2025-05-21T12:26:21","slug":"exclude-custom-module-js-from-minification-of-js-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/exclude-custom-module-js-from-minification-of-js-magento-2\/","title":{"rendered":"How to Exclude Custom Module JS from Minification of JS in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">JavaScript minification in Magento 2 deletes&nbsp;odd components like spaces or symbols from JS files to decrease&nbsp;the size of the code&nbsp;and to improve the website load time. To minify JavaScript from Magento 2 backend, go to <strong>Stores &gt; Configuration &gt; Advanced &gt; Developer <\/strong>when the developer mode is on.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/01\/magento-2-javascript-minification.png\" alt=\"magento 2 javascript minification\" class=\"wp-image-7300\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>&nbsp;If the store is in Production mode, the above option will not be visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When \u201cMinify JavaScript Files\u201d is turned on, Magento 2 adds .min to the script.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/01\/Javascript-minified-files.png\" alt=\"Javascript minified files\" class=\"wp-image-7301\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The core benefit of minifying Js&nbsp;is to reduce the number of characters in the code, and therefore the size of the resulting file. The smaller the file, the faster it\u2019s&nbsp;downloaded and processed and thus, improves the page load time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, when Javascript minification is on from the backend, it minifies the custom module as well as 3rd party Js files as well. Which adds .min file for the custom module files and as such file is not available in the 3rd party API server, it throws 404 error.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/01\/3rd-party-minified-javascript-error.png\" alt=\"3rd party minified javascript error\" class=\"wp-image-7302\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To exclude custom module JS from Minification of Js in Magento 2, use the below solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Exclude Custom Module Js from Minification of JS in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>di.xml<\/strong>&nbsp;file at&nbsp;<strong><strong>Vendor\\Extension\\etc<\/strong><\/strong>&nbsp;directory<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Framework\\View\\Asset\\Minification\">\n        &lt;plugin name=\"my-exclude-Extension\" type=\"Vendor\\Extension\\Plugin\\ExcludeFilesFromMinification\" \/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create file<strong>&nbsp;ExcludeFilesFromMinification.php<\/strong>&nbsp;at&nbsp;<strong>Vendor\\Extension\\Plugin<\/strong>&nbsp;directory<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Plugin;\nuse Magento\\Framework\\View\\Asset\\Minification;\n\nclass ExcludeFilesFromMinification\n{\n    public function aroundGetExcludes(Minification $subject, callable $proceed, $contentType)\n    {\n        $result = $proceed($contentType);\n        if ($contentType != 'js') {\n            return $result;\n        }\n        $result[] = 'your file path'; \/\/ for e.g https:\/\/www.google.com\/recaptcha\/api.js'\n        return $result;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all about excluding 3rd party js files from minification. After implementing the above code, your custom module js files will be excluded from the minification and you&nbsp;will no longer get 404 error for the js files. You can also have a look at&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-website-audit-checklist\/\">Magento website audit checklist<\/a>&nbsp;to make your website perform well in all possible ways.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript minification in Magento 2 deletes&nbsp;odd components like spaces or symbols from JS files to decrease&nbsp;the size of the code&nbsp;and to improve the website load&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-757","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=757"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"predecessor-version":[{"id":14006,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/757\/revisions\/14006"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}