I Only Speak Liquid

Native Loading vs. Lazy Libraries

Anthony
|
December 15, 2023

What I’ve been thinking about:

I had a client recently ask me if their premium Shopify theme had lazy loading implemented as they couldn't see a lazysizes.js file. Great question - I knew it did, but it still triggered some further digging on my end.

What is lazy loading?

Lazy loading defers the loading of certain areas of the webpage, especially images below the fold, until they are needed. This can help speed up page load times.

There's a couple of simple ways to implement lazy loading - using native browser-level lazy loading, and/or using a js library like lazysizes.js

Native Browser Lazy Loading (AKA - HTML attributes)

You can now use the attribute loading="lazy" within your img element to defer loading of the resource. This is supported by most modern browsers.

Eg.

<img src="image.png" loading="lazy" alt="…" width="200" height="200">

You can do this easily in liquid using image_tag (which has replaced the deprecated img_tag)

Eg.

image_tag: class: 'custom-class', loading: 'lazy'

Pros

  • Liquid outputs the loading attribute, the img srcset, and alt tags for you
  • Doesn't require js, so users that block js won't be affected
  • Doesn't need an additional library

Cons

  • You have to ensure you don't use loading="lazy" on content that appears above the fold
  • Might require a js library to polyfill older browsers anyway

Lazy Loading Library

lazysizes is hugely popular and quite simple to implement. You add the file to your assets folder, and include it in theme.liquid. You then apply the lazyload class to your images. It can also calculate the sizes attribute responsively, using data-sizes="auto" and an appropriate data-srcset.

eg.

<img data-sizes="auto" data-srcset="responsive-image1.jpg 300w, responsive-image2.jpg 600w, responsive-image3.jpg 900w" class="lazyload" />

Pros

  • Simple to set up and implement
  • Relatively lightweight and can reduce the amount of liquid logic required to build the responsive <img> element.
  • Calculates sizes automatically when using appropriate srcset.

Cons

  • An additional library to add and load.

How to check your theme for lazysizes.js

Sometimes theme developers include lazysizes or an alternate library as part of their vendor.js file. This was the reason my client couldn't spot an additional lazysizes.js file in network - so be sure to check thoroughly before implementing anything new!

3 links you can’t miss:

  • Regex101: Regex has never come naturally to me - I'm not sure why. Regex101 lets you enter a set of strings, and has a handy lookup to help you build out your regular expressions.
  • Dummy Image Generator: Sick of the 'What size does this image need to be?' question from clients? Quickly create dummy images with a size displayed on them to pop in as temp content for your clients as you build.
  • Starter Story: I love entrepreneur stories and the DTC world in general. This is a little newsletter I've been subbed to since it launched. It has interviews with founders/business owners at all scales, and industries.

One app I like:

Accentuate Custom Fields by Accentuate

Even with the leaps and bounds Shopify has made with metafields recently, an app like Accentuate Custom Fields still has its use case.

ACF gives you the power to extend metafield functionality with an amazing UI (for your clients), and features like repeatable fields.

I've used it to create custom product forms for a few different clients. The repeatable fields feature allows me to set up a UI for the client that helps them create each step in their custom product form - whether they need to put together different steps for customisable jewellery products, or for different steps in a customisable cake.

Accentuate gives the client a simple UI to create each unique step for each product - while storing all of the data in metafields. You can then loop through the metafields and render out your custom form in liquid and JS.

7,93
15,86
23,8
31,73
39,66
47,6
55,53
63,46
71,4

What I’ve been thinking about:

I had a client recently ask me if their premium Shopify theme had lazy loading implemented as they couldn't see a lazysizes.js file. Great question - I knew it did, but it still triggered some further digging on my end.

What is lazy loading?

Lazy loading defers the loading of certain areas of the webpage, especially images below the fold, until they are needed. This can help speed up page load times.

There's a couple of simple ways to implement lazy loading - using native browser-level lazy loading, and/or using a js library like lazysizes.js

Native Browser Lazy Loading (AKA - HTML attributes)

You can now use the attribute loading="lazy" within your img element to defer loading of the resource. This is supported by most modern browsers.

Eg.

<img src="image.png" loading="lazy" alt="…" width="200" height="200">

You can do this easily in liquid using image_tag (which has replaced the deprecated img_tag)

Eg.

image_tag: class: 'custom-class', loading: 'lazy'

Pros

  • Liquid outputs the loading attribute, the img srcset, and alt tags for you
  • Doesn't require js, so users that block js won't be affected
  • Doesn't need an additional library

Cons

  • You have to ensure you don't use loading="lazy" on content that appears above the fold
  • Might require a js library to polyfill older browsers anyway

Lazy Loading Library

lazysizes is hugely popular and quite simple to implement. You add the file to your assets folder, and include it in theme.liquid. You then apply the lazyload class to your images. It can also calculate the sizes attribute responsively, using data-sizes="auto" and an appropriate data-srcset.

eg.

<img data-sizes="auto" data-srcset="responsive-image1.jpg 300w, responsive-image2.jpg 600w, responsive-image3.jpg 900w" class="lazyload" />

Pros

  • Simple to set up and implement
  • Relatively lightweight and can reduce the amount of liquid logic required to build the responsive <img> element.
  • Calculates sizes automatically when using appropriate srcset.

Cons

  • An additional library to add and load.

How to check your theme for lazysizes.js

Sometimes theme developers include lazysizes or an alternate library as part of their vendor.js file. This was the reason my client couldn't spot an additional lazysizes.js file in network - so be sure to check thoroughly before implementing anything new!

3 links you can’t miss:

  • Regex101: Regex has never come naturally to me - I'm not sure why. Regex101 lets you enter a set of strings, and has a handy lookup to help you build out your regular expressions.
  • Dummy Image Generator: Sick of the 'What size does this image need to be?' question from clients? Quickly create dummy images with a size displayed on them to pop in as temp content for your clients as you build.
  • Starter Story: I love entrepreneur stories and the DTC world in general. This is a little newsletter I've been subbed to since it launched. It has interviews with founders/business owners at all scales, and industries.

One app I like:

Accentuate Custom Fields by Accentuate

Even with the leaps and bounds Shopify has made with metafields recently, an app like Accentuate Custom Fields still has its use case.

ACF gives you the power to extend metafield functionality with an amazing UI (for your clients), and features like repeatable fields.

I've used it to create custom product forms for a few different clients. The repeatable fields feature allows me to set up a UI for the client that helps them create each step in their custom product form - whether they need to put together different steps for customisable jewellery products, or for different steps in a customisable cake.

Accentuate gives the client a simple UI to create each unique step for each product - while storing all of the data in metafields. You can then loop through the metafields and render out your custom form in liquid and JS.

Talk to an expert
Discuss the project for free
with a Storetasker Expert
From an Expert
Anthony Vita
7,93
15,86
23,8
31,73
39,66
47,6
55,53
63,46
71,4