Shopify image optimization: the right picture for every screen
- Topic
- Development
- Published
- Reading time
- 8 min read
- Written by
- Binaery
Shopify image optimization is mostly one decision made in the theme: which size of each picture to ask for. Shopify stores your original, resizes it on request and picks the file format for each browser. A theme that asks for a 2,000 pixel picture to fill a 180 pixel card on a phone is slow, however well the picture was compressed before it was uploaded.
This is for the developer on your team, or the founder who wants to know what to ask them. By the end you'll know what Shopify does for you, the few lines of code that ask for the right size, and which picture on each page has to be treated differently from all the others.
The short version
- Shopify resizes and compresses pictures for you, and serves a newer format such as WebP when the browser supports it. You don't upload five versions.
- The theme has to ask for a size. One line of Liquid gives the browser a list of sizes and a hint about how wide the picture will be shown.
- Pictures far down the page should load lazily. Shopify's image filter does that by itself from the fourth section on.
- The largest picture on the first screen is the exception. It loads at once, and it's the only one marked as high priority.
- Every picture needs a width and height in the code, so the page doesn't jump while it loads. Shopify's filter adds them.
- Upload a sensible original. Shopify can make a picture smaller. It never makes one larger than what you gave it.
Here's the one line, spread over six so it can be read:
{{ product.featured_image
| image_url: width: 1200
| image_tag:
widths: '400, 600, 800, 1200',
sizes: '(min-width: 990px) 50vw, 100vw'
}}
In plain words: take the product's main picture, make it available at four widths, and tell the browser the picture fills half the screen on a wide display and all of it on a narrow one. The browser then picks the smallest version that will look sharp. A phone gets a small file. A big monitor gets a big one.
What Shopify does with a picture you upload
Shopify keeps your original and serves every other version from its image CDN. The docs for the image_url filter, which is how a theme asks for a picture, set out the rules:
- The theme must ask for a width or a height. Shopify works out the other one.
- A picture can be resized up to 5,760 pixels on a side, and never beyond its original dimensions.
- Shopify detects which formats the browser supports, such as WebP and AVIF, and chooses one for quality and file size.
- If the theme doesn't name a quality, Shopify chooses one.
So the old habit of exporting a small, a medium and a large version of every photo isn't needed. One good original is enough. What's left is the theme's job.
Asking for the right size
A page doesn't know what screen it will be shown on. The fix has two parts, and Shopify's image_tag filter writes both.
A list of sizes. The filter prints the picture's address several times over, at different widths. By default Shopify picks a sensible set, up to the width the theme asked for. The widths setting in the code above replaces that with your own list.
A hint about the layout. The browser chooses from the list before it has laid out the page, so it needs to be told how wide the picture will be. That's the sizes setting. "Half the screen on desktop, all of it on a phone" is enough.
The mistake we'd look for first is a missing hint. Without it the browser assumes the picture fills the whole screen. For a small product card in a grid of four, that means a file several times larger than it needs.
The one picture that goes first
Speed is judged mostly by how quickly the largest thing on the first screen shows. Shopify ranks that number as good at 2,500 milliseconds or less. On most pages the largest thing is a picture. Not always.
Here are three homepages on a phone, from stores we designed for a paint brush brand, a meal delivery brand and a protein brand. On the first, a large photo is the first thing on the screen. On the second, it's a headline on a dark green panel, with the food further down. On the third, the headline and a list of four points come first, and the photo starts below them.
Paint brush brand
The homepage on a phone
Meal delivery brand
The homepage on a phone, as designed
Protein brand
The homepage on a phone, as designed
Three stores, three different answers to "what has to arrive first". On two of them it may well be words. The developer has to look at the real page on a real phone to know which picture, if any, deserves special treatment.
For that one picture, Shopify's guidance is clear:
- Never lazy load it. Lazy loading tells the browser to wait. Shopify calls doing this to the main picture one of the most common and damaging mistakes in Shopify themes.
- Mark it high priority, and only it. One per page.
- Don't fade it in. An animation delays the moment it counts as shown.
The filter helps without being asked. If a theme doesn't say how a picture should load, Shopify loads pictures in the first three sections at once and lazy loads those in the fourth section and after. A theme that sets this by hand everywhere can easily get it wrong somewhere.
Grids need a second rule
A collection page is one section holding as many as 50 products. The "first three sections" rule would load all 50 pictures at once. Shopify's answer is to count inside the loop:
{% for product in collection.products %}
{%- liquid
if forloop.index <= 4
assign image_loading = 'eager'
else
assign image_loading = 'lazy'
endif
-%}
{{ product.featured_image
| image_url: width: 400
| image_tag: loading: image_loading
}}
{% endfor %}
It reads: for the first four products, load the picture straight away. For every one after that, wait until the shopper scrolls near it. Four is Shopify's example. The right number is however many cards your grid shows on the first screen.
Why the page shouldn't jump
You've seen a page where the text drops down as a picture arrives above it. The browser didn't know how tall the picture would be, so it left no room. Shopify lists pictures without dimensions among the common causes of a poor layout stability score.
The fix is a width and a height written into the code for every picture. Shopify's filter adds both by default, worked out from the picture itself. The way to break it is to write the picture's code by hand and leave them off.
What to upload
Shopify's help center keeps this simple in its page on improving performance: use JPG for photos and PNG for graphics that need transparency, and size pictures appropriately. Two things follow from the rules above.
- Upload large enough for the biggest place the picture will be shown, on a sharp screen. Shopify can't add detail that isn't there.
- Don't upload far beyond that. A 6,000 pixel original for a picture that's never shown wider than 1,200 only slows down your own uploads.
A worked example: one product card
Say your collection grid shows two cards across on a phone, each 180 pixels wide. Phone screens are sharp, so the browser wants about twice that: a picture 360 pixels wide.
- If the theme asks for a 2,000 pixel picture and gives no list of sizes, every card downloads a 2,000 pixel file. That's about thirty times the pixels the card can show.
- With widths of 200, 400 and 600 and a hint that the card fills half the screen, the browser picks the 400 pixel version.
- Multiply by the 24 products on the page.
Nothing about the photos changed. The theme asked a better question.
What image optimization won't fix
- Scripts. Pictures are often blamed for what apps and tags are doing. Our article on what really slows a store down starts there.
- Too many pictures. Twelve sections of photography on a homepage is slow at any size.
- Bad pictures. A fast blurry photo is still a blurry photo.
- Video. A video that plays by itself at the top of the page is a different and bigger subject.
A checklist for your developer
- Is every picture printed with Shopify's image filters, not by hand?
- Does every picture have a list of sizes and a layout hint that matches where it's shown?
- On the homepage, a collection page and a product page, which thing is largest on the first screen of a phone?
- If it's a picture, does it load at once, with high priority, and without a fade?
- In grids, do only the cards on the first screen load straight away?
- Does any old lazy loading script still run in the theme?
- Does every picture have a width and a height in the code?
Common questions
Does Shopify optimize images automatically?
Partly. Shopify's image CDN resizes pictures on request, compresses them and serves a format such as WebP or AVIF when the browser supports it. The theme still has to ask for the right size for each place a picture is shown, and decide which pictures load first.
What size should Shopify images be?
Upload a picture large enough for the widest place it's shown on a sharp screen. Shopify can resize a picture up to 5,760 pixels and never beyond its original dimensions. The theme then requests smaller versions for smaller screens, so one good original is enough.
Should every image on a Shopify store be lazy loaded?
No. Pictures below the first screen should be lazy loaded, but the largest picture on the first screen should load at once. Shopify's developer docs call lazy loading that picture one of the most common and damaging mistakes in Shopify themes. Shopify's image filter loads the first three sections eagerly by default.
Do I need to convert images to WebP before uploading to Shopify?
No. Shopify detects which formats the visitor's browser supports and chooses one for quality and file size. Upload JPG for photos and PNG for graphics that need transparency, and let Shopify's image CDN do the conversion.
Next step
Open your own homepage on your phone and say out loud what the largest thing on the first screen is. Then ask your developer how that picture is loaded. Speed is one of the five areas our site audit covers. Tell us about your store and one of us will write back within 48 hours.
Keep reading.
Rather have us do it?
Everything in this article is something we do for a living. Tell us about the store and what you're trying to move.
Fly our work




