How to Remove Unwanted JS and CSS File in Magento 2

remove_js_css_file_magento_2

Magento 2 default luma theme includes so many JS and CSS files into the page. When we develop our custom theme, we are adding some more JS and CSS files. These JS and CSS lead to performance degradation and increase overall page load time.

But if you working on performance improvement and want to remove unwanted JS and CSS files you should consider below code snippet.

For example print.css file added into every pages of the Magento 2 website. It is use for removing some header and footer blocks from print page. It is not useful at all. To remove that css create default_head_blocks.xml file under app/design/frontend/vendor/theme/Magento_Theme/layout directory and add below code.

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <remove src="css/print.css"/>
    </head>
</page>

Same as CSS, you can remove unwanted JS files from being added to the page. Add below code snippet to remove JS file.

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <remove src="Vendor_Module::js/print.js"/>
    </head>
</page>

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

Was this post helpful? Please support Us!

Follow us on twitter or Like us on facebook.

 


To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close

Increase Your
Magento 2
Knowledge

Get Weekly Tutorial
to your Inbox
Subscribe Me
close-link
Subscribe Here