Use Liquid filter chains instead of manual URL construction
Generate image markup through Shopify's optimized filter system instead of manually constructing URLs and attributes, to ensure automatic access to platform performance optimizations.
Manual URL construction bypasses Shopify's automatic optimizations: responsive srcset generation, width and height attributes, focal point positioning, format negotiation for WebP and AVIF, CDN parameters, and Early Hints support.
Filter chains are also faster to render than the equivalent hand-written Liquid, because the filters are implemented natively instead of being interpreted as template code. How much faster depends on the page: the gap is small on a page with a few images and large on a page that builds many URLs, such as a variant-heavy product page. Measure your own pages with Theme Inspector rather than assuming a fixed number.
Native filters are compiled to bytecode for faster execution, use optimized algorithms for efficient URL generation, cache aspect ratio and dimension calculations, and provide direct CDN integration with optimal URL parameters. Manual string manipulation runs interpreted Liquid code, which is significantly slower.
Filter chains are less error-prone and automatically benefit from platform improvements without code changes.
Anchor to The filter chain patternThe filter chain pattern
Pipe image objects through image_url then image_tag:
Because image_url requests a width of 800, candidate widths above 800 are dropped, and the rendered srcset contains the 400w, 600w, and 800w entries.
This generates complete optimized markup with srcset, dimensions, focal points, and proper CDN URLs.
Anchor to Automatic optimizationsAutomatic optimizations
- Responsive images: Automatically generates a
srcset. The candidate widths start from the defaults352,832,1200, and1920. If you passwidthtoimage_url, then that width is added to the set and the set is re-sorted. If you passwidthstoimage_tag, then your list replaces the defaults entirely. The set is then filtered to widths no larger than the width of theimage_urloutput, so the markup never asks for an upscaled image. If nothing survives that filter, then nosrcsetattribute is emitted at all. widthandheightattributes: Automatically generates correct dimensions based on aspect ratio, preventing CLS by reserving space before the image loads.- Focal point positioning: Automatically applies an
object-positionstyle if a focal point is set on the image. - Format negotiation: The CDN automatically serves WebP or AVIF when the browser supports them, reducing file size by 25 to 35 percent.
- Cache optimization: Includes a version parameter for proper cache invalidation.
- Early Hints support: Use the
preload: trueparameter to trigger HTTP 103 Early Hints, so the image can start downloading before HTML parsing.
Anchor to Migrate from manual constructionMigrate from manual construction
Identify manual construction patterns by searching your theme for:
| replace:in image contexts.- Direct
image.srcusage in<img>tags. - Manual
srcsetconstruction. - Hardcoded
widthandheighton images.
Replace these patterns:
Then enhance with performance parameters like widths, sizes, and loading.
Test to verify that images display correctly, check responsive behavior at different viewports, and measure performance improvement with Theme Inspector.
Anchor to Background imagesBackground images
For CSS background images, use filter for URL only:
<img> tags with object-fit: cover are more performant than background images because they support native lazy loading, responsive images through srcset, automatic dimensions (which prevents CLS), and fetch priority hints.
<img> tags with object-fit: cover are more performant than background images because they support native lazy loading, responsive images through srcset, automatic dimensions (which prevents CLS), and fetch priority hints.
Anchor to ExamplesExamples
Anchor to Manual construction anti-patternsManual construction anti-patterns
Common mistakes when building image URLs manually:
What's missing:
- No automatic
srcset. - No correct
widthandheightbased on aspect ratio. - No focal point positioning.
- No format negotiation.
- No CDN optimization parameters.
- No cache versioning.
- No Early Hints support.
- Breaks when Shopify updates the CDN URL structure.
- Doesn't benefit from the native filter implementations, so it renders more slowly.
Anchor to Recommended filter chain patternRecommended filter chain pattern
Anchor to Hero image with all optimizationsHero image with all optimizations
A single filter chain provides:
- Responsive images.
- Correct dimensions.
- Focal points.
- Format optimization.
- Early Hints.
- Future compatibility.
A manual implementation would require more than 20 lines of complex Liquid and would miss platform optimizations.
Anchor to Product gridProduct grid
Automatic benefits:
- Correct
widthandheightfor each image. - Responsive
srcsetfor all viewports. - The first 4 images load eagerly and the rest load lazily.
- Focal points are applied if set.
- WebP or AVIF is served when supported.
The {% paginate %} tag limits the query to 12 products, not the filter chain. Inside a paginate block the page size already bounds the loop, so a limit on the for tag is redundant.
Anchor to Replace manual ,[object Object], constructionReplace manual srcset construction
srcset constructionBefore (10 lines, no optimizations, complex):
After (4 lines, all automatic optimizations, simple):
Anchor to Collection images with croppingCollection images with cropping
Cropping is handled in the image_url filter, and responsive handling is in the image_tag filter.
Anchor to TestingTesting
- Use Theme Inspector to compare Liquid render time between manual construction and filter chains.
- Check the Chrome DevTools Network panel to verify that the
srcsetgenerates correct image URLs. - Use the Chrome DevTools Network panel to check for format negotiation (WebP or AVIF delivery). Look at the
Content-Typeresponse header. - Visually inspect to confirm that focal points apply correctly.
- Measure performance improvement before and after migration.
Anchor to ReferencesReferences
image_tagfilterimage_urlfilterproduct.featured_imagefocal_pointobject- Shopify CDN
- Responsive images on Shopify with Liquid
- Optimizing images for performance on Shopify