How to Lazy Load Images on Shopify to Make Your Site Faster

What is Lazy Loading?

Lazy Loading is a performance optimization technique that loads images as the user scrolls down the page, instead of during the initial page load.

Also known as “defer offscreen images“.

We often see a 10% – 30% reduction in page weight by implementing lazy loading.

On the browser side, the page loads faster because it’s not loading all the images right away.

On the tech side, it works by replacing the html image src attribute with a data-src placeholder – this prevents the image from loading in the standard html sequence. Then javascript is used to grab that placeholder url and update the element’s src attribute, loading the image when it comes into view.

The Google effect: Lazy loaded images don’t contribute to the initial page load, so your site is lighter and gets the benefits that come with a faster load time (improved rankings and traffic).

Why is Lazy Loading Awesome?

The page loads faster!

Faster pages = better SEO rankings

Faster pages = better conversion rates

Consider this:

Now let’s take a look at this example of a customer’s home page before and after implementing lazy loading:

BEFORE

Pretty heavy page with a slow load time at this point, buuuuuut…

AFTER

Check the highlights after this one optimization:

Mobile load time improved by over 2.5 seconds

20 fewer server requests

Page weight decreased by 1MB

Nice!

Those stats are using webpagetest.org with mobile LTE 12Mbs download speed.

The site is still heavy at about 10MB (we can usually get sites down to 1MB – 2MB with a full optimization… but even with just the lazy loading you can see the significant load time reduction.

How to Add Lazy Loading to Your Shopify Theme

Add the lazysizes.js library to your theme assets folder and include it in theme.liquid

Locate the image tags in your theme files

Update those image tags by swapping src with data-source and adding the “lazyload” class

The detailed instructions to implement using lazysizes are on that page if you scroll down.

Sometimes you can run into problems, depending how the theme code is setup, but that’s part of the fun in learning 🤓

The exact files to edit in your Shopify site will vary based on your theme, but generally you want to look for the template code that loads images, and swap in the lazy loading syntax explained by lazysizes (or whatever library you end up choosing).

For example, if you’re using the lazysizes library I recommended, once you have that installed in your theme.liquid file, you want to swap out your standard img tags:

<img src="IMAGE_URL">

With the new lazy loading syntax:

<img data-src="IMAGE_URL" class="lazyload">

To install the library, download the latest file from that lazysizes page, upload to your Shopify theme Assets folder and name it lazysizes.min.js, then include it in your theme.liquid <head> section like this:

*The <style> part is used to create a subtle CSS animated fade in effect.

Once you have the code setup, you’ll see your images fade into view as you scroll down your site, rather than all at the initial page load (you can also refresh this page and scroll down really fast to try and catch the affect here).

Lazy loading can usually speed up most of your site – Home, Collections, Product pages, and Blog posts all benefit.

When we implement lazy loading, most of your store is good for the future and will automatically lazy load images. In some cases there may require manual intervention for future elements, but we’ll explain if that’s the situation in your theme.

How to check if lazy loading is set up on your website

To see if you have lazy loading on your site, you can run it through the Shopify Analyzer.

A second way to check is by inspecting the browser code by right clicking on your page, then selecting “View source”. Search for “lazy” on that page – if there aren’t any elements with “lazy” then you probably don’t have it.

If you view source and see some “lazy” elements, it’s still worth running through an analyzer tool to make sure they’re all loaded with “true lazy loading” (and not just “placeholder lazy loading”).

What images should you lazy load?

You’ll get the most gains by lazy loading:

collection page images

home page images

related products

blog images

Sometimes you might see a warning in analysis tools for icons or small graphics – I would’t worry about these.

The key benefits from lazy loading come from deferring large graphics and product images.

True lazy loading vs placeholder lazy loading

You might have lazy loading already set up, but still getting warnings in analysis tools.

This is likely because you have “placeholder lazy loading” instead of “true lazy loading”.

True Lazy Loading: Images don’t have a src attribute, so they don’t load on page load, only when the user scrolls into view.

Placeholder Lazy Loading: Images have a small placeholder thumbnail that loads on initial page load, then as the user scrolls the larger regular image replaces the thumbnail.

Sometimes having placeholders is a design preference, and that’s ok. But if you want to maximize performance we recommend converting to “true lazy loading”.

Lazy loading and SEO

When you lazy load images, your html image elements load normally. The only thing that changes from the html side is using the attribute data-src instead of src, which prevents the image from showing to the viewer until it triggers to load.

From an SEO standpoint, the important alt attribute is still present for page crawlers, and the page loads faster with lazy loading, so we recommend it for both performance and SEO.

Some of our posts contain affiliate links, meaning Speed Boostr could receive a commission if you sign up through these links. We only recommend apps and services we believe in. Please read our affiliate discaimer for more information.

Hey Dino, it looks good to me. Lazy loading will only apply to images under the fold. So for images close to the fold it will depend on the device being accessed.

Another possibility is that if your home page slider / banner is slower to load (javascript based, video, etc) then images under it may be moved up during load, then pushed back down. If they get moved up then they get loaded.

As long as you don’t have the “src” attribute (and use “data-src” instead), and have the lazysizes script loading in the

, then the images will lazy load when they’re able to (in most cases, other than the exceptions I mentioned).

Hi Joe. I’m not a developer but I tried to follow the above tutorial and seem to have screwed up my website. My images are appearing only when I’m moving my cursor over them. I need to undo this. Please help!

Hey Varsha, I took a look at your site and found the problem: I see you have a call to the lazysizes.min.js file on your site, but the link is broken. This probably means that the code you have in your theme.liquid file is incorrect, or you haven’t uploaded the lazysizes library into your theme Assets folder.

Try those 2 things. If you get stuck you can contact us. Also, I recommend duplicating your live theme whenever you make code edits, this way you can test the edits before they publish live :)