Magento 2 load JS at the end

Magento 2 load JS at the end

July 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 load JS at the end

Improving Magento 2 Performance: Loading JavaScript Files at the End

Slow loading times on your e-commerce site can significantly impact user experience and conversion rates. If you’ve identified that the JavaScript files are slowing down your product page in Magento 2, this guide will help you optimize their loading order.

Understanding Magento’s Default Behavior

By default, Magento 2 loads all JavaScript files at the bottom of the <body> tag using the Mage_Template_Block::appendJs() method. This is a performance best practice as it allows for parallel downloads and avoids blocking other resources during rendering.

However, if you find that certain scripts are critical only after the page has fully loaded (like analytics or recommendation plugins), moving them to the end can provide a noticeable speed boost.

Moving Scripts to the End

To modify the script loading order in Magento 2, you’ll need to override the app/design/frontend/Your_Theme/theme.js file. Create this file if it doesn’t exist, and add your custom JavaScript at the very end of the array. Here’s an example:

var config = {
    mixins: {
        'mixins/js/jquery/purl': 'Magento_Page/js/mixins/jquery/purl',
        'mage/dataPost': 'Vendor_Module/js/mage/dataPost-mixin'
    },
    requirejs: {
        waitSeconds: 20,
        paths: {
            // Your custom paths
        }
    },
    init: function () {
        // ... existing code ...
        require(['jquery', 'Magento_Catalog/js/catalog-add-to-cart'], function($) {
            // Your custom script here, running after the page has loaded
            console.log('Running at the end of the page load');
        });
    }
};

In this example, we’re adding a new require call at the end of the init method. The script inside this call will execute after all other scripts have loaded.

Avoiding Common Pitfalls

  • Don’t move core scripts: Only move non-critical third-party or custom scripts to the end.
  • Test thoroughly: Moving scripts can sometimes break functionality, so ensure you test your site extensively.
  • Consider lazy loading: Instead of moving scripts to the bottom, consider using data-load-event="after" in your view files for more granular control.

Conclusion: Optimizing JavaScript Loading in Magento 2

Improving JavaScript performance is a key strategy for enhancing your Magento store’s speed. By understanding the default behavior and applying targeted optimizations like moving scripts to the end, you can achieve significant performance gains with minimal effort.

For more advanced techniques like code splitting and asynchronous loading of Magento modules, consider referring to our Magento 2 Performance Optimization Guide at Laravel Company.

Remember, every optimization should be tested in a staging environment first. Always keep backups and monitor your site’s performance using tools like Google Lighthouse after making changes.

Happy optimizing!

Magento 2 Catalog Database Performance

Our network