Remove extraneous JS files from Magento Modern Theme - ccard.js is on every page but only used at checkout

Remove extraneous JS files from Magento Modern Theme - ccard.js is on every page but only used at checkout

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Remove extraneous JS files from Magento Modern Theme - ccard.js is on every page but only used at checkout

Streamlining Magento Performance: Removing Unnecessary JS Files

Hello fellow Magento developers! Today I’m going to tackle a common performance issue that plagues many Magento stores - unnecessary JavaScript files being loaded on every page when they’re only needed in specific areas, like the checkout process. In this post, we’ll focus on removing the lib/ccard.js file from all pages except for the onepagecheckout area.

The Current Problem: Excessive JS File Loading

The ccard.js script handles credit card verification, which is obviously only needed during the checkout process in Magento. However, by default, it’s loaded on every single page of your site, including your homepage and category pages. This leads to unnecessary JavaScript requests and slows down your entire website.

The Invalid Approach: Commenting Out In page.xml

The naive approach would be to comment out the reference to ccard.js in your theme’s page.xml file:

<!--
<reference name="head">
    <action method="addJs"><script>lib/ccard.js</script></action>
</reference>
-->

While this removes the script from most pages, it also stops the script from loading anywhere at all. This means your onepagecheckout won’t have credit card verification either! Not a great solution.

The Proper Approach: Conditional Loading with layout XML

The correct way to handle this is by using Magento’s layout XML and conditionally adding the JavaScript file only in areas where it’s needed. Here’s how:

  1. Open your theme’s checkout.xml file (you can find this under app/design/themes/theme_name/layout/checkout.xml).

  2. Add a new reference for the “One page checkout main layout” section, and specify the script to be loaded there:

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layouts.xsd">
    <referenceContainer name="root" htmlClass="page-wrapper" innerWrap="true">
        <!-- ... existing content ... -->
        <referenceBlock name="head.components">
            <block type="Magento\Theme\Block\Html\Head"
                   as="head_components" template="html/head.phtml"/>
        </referenceBlock>
    </referenceContainer>

    <!-- Add this section here -->
    <onepagecheckout_mainLayout>
        <reference name="root">
            <action method="addJs"><script>lib/ccard.js</script></action>
        </reference>
    </onepagecheckout_mainLayout>
</layout>

Note that we’re using the addJs action within a specific layout handle (onepagecheckout_mainLayout). This ensures that the script is only loaded when the onepagecheckout is being rendered.

The Result: Improved Performance

After implementing this change, your website will no longer load the ccard.js file on every page. Instead, it’ll be selectively loaded only during the checkout process, exactly where it’s needed. This reduces JavaScript requests and improves your site’s performance.

Remember, keeping your Magento store lean and efficient is key to providing a great user experience. Regularly review and optimize your JS files like this for maximum impact.

If you’re looking for more tips on Magento performance optimization, be sure to check out our post on reducing Magento’s JavaScript bundle size.

Happy optimizing!

Checkout Catalog Performance Themes

Our network