How to defer JavaScript execution in Magento 2
Being a boolean attribute, defer specifies that the script is executed after the page parsing. The attribute is aimed at external scripts only (the src attribute should be present). Find more information about defer
Despite Magento 2 supports standard script deferring, it never uses defer in its core. To change the situation, you can use the following code:
1 2 3 4 5 6 7 8 |
<?xml version='1.0'?> <page xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' layout='admin-login' xsi:noNamespaceSchemaLocation='../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd'> <head> <script src='Dfe_Login::main.js' defer='defer'/> </head> </page> |
More tips from Magento 2 Developer’s Cookbook