How to force addJs in Magento to run async

How to force addJs in Magento to run async

July 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to force addJs in Magento to run async

Forcing Magento’s addJs Method to Run JavaScript Files Asynchronously

As an experienced developer working with e-commerce platforms like Magento, you’ve probably found yourself wanting more control over how your JavaScript files are loaded. The built-in addJs method is convenient for including necessary scripts in your views, but it lacks the ability to specify that a script should load asynchronously by default.

The Default addJs Behavior

Magento’s core addJs method, used within layout XML files like page.xml, looks something like this:

<action method="addJs">
    <script>prototype/prototype.js</script>
</action>

This generates HTML output that resembles the following:

<script type="text/javascript" src="//domain.com/js/prototype/prototype.js"></script>

Notice that there’s no async attribute included in the script tag. This means the script will load synchronously, potentially blocking other resources from rendering and slowing down your page’s perceived performance.

The Current Limitations

Unfortunately, as of Magento 2.x, there is no built-in parameter or option to specify that a script should be loaded asynchronously when using addJs. The method generates the script tag without offering this functionality.

This limitation is in place because Magento’s core uses addJs for critical files like Prototype and jQuery, which need to load synchronously to ensure they’re available before other parts of your page’s JavaScript runs.

Achieving Asynchronous Loading

To force your scripts to load asynchronously, you’ll have to manually modify the generated HTML. There are a few ways to approach this:

1. Override the addJs Method

You could override Magento’s core addJs method in a custom extension or theme. This involves creating a new method that extends the core one and appends an async attribute to the script tag before it’s output.

However, this requires significant knowledge of Magento’s internals and layout system. It’s also not recommended for critical files like Prototype and jQuery, as mentioned earlier.

2. Use Layout XML Parameters

You could use layout XML parameters to specify that a script should be loaded after a certain element on the page. This effectively forces asynchronous loading by delaying the script until that element is processed.

<referenceContainer name="head">
    <block class="Magento\Framework\View\Element\Template" template=" path/to/your/script.phtml" />
    <block type="core/text_html" name="after.script.container" as="after_script_container">
        <action method="setText">
            <text>
                &lt;script type="text/javascript" src="//domain.com/js/prototype/prototype.js" async&gt;&lt;/script&gt;
            </text>
        </action>
    </block>
</referenceContainer>

This technique is more flexible and can be applied to any script, but it requires additional setup.

3. Use a Custom JavaScript File

You could create a custom JavaScript file that includes all your asynchronous scripts using the <script async> syntax. Then, you would include this custom file in your layout XML instead of using addJs.

This is the most straightforward approach, but it requires maintaining an additional script file and ensuring it’s up-to-date with any changes to your asynchronous scripts.

Conclusion

While Magento 2.x does not currently support forcing scripts loaded via addJs to be asynchronous by default, there are workarounds you can use to achieve this goal. The best approach depends on the specific requirements of your project and your level of comfort with customizing Magento’s core functionality.

Remember, any changes to Magento’s core should be done through proper extension development or theme overrides to ensure they don’t conflict with future updates. Always test your modifications thoroughly before deploying them to a production environment.

For more information on Magento layout XML and the addJs method, check out these resources on Magento 2 Layout XML Reference at LaravelCompany.

Magento 2 Database Performance Themes

Our network