How to Remove Unwanted JS and CSS File in 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>
Leave a Comment
(0 Comments)
Useful Magento 2 Articles
Author Info
Chirag
Connect With Me