331A96E0-3037-43FC-8D93-543B900B11C9

😍 Loving this theme? Download

Please enter at least 3 characters.

How to lazy load any script in Prime theme?

Scripts take huge resources and they can slow down webpage speed if not optimized. Lazy load scripts that are not needed upon page load.
How to lazy load any script in Prime theme?
How to lazy load any script in Prime theme?

Prime uses @shinsenter/defer.js to lazy load all images, iframes and videos. Now, with new update you can easily load scripts on user activity or after certain time. 👏

Here's how you can achieve it.

Without lazy loading of script:

<script type="text/javascript" src="/feeds/posts/summary/-/Food?published&amp;alt=json-in-script&amp;callback=labelthumbs" ></script>

With lazy loading of script:

<script type="more-lazyscript" src="/feeds/posts/summary/-/Food?published&amp;alt=json-in-script&amp;callback=labelthumbs" ></script>
In our theme, we used this custom code to implement the functionality.
<script>
// Script with type of "more-lazyscript" loads after 700ms
  Defer.all('script[type="more-lazyscript"]', 700);  

// Script with type of "activity-lazyscript" loads after 700ms but after a user activity is triggered
  Defer.all('script[type="activity-lazyscript"]', 700, true);   
</script>

If you set true, the load time of script is calculated upon user activity such as scroll.

You can define your own lazy load script. Try it on adsense and let me know what code you set.

Some Random Content to extend this article

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. 

The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Image by catalyststuff on Freepik

Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. 

Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

 

Happy Blogging 👦

Share this post

Explore more articles by Anonymous

Post a Comment

Please leave comments related to the content. All comments are highly moderated and visible upon approval.

Comment Shortcodes

  • To insert an image, add [img]image_link[/img]
  • To insert a block of code, add [pre]parsed_code[/pre]
  • To insert a link, add [link=your_link]link_text[/link]
  • To insert a quote, add [quote]quote_text[/quote]
  • To insert a code, add [code]parsed_code[/code]
By clicking "Accept", you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and improve marketing.