How to Defer Parsing of JavaScript in Magento
If we are talking about E-commerce business, excellent service and user experience are key components. The longer a potential customer has to wait to complete the shopping, the more time he gets to change his mind to complete the process! When it comes to the site load speed, online shoppers are unforgiving!
Studies over time have clearly shown that the conversion rate is impacted by the site speed. Nobody likes to wait and Google is not interested in ranking a site that nobody likes! Hence it is utmost important to optimize the site speed in order to improve the rankings. Google’s algorithm compares your site speed with that of your competitor’s. The search giant awards or punishes you with the rankings accordingly!
To be blunt, the site speed is not the only factor responsible for ranking. But in the competitive E-commerce business, every effort counts and can earn you a dollar! And that’s why it is important to optimize the site speed.
Focussing on the Magento store, the defer parsing of Javascript helps improve the site speed. Usually, the Javascript is placed between the header tags and the code is implemented from top to bottom when a website loads. This results in loading of all the JS taking time which is not necessary at the point of time.
Before the defer parsing of JavaScript in Magento:
The defer parsing of Javascript in Magento enables loading of HTML and CSS scripts before the JS. This is enabled by replacing the Javascript from the header to footer tags. It results in the site speed appearing to be faster for the user! Hence happy user 🙂
Learn the step-wise method to defer parsing of Javascript in Magento given below!
Method to Defer Parsing of Javascript in Magento:
- Add observer code in config.xml Â
12345678910111213<frontend>Â Â Â Â <events>Â Â Â Â Â Â <http_response_send_before>Â Â Â Â Â Â Â Â <observers>Â Â Â Â Â Â Â Â Â Â <extension_deferjs_http_response_send_before>Â Â Â Â Â Â Â Â Â Â Â Â <class>extension/observer</class>Â Â Â Â Â Â Â Â Â Â Â Â <type>singleton</type>Â Â Â Â Â Â Â Â Â Â Â Â <method>httpResponseSendBefore</method>Â Â Â Â Â Â Â Â Â Â </extension_deferjs_http_response_send_before>Â Â Â Â Â Â Â Â </observers>Â Â Â Â Â Â </http_response_send_before>Â Â Â Â </events>Â Â </frontend> - Add below code in the observer.php file
123456789101112public function httpResponseSendBefore($observer)Â Â {Â Â Â Â $response = $observer->getEvent()->getResponse();Â Â Â Â $html = $response->getBody();Â Â Â Â preg_match_all('#(<script.*?</script>)#is', $html, $matches);Â Â Â Â $js = '';Â Â Â Â foreach ($matches[0] as $value)Â Â Â Â Â Â $js .= $value;Â Â Â Â $html = preg_replace('#<script.*?</script>#is', '', $html);Â Â Â Â $html = preg_replace('#</body>#',$js.'</body>',$html);Â Â Â Â $response->setBody($html);Â Â }
Implement the above two steps and offer an optimized Magento store to the visitors!
After the defer parsing of JavaScript in Magento:
Please share any doubts in the Comments section and I’d be solving it asap!
If you want to skip the tutorial and still want to have the best user experience at your store, try the free Magento Defer Parsing of Javascript extension!
Reward the post with 5 stars if it helped you optimize the Magento store!
Thank you for allowing me to help boost your website’s performance for a quicker and better experience! ?
Improve the loading speed of your online store easily by using Magento Defer Parsing of JavaScript.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Hello Meetanshi,
I want to know this module is add the attribute defer into the like this.
Thanks,
Vishal Padhariya
Hello Vishal,
Please add the code of all the JS after tag.
Thanks.
Hi Meetanshi, I have copied all files to respective folder except package.xml then activated the extension from admin but it shows no change in defer parsing, gtmetrix tool show no improvement,
Is i need to do anything else
Hi Avinash,
Maybe due to some confliction, it’s not working. You will need to debug further to get it to work.
I have an existing package.xml from another module in the root – when I change your file name it does not seem to be working.
You can skip our extension’s package.xml
That file has no effect on the working of our extension.
Hi Meetanshi,
Thanks for such a wonderful blog. I need to know where to put this code ? I mean , Do I need to create a custom extension ?
Please help ?
Hi Praveen,
You need to create a custom extension for it. But rather, you can download and use our FREE extension https://meetanshi.com/magento-defer-parsing-javascript.html