Skip to content

Conversation

@octipus
Copy link

@octipus octipus commented Jan 23, 2026

Improve image snippet with responsive image best practices

Summary

This PR enhances the snippets/image.liquid snippet to follow Shopify's performance best practices for responsive images, based on recommendations from:

Changes

New Features

  1. Responsive Image Support (srcset and sizes)

    • Added widths parameter to generate multiple image sizes for srcset
    • Added sizes parameter to provide responsive display hints to the browser
    • Automatically defaults to base width and 2x version if widths is not provided
  2. Loading Strategy Control

    • Added loading parameter (defaults to 'lazy' for below-fold images)
    • Can be set to 'eager' for LCP images to improve Largest Contentful Paint
  3. Fetch Priority Support

    • Added optional fetchpriority parameter
    • Use 'high' for LCP images to prioritize their loading
  4. Picture Tag for Mobile DPR Capping

    • Added optional use_picture parameter
    • Prevents oversized images on high-DPR mobile devices (3x+) by capping at 2x
    • Configurable mobile_breakpoint parameter (default: 800px)
  5. CSS Updates

    • Added styles to support <picture> tag structure
    • Maintains existing image wrapper styles

Backward Compatibility

All existing usage patterns continue to work without any changes. All new parameters are optional with sensible defaults.

Usage Examples

{% comment %} Basic usage (backward compatible) {% endcomment %}
{% render 'image', image: product.featured_image %}

{% comment %} With responsive srcset {% endcomment %}
{% render 'image',
  image: product.featured_image,
  widths: '300, 600, 900',
  sizes: '(min-width: 800px) 600px, 100vw'
%}

{% comment %} LCP image with high priority {% endcomment %}
{% render 'image',
  image: product.featured_image,
  loading: 'eager',
  fetchpriority: 'high',
  widths: '600, 1200'
%}

{% comment %} With picture tag for mobile DPR capping {% endcomment %}
{% render 'image',
  image: collection.featured_image,
  use_picture: true,
  widths: '300, 600, 1200',
  sizes: '(min-width: 800px) 600px, 100vw'
%}

Performance Improvements

Testing with a 2000×1000px image demonstrates significant performance gains:

Metric Before After Improvement
File Size 680 KB 234 KB 66% reduction
Load Time 141 ms 55 ms 61% faster

These improvements come from:

  • Responsive srcset delivering appropriately sized images
  • Automatic format optimization (AVIF/WebP) via Shopify's CDN
  • Proper sizes attribute preventing oversized downloads

Benefits

  • Improved Performance: Responsive images reduce bandwidth usage and improve load times
  • Better LCP: loading: 'eager' and fetchpriority: 'high' optimize LCP images
  • Mobile Optimization: Picture tag prevents oversized images on high-DPR mobile devices
  • Developer Experience: Comprehensive documentation and examples in LiquidDoc comments

Testing

  • ✅ Verified backward compatibility with existing usage in sections/product.liquid, sections/collection.liquid, sections/cart.liquid, and others
  • ✅ No linter errors
  • ✅ All existing parameters (image, url, class, width, height, crop) work as before

- Updated custom-section.liquid to utilize the new image rendering method with responsive attributes.
- Modified image.liquid to include additional parameters for improved accessibility and performance, such as alt text, loading strategy, and srcset handling.
- Implemented picture tag support for better mobile image optimization and added default values for loading and widths.

These changes improve the flexibility and responsiveness of image handling in the theme.
@octipus
Copy link
Author

octipus commented Jan 23, 2026

I have signed the CLA!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant