I used to write my own lazy-loading logic in every Shopify theme. Then I read the image_tag docs properly and deleted most of it. Shopify already does more than people think, and the two things it doesn't do are the ones that actually move LCP.

Here's what you get for free, what you still have to write, and a hero snippet I now paste into most theme builds.

What image_tag does on its own

{{ product.featured_image | image_url: width: 1200 | image_tag }} 

That one line gives you:

  • width and height attributes, taken from the image's dimensions. No layout shift as the image loads.
  • A srcset with a set of widths Shopify picks, up to the width you asked for in image_url.
  • loading="lazy" on images in sections further down the page.
  • A modern format. image_url serves whatever the browser supports, WebP or AVIF included. You don't need <picture> for this.

Notice the catch in the second point. The srcset tops out at the width in image_url. If you write image_url: width: 600 because that's how big the card looks in your design, a retina laptop never gets more than 600px and the image looks soft. Ask for the biggest size the image could ever display at, and let srcset hand out smaller ones. The cap is 5760px.

Gap one: sizes

image_tag doesn't know how wide your image is in the layout. Without a sizes attribute, the browser assumes the image fills the whole viewport (that's the HTML default, 100vw) and picks from the srcset accordingly.

On a product grid that's a lot of wasted bytes. A card in a four-column grid on desktop is about a quarter of the screen, but the browser downloads a full-width file for each one.

Tell it the truth:

{{ card_product.featured_media | image_url: width: 1200 | image_tag: sizes: '(min-width: 990px) 25vw, (min-width: 750px) 33vw, 50vw', alt: card_product.featured_media.alt | escape }} 

Match the breakpoints to your grid CSS. They don't have to be exact. Close is a big improvement over "assume full width".

Gap two: the hero

The lazy default helps lower sections. It does nothing to make the first image load faster, and the first image is usually your LCP element. For that you want three things:

  1. loading="eager" (or no lazy), so it's never deferred.
  2. fetchpriority="high", so the browser fetches it before less important stuff.
  3. A preload hint, so the request starts before the HTML parser even reaches the <img>.

image_tag has a preload parameter for the third one. It sends a Link preload header with the srcset and sizes included. It doesn't change the <img> itself, and Shopify's docs say to use it sparingly, which makes sense: preload everything and you've prioritised nothing.

For the first two, image_tag passes any parameter that matches an HTML attribute straight through, so loading and fetchpriority just work.

Knowing which section is first

Hard-coding "this section is the hero" breaks the moment the merchant drags a different section to the top. section.index solves that. It's the 1-based position of the section within its location.

The location part matters: the header lives in its own section group, so the first section in the template is index 1 even though the header is above it.

{%- liquid assign is_first = false if section.index == 1 and section.location == 'template' assign is_first = true endif if is_first assign img_loading = 'eager' assign img_priority = 'high' else assign img_loading = 'lazy' assign img_priority = 'auto' endif -%} {%- if section.settings.image -%} {{ section.settings.image | image_url: width: 3200 | image_tag: sizes: '100vw', loading: img_loading, fetchpriority: img_priority, preload: is_first, class: 'hero__image', alt: section.settings.image.alt | escape }} {%- endif -%} 

Drop it into an image banner or slideshow section (first slide only for a slideshow) and it adapts wherever the merchant puts it.

Two things that will confuse you

It looks wrong in the theme editor. section.index returns nil in the online store editor, so is_first is false there and the hero renders as lazy. That's expected. Check the real storefront, not the editor preview.

It returns nil in a few other places too. Static sections (rendered with {% section %} in a layout) and responses from the Section Rendering API also get nil. If your hero is a static section, you'll need to set it eager by hand.

How I check it worked

Open the storefront in Chrome, run a Lighthouse test on mobile and expand the LCP element in the report. It should be your hero image, and the "lazy load" warning for it should be gone. Then in the Network panel, filter by Img and confirm the hero request starts near the top of the waterfall, and that the grid images are small files, not full-width ones.

Plenty of custom sections I open are missing one or both. The sizes fix is a one-line change per snippet, so it's the first thing I'd do.

If you're working through a slow store, this is part of a longer checklist I keep for Shopify speed work: https://rizehub.ph/blog/shopify-speed-optimization-checklist