惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
The GitHub Blog
The GitHub Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园_首页
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
The Cloudflare Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Shopify Speed Optimization with Liquid Code (5 Patterns)
Md Kaspian F · 2026-05-06 · via DEV Community

I audited 14 Shopify themes last quarter for speed. 11 of them blamed apps. None had touched Liquid loop count, capture-in-loop allocations, or image output.

After optimizing 100+ Shopify stores over 12 years: the code-level patterns in your theme files account for 40-60% of total render time. Apps matter. Images matter. The template layer is where the compounding problems live.

Here are the 5 Liquid patterns that move the needle.


1. Drop capture from loops

assign stores a value. capture renders a full block and stores it as a string. Using capture inside a loop means a new string allocation on every iteration.

Slow — 48 allocations on a 48-product collection:

{% for product in collection.products %}
  {% capture product_card %}
    <div class="product-card">
      <h3>{{ product.title }}</h3>
      <p>{{ product.price | money }}</p>
    </div>
  {% endcapture %}
  {{ product_card }}
{% endfor %}

Enter fullscreen mode Exit fullscreen mode

Fast — direct output, zero allocations:

{% for product in collection.products %}
  <div class="product-card">
    <h3>{{ product.title }}</h3>
    <p>{{ product.price | money }}</p>
  </div>
{% endfor %}

Enter fullscreen mode Exit fullscreen mode

Use capture only when you need a reusable HTML block built once and output in a different location.


2. Cap nested loops with limit and {% break %}

Nested loops are the single biggest source of Liquid render time problems.

Slow — 2,500 iterations on a featured collections section:

{% for collection in collections %}
  {% for product in collection.products %}
    {% for image in product.images %}
      <img src="{{ image | image_url: width: 300 }}" alt="{{ image.alt }}">
    {% endfor %}
  {% endfor %}
{% endfor %}

Enter fullscreen mode Exit fullscreen mode

Fast — 32 iterations, using featured_image and limit:

{% for collection in collections limit: 4 %}
  {% for product in collection.products limit: 8 %}
    {% if product.featured_image %}
      <img src="{{ product.featured_image | image_url: width: 300 }}"
           alt="{{ product.featured_image.alt | default: product.title }}"
           width="300" height="300" loading="lazy">
    {% endif %}
  {% endfor %}
{% endfor %}

Enter fullscreen mode Exit fullscreen mode

Use {% break %} to stop early once you have the N items you need. Use {% continue %} to skip non-matching items without a nested if.

Real result: Factory Direct Blinds went from 4,800 collection iterations to 216. LCP dropped from 22s to 2.7s.


3. Output images with image_tag (srcset + dimensions)

This single change can improve LCP by 500ms+ on collection pages.

Slow — no srcset, no dimensions, no lazy loading:

<img src="{{ product.featured_image | image_url: width: 800 }}">

Enter fullscreen mode Exit fullscreen mode

Fast:

{{ product.featured_image | image_url: width: 800 | image_tag:
  srcset: "200,400,600,800",
  sizes: "(max-width: 768px) 100vw, 400px",
  loading: "lazy",
  decoding: "async",
  alt: product.featured_image.alt | default: product.title,
  width: 800,
  height: 800
}}

Enter fullscreen mode Exit fullscreen mode

Critical: Your hero and first visible product image should use loading: "eager", not lazy. Lazy-loading your LCP element is one of the most common speed mistakes I see on audits.

To handle this in a grid, conditionally eager-load the first 4:

{% for product in collection.products limit: 24 %}
  {% assign img_loading = forloop.index <= 4 | iif: "eager", "lazy" %}
  {{ product.featured_image | image_url: width: 600 | image_tag:
    loading: img_loading,
    width: 600,
    height: 600
  }}
{% endfor %}

Enter fullscreen mode Exit fullscreen mode


4. Preload hero image and critical font in theme.liquid

{% if template == 'index' %}
  {% assign hero_image = section.settings.hero_image %}
  {% if hero_image %}
    <link rel="preload" as="image"
          href="{{ hero_image | image_url: width: 1200 }}"
          imagesrcset="{{ hero_image | image_url: width: 600 }} 600w, {{ hero_image | image_url: width: 1200 }} 1200w"
          imagesizes="100vw">
  {% endif %}
{% endif %}

<link rel="preload" as="font" type="font/woff2"
      href="{{ 'your-heading-font.woff2' | asset_url }}" crossorigin>

Enter fullscreen mode Exit fullscreen mode

Wrap third-party preconnects in conditionals so they only fire when the feature is enabled:

{% if settings.enable_reviews %}
  <link rel="dns-prefetch" href="https://api.judge.me">
{% endif %}

Enter fullscreen mode Exit fullscreen mode


5. Push dynamic content onto the Section Rendering API

Instead of a full page reload to update one section, fetch just that section's HTML.

Slow — full page reload on filter click:

window.location.href = newUrl;

Enter fullscreen mode Exit fullscreen mode

Fast — Section Rendering API:

async function updateCollection(url) {
  const sectionId = 'collection-grid';
  const response = await fetch(`${url}?sections=${sectionId}`);
  const data = await response.json();
  document.getElementById(sectionId).innerHTML = data[sectionId];
}

Enter fullscreen mode Exit fullscreen mode

Response size drops from 100KB+ to 5-15KB. Perceived load time drops from 2-3 seconds to 200-400ms.

Good candidates: collection filtering, cart drawer, product recommendations, quick-view modals.


Before and after numbers

Store Metric Before After
WD Electronics Mobile LCP 9.3s 3.1s
WD Electronics Lighthouse 41 72
WD Electronics DOM Elements 4,200+ 1,100
Factory Direct Blinds Mobile PageSpeed 38 81
Factory Direct Blinds LCP 22.0s 2.7s
Factory Direct Blinds Liquid Render 840ms 65ms

Both stores saw measurable conversion improvements within 30 days of deploying the speed fixes.


How to verify in 5 minutes

  1. Install the Shopify Theme Inspector Chrome extension. Open DevTools, go to the Shopify tab, reload your slowest collection page. Total Liquid render time should be under 100ms. Over 200ms means a section is bleeding render budget.
  2. Open PageSpeed Insights on Mobile. Read Field Data first — that is real Chrome users, the metric Google ranks on.
  3. Check Search Console > Experience > Core Web Vitals for which URL groups are flagged Poor.

The full post (with more code and the FAQ section) is at kaspianfuad.com.

If you want a professional audit of your theme's Liquid performance, I run a Shopify speed-focused CRO audit that covers every pattern above plus the JS and third-party script layer.