Shopify Performance Optimization
Before writing code
Fetch live docs:
- Web-search
site:shopify.dev theme performance for theme optimization
- Web-search
site:shopify.dev hydrogen caching for Hydrogen caching strategies
- Web-search
site:web.dev core web vitals for current CWV guidelines and thresholds
- Web-search
site:shopify.dev image optimization cdn for image URL transforms
- Web-search
site:shopify.dev theme speed report for Shopify's built-in speed metrics
Liquid Rendering Performance
Template Optimization
- Minimize Liquid logic — complex loops and conditionals slow server-side rendering
- Use
{% render %} (not {% include %}) — isolated scope prevents variable conflicts
- Avoid nested loops —
O(n²) in Liquid is expensive
- Limit
forloop iterations with limit: parameter
- Pre-compute values with
{% assign %} instead of repeating expressions
Object Access
- Access specific properties:
{{ product.title }} not {{ product | json }}
- Avoid
all_products[handle] in loops — each is a separate data lookup
- Use section settings to pass data instead of global lookups
- Minimize use of
{{ content_for_header }} scripts (managed by Shopify — cannot remove, but minimize additional scripts)
Liquid Anti-Patterns
| Anti-Pattern |
Why It's Slow |
Better Approach |
Nested for loops |
O(n²) rendering |
Flatten data, use single loop |
all_products[handle] in loop |
Data fetch per iteration |
Pass products via section settings |
{% include %} with variables |
Shared scope causes conflicts |
Use {% render %} (isolated) |
Complex {% if %} chains |
Evaluated every render |
Simplify conditions, use {% case %} |
| Unused sections in templates |
Rendered even if hidden |
Remove from JSON template |
Asset Optimization
CSS
- Minimize CSS — remove unused styles
- Use
{{ 'style.css' | asset_url | stylesheet_tag }} for proper caching
- Critical CSS: inline above-the-fold styles in
<head>
- Defer non-critical CSS:
media="print">
JavaScript
- Defer non-critical JS:
<script defer> or dynamic import()
- Minimize JS bundles — Shopify themes don't need frameworks for most UI
- Use native browser APIs over jQuery
- Load third-party scripts asynchronously (
async attribute)
- Avoid render-blocking scripts in
<head>
Images
Shopify CDN image optimization:
# Responsive images with srcset
{{ image | image_url: width: 800 }}
{{ image | image_url: width: 400 }}
# Srcset pattern
<img
srcset="{{ image | image_url: width: 400 }} 400w,
{{ image | image_url: width: 800 }} 800w,
{{ image | image_url: width: 1200 }} 1200w"
sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
src="{{ image | image_url: width: 800 }}"
alt="{{ image.alt }}"
loading="lazy"
width="{{ image.width }}"
height="{{ image.height }}"
>
Key image practices:
- Use
loading="lazy" for below-the-fold images
- Use
fetchpriority="high" for LCP image
- Set explicit
width and height to prevent layout shift
- Shopify CDN automatically serves WebP/AVIF when supported
- URL parameters:
?width=, ?height=, ?crop=, ?format=
Fetch live docs: Web-search site:shopify.dev image_url filter parameters for current CDN transform options — new parameters are added over time.
Core Web Vitals
LCP (Largest Contentful Paint)
Target: < 2.5 seconds
- Preload hero/LCP image:
<link rel="preload" as="image" href="{{ image | image_url: width: 1200 }}">
- Use
fetchpriority="high" on LCP image
- Avoid lazy-loading above-the-fold images
- Minimize render-blocking CSS and JS
- Use server-side rendering (Liquid or Hydrogen SSR)
CLS (Cumulative Layout Shift)
Target: < 0.1
- Set explicit
width and height on all images and media
- Reserve space for dynamic content (ads, embeds, lazy-loaded images)
- Avoid inserting content above existing content after page load
- Use
aspect-ratio CSS property for responsive media containers
- Avoid dynamically injected banners or pop-ups that shift content
INP (Interaction to Next Paint)
Target: < 200ms
- Minimize main-thread blocking JavaScript
- Break up long tasks with
requestIdleCallback or setTimeout(fn, 0)
- Use CSS for animations and transitions (not JS)
- Debounce event handlers (scroll, resize, input)
- Avoid synchronous layout thrashing (read-then-write DOM patterns)
Fetch live docs: CWV thresholds and measurement methodology evolve. Web-search site:web.dev core web vitals thresholds for current targets.
Hydrogen Caching
Cache Strategies
// Pattern: apply cache strategy to storefront query
const data = await storefront.query(QUERY, {
cache: CacheLong(), // products, collections
});
const cart = await storefront.query(CART_QUERY, {
cache: CacheShort(), // dynamic data
});
| Strategy |
Use For |
CacheLong() |
Products, collections, pages |
CacheShort() |
Cart, personalized content |
CacheNone() |
Customer-specific data |
CacheCustom({...}) |
Fine-tuned scenarios |
Fetch live docs for exact TTL values — Hydrogen caching defaults may change across versions.
Streaming SSR
- Use
defer() in loaders for non-critical data
- Critical data renders immediately, deferred data streams in
- Show loading states with
<Suspense> + <Await>
- Preload routes with
<Link prefetch="intent">
Shopify CDN
Shopify's global CDN:
- Automatic for all theme assets and images
- Cache-Control headers managed by Shopify
- Image transformations via URL parameters
- No manual CDN configuration needed for themes
- Asset fingerprinting for cache busting
Measurement Tools
| Tool |
What It Measures |
| Shopify Theme Speed Report |
Overall theme score in admin |
| Google Lighthouse |
CWV + performance audit |
| WebPageTest |
Real-world loading waterfall |
| Chrome DevTools Performance |
JS profiling, layout shifts |
| Search Console CWV Report |
Field data from real users |
Best Practices
- Measure before optimizing — use Lighthouse, WebPageTest, Shopify's theme speed report
- Focus on LCP image optimization first (biggest impact for most stores)
- Lazy-load everything below the fold
- Minimize third-party scripts (analytics, chat widgets, social embeds)
- Use Shopify's built-in analytics over custom tracking scripts where possible
- For Hydrogen: cache aggressively, stream non-critical data, preload routes
- Test on real devices and slow connections (3G throttling)
- Set explicit dimensions on all media to prevent CLS
- Use
font-display: swap for custom fonts
Fetch the Shopify performance documentation, Core Web Vitals guides, and Hydrogen caching docs for exact optimization techniques and current best practices before implementing.
1---2name: shopify-performance3description: Optimize Shopify performance — Liquid rendering, asset optimization, CDN strategies, Core Web Vitals, Hydrogen caching, image optimization, preloading, and lazy loading. Use when improving Shopify store speed.4allowed-tools: Read, Write, Edit, Bash, Grep, Glob, WebSearch, WebFetch5metadata:6 source: https://skillmd.com/skills/orcaqubits/shopify-performance7---89# Shopify Performance Optimization1011## Before writing code1213**Fetch live docs**:141. Web-search `site:shopify.dev theme performance` for theme optimization152. Web-search `site:shopify.dev hydrogen caching` for Hydrogen caching strategies163. Web-search `site:web.dev core web vitals` for current CWV guidelines and thresholds174. Web-search `site:shopify.dev image optimization cdn` for image URL transforms185. Web-search `site:shopify.dev theme speed report` for Shopify's built-in speed metrics1920## Liquid Rendering Performance2122### Template Optimization2324- Minimize Liquid logic — complex loops and conditionals slow server-side rendering25- Use `{% render %}` (not `{% include %}`) — isolated scope prevents variable conflicts26- Avoid nested loops — `O(n²)` in Liquid is expensive27- Limit `forloop` iterations with `limit:` parameter28- Pre-compute values with `{% assign %}` instead of repeating expressions2930### Object Access3132- Access specific properties: `{{ product.title }}` not `{{ product | json }}`33- Avoid `all_products[handle]` in loops — each is a separate data lookup34- Use section settings to pass data instead of global lookups35- Minimize use of `{{ content_for_header }}` scripts (managed by Shopify — cannot remove, but minimize additional scripts)3637### Liquid Anti-Patterns3839| Anti-Pattern | Why It's Slow | Better Approach |40|-------------|--------------|-----------------|41| Nested `for` loops | O(n²) rendering | Flatten data, use single loop |42| `all_products[handle]` in loop | Data fetch per iteration | Pass products via section settings |43| `{% include %}` with variables | Shared scope causes conflicts | Use `{% render %}` (isolated) |44| Complex `{% if %}` chains | Evaluated every render | Simplify conditions, use `{% case %}` |45| Unused sections in templates | Rendered even if hidden | Remove from JSON template |4647## Asset Optimization4849### CSS5051- Minimize CSS — remove unused styles52- Use `{{ 'style.css' | asset_url | stylesheet_tag }}` for proper caching53- Critical CSS: inline above-the-fold styles in `<head>`54- Defer non-critical CSS: `media="print" onload="this.media='all'"`5556### JavaScript5758- Defer non-critical JS: `<script defer>` or dynamic `import()`59- Minimize JS bundles — Shopify themes don't need frameworks for most UI60- Use native browser APIs over jQuery61- Load third-party scripts asynchronously (`async` attribute)62- Avoid render-blocking scripts in `<head>`6364### Images6566Shopify CDN image optimization:6768```liquid69# Responsive images with srcset70{{ image | image_url: width: 800 }}71{{ image | image_url: width: 400 }}7273# Srcset pattern74<img75 srcset="{{ image | image_url: width: 400 }} 400w,76 {{ image | image_url: width: 800 }} 800w,77 {{ image | image_url: width: 1200 }} 1200w"78 sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"79 src="{{ image | image_url: width: 800 }}"80 alt="{{ image.alt }}"81 loading="lazy"82 width="{{ image.width }}"83 height="{{ image.height }}"84>85```8687Key image practices:88- Use `loading="lazy"` for below-the-fold images89- Use `fetchpriority="high"` for LCP image90- Set explicit `width` and `height` to prevent layout shift91- Shopify CDN automatically serves WebP/AVIF when supported92- URL parameters: `?width=`, `?height=`, `?crop=`, `?format=`9394> **Fetch live docs**: Web-search `site:shopify.dev image_url filter parameters` for current CDN transform options — new parameters are added over time.9596## Core Web Vitals9798### LCP (Largest Contentful Paint)99100Target: < 2.5 seconds101102- Preload hero/LCP image: `<link rel="preload" as="image" href="{{ image | image_url: width: 1200 }}">`103- Use `fetchpriority="high"` on LCP image104- Avoid lazy-loading above-the-fold images105- Minimize render-blocking CSS and JS106- Use server-side rendering (Liquid or Hydrogen SSR)107108### CLS (Cumulative Layout Shift)109110Target: < 0.1111112- Set explicit `width` and `height` on all images and media113- Reserve space for dynamic content (ads, embeds, lazy-loaded images)114- Avoid inserting content above existing content after page load115- Use `aspect-ratio` CSS property for responsive media containers116- Avoid dynamically injected banners or pop-ups that shift content117118### INP (Interaction to Next Paint)119120Target: < 200ms121122- Minimize main-thread blocking JavaScript123- Break up long tasks with `requestIdleCallback` or `setTimeout(fn, 0)`124- Use CSS for animations and transitions (not JS)125- Debounce event handlers (scroll, resize, input)126- Avoid synchronous layout thrashing (read-then-write DOM patterns)127128> **Fetch live docs**: CWV thresholds and measurement methodology evolve. Web-search `site:web.dev core web vitals thresholds` for current targets.129130## Hydrogen Caching131132### Cache Strategies133134```typescript135// Pattern: apply cache strategy to storefront query136const data = await storefront.query(QUERY, {137 cache: CacheLong(), // products, collections138});139140const cart = await storefront.query(CART_QUERY, {141 cache: CacheShort(), // dynamic data142});143```144145| Strategy | Use For |146|----------|---------|147| `CacheLong()` | Products, collections, pages |148| `CacheShort()` | Cart, personalized content |149| `CacheNone()` | Customer-specific data |150| `CacheCustom({...})` | Fine-tuned scenarios |151152> **Fetch live docs** for exact TTL values — Hydrogen caching defaults may change across versions.153154### Streaming SSR155156- Use `defer()` in loaders for non-critical data157- Critical data renders immediately, deferred data streams in158- Show loading states with `<Suspense>` + `<Await>`159- Preload routes with `<Link prefetch="intent">`160161## Shopify CDN162163Shopify's global CDN:164- Automatic for all theme assets and images165- Cache-Control headers managed by Shopify166- Image transformations via URL parameters167- No manual CDN configuration needed for themes168- Asset fingerprinting for cache busting169170## Measurement Tools171172| Tool | What It Measures |173|------|-----------------|174| Shopify Theme Speed Report | Overall theme score in admin |175| Google Lighthouse | CWV + performance audit |176| WebPageTest | Real-world loading waterfall |177| Chrome DevTools Performance | JS profiling, layout shifts |178| Search Console CWV Report | Field data from real users |179180## Best Practices181182- Measure before optimizing — use Lighthouse, WebPageTest, Shopify's theme speed report183- Focus on LCP image optimization first (biggest impact for most stores)184- Lazy-load everything below the fold185- Minimize third-party scripts (analytics, chat widgets, social embeds)186- Use Shopify's built-in analytics over custom tracking scripts where possible187- For Hydrogen: cache aggressively, stream non-critical data, preload routes188- Test on real devices and slow connections (3G throttling)189- Set explicit dimensions on all media to prevent CLS190- Use `font-display: swap` for custom fonts191192Fetch the Shopify performance documentation, Core Web Vitals guides, and Hydrogen caching docs for exact optimization techniques and current best practices before implementing.