Async loading of javascript in Magento for PageSpeed

Async loading of javascript in Magento for PageSpeed

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Async loading of javascript in Magento for PageSpeed

Optimizing Magento for PageSpeed: Async JavaScript Loading

As an e-commerce owner running Magento 1.9.3, you’re likely facing performance issues that negatively impact your users’ experience. One common culprit is synchronous loading of JavaScript and CSS files, which can block page rendering as per Google’s PageSpeed Insights.

Understanding the Issue

When you enable async or defer attributes for your scripts:

  • Async: The browser downloads the script immediately but waits until it finishes parsing HTML before executing.
  • Defer: The browser loads the script after parsing HTML and before render.

In theory, this should improve performance by allowing parallel rendering of HTML content. However, in practice, this can lead to JavaScript exceptions if scripts referenced within your templates (.phtml files) are executed before the external async/defered scripts have finished loading.

The Current Approach

You’ve implemented async loading for all your scripts as suggested on StackExchange:

<action method="addItem">
    <type>skin_js</type>
    <name>js/jquery.fancybox.2.1.5.min.js</name>
    <params>defer</params>
</action>
<action method="addItem">
    <type>skin_js</type>
    <name>js/jquery.fancybox.2.1.5.min.js</name>
    <params>async</params>
</action>

While this optimizes loading, it introduces the risk of JavaScript exceptions due to the sequence.

The Challenges

The primary issue lies in the timing: scripts embedded within your .phtml templates are executed before external async/defered scripts finish loading. This timing mismatch can cause your site’s functionality to fail, especially when clearing browser cache and reloading the page.

A Better Approach

  1. Move critical scripts at the end of the body: Place only essential scripts right before the closing </body> tag. This ensures they load last, minimizing blocking.
  2. Lazy load non-critical scripts: Use JavaScript libraries like IntersectionObserver or native browser APIs to delay loading of non-essential scripts until needed (e.g., when a user scrolls into view).
  3. Minimize and compress assets: Use tools like Gulp or Magento’s built-in minification features to reduce file sizes, speeding up downloads.
  4. Consider a PageSpeed optimization extension: Solutions like MagePal PageSpeed Optimization or Aheadworks Page Cache can handle many optimizations for you.

Conclusion

While Magento’s default async loading is a step in the right direction, it requires careful management of script dependencies and execution sequencing. The best approach combines deliberate script placement, intelligent lazy loading, and comprehensive asset optimization.

Remember, optimizing for performance isn’t just about PageSpeed scores—it’s about providing your customers with a fast, reliable shopping experience that drives conversions. At Laravel Company, we specialize in Magento performance tuning and can help you achieve the perfect balance between speed and functionality: Learn more about our Magento optimization services.

Magento 1 Database Performance Themes

Our network