How to defer javascript files in theme.liquid in shopify?

by hal.littel , in category: PHP CMS , a month ago

How to defer javascript files in theme.liquid in shopify?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , a month ago

@hal.littel 

To defer JavaScript files in theme.liquid in Shopify, follow these steps:

  1. Open your Shopify admin dashboard and go to Online Store > Themes.
  2. Click on the Actions dropdown menu and select Edit code.
  3. In the Layout section, locate the theme.liquid file.
  4. Find the tag in the theme.liquid file.
  5. Add the defer attribute to the script tag that includes the JavaScript file you want to defer. For example, if you want to defer a script called script.js, the code would look like this:
1
<script src="{{ 'script.js' | asset_url }}" defer></script>


  1. Save your changes and check your website to confirm that the JavaScript file is now deferred.


By adding the defer attribute to the script tag in the theme.liquid file, you are telling the browser to load the JavaScript file in the background and defer its execution until after the document has finished parsing. This can help improve the loading speed of your Shopify store.