{"id":1905,"date":"2026-05-05T23:12:04","date_gmt":"2026-05-05T16:12:04","guid":{"rendered":"https:\/\/daiilynews.cu.ma\/shopify-speed-optimization-with-liquid-code-5-patterns\/"},"modified":"2026-05-05T23:12:04","modified_gmt":"2026-05-05T16:12:04","slug":"shopify-speed-optimization-with-liquid-code-5-patterns","status":"publish","type":"post","link":"https:\/\/daiilynews.cu.ma\/?p=1905","title":{"rendered":"Shopify Speed Optimization with Liquid Code (5 Patterns)"},"content":{"rendered":"<p> <br \/>\n<br \/>\n                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.<\/p>\n<p>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.<\/p>\n<p>Here are the 5 Liquid patterns that move the needle.<\/p>\n<p>  1. Drop capture from loops<\/p>\n<p>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.<\/p>\n<p>Slow \u2014 48 allocations on a 48-product collection:<\/p>\n<p>{% for product in collection.products %}<br \/>\n  {% capture product_card %}<\/p>\n<p>      {{ product.title }}<br \/>\n      {{ product.price | money }}<\/p>\n<p>  {% endcapture %}<br \/>\n  {{ product_card }}<br \/>\n{% endfor %}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Fast \u2014 direct output, zero allocations:<\/p>\n<p>{% for product in collection.products %}<\/p>\n<p>    {{ product.title }}<br \/>\n    {{ product.price | money }}<\/p>\n<p>{% endfor %}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Use capture only when you need a reusable HTML block built once and output in a different location.<\/p>\n<p>  2. Cap nested loops with limit and {% break %}<\/p>\n<p>Nested loops are the single biggest source of Liquid render time problems.<\/p>\n<p>Slow \u2014 2,500 iterations on a featured collections section:<\/p>\n<p>{% for collection in collections %}<br \/>\n  {% for product in collection.products %}<br \/>\n    {% for image in product.images %}<br \/>\n      {{ image | image_url: width: 300 }}&#8221; alt=&#8221;https:\/\/dev.to\/mdkaspianfuad\/{{ image.alt }}&#8221;><br \/>\n    {% endfor %}<br \/>\n  {% endfor %}<br \/>\n{% endfor %}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Fast \u2014 32 iterations, using featured_image and limit:<\/p>\n<p>{% for collection in collections limit: 4 %}<br \/>\n  {% for product in collection.products limit: 8 %}<br \/>\n    {% if product.featured_image %}<br \/>\n      {{ product.featured_image | image_url: width: 300 }}&#8221;<br \/>\n           alt=&#8221;https:\/\/dev.to\/mdkaspianfuad\/{{ product.featured_image.alt | default: product.title }}&#8221;<br \/>\n           width=&#8221;300&#8243; height=&#8221;300&#8243; loading=&#8221;lazy&#8221;><br \/>\n    {% endif %}<br \/>\n  {% endfor %}<br \/>\n{% endfor %}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Use {% break %} to stop early once you have the N items you need. Use {% continue %} to skip non-matching items without a nested if.<\/p>\n<p>Real result: Factory Direct Blinds went from 4,800 collection iterations to 216. LCP dropped from 22s to 2.7s.<\/p>\n<p>  3. Output images with image_tag (srcset + dimensions)<\/p>\n<p>This single change can improve LCP by 500ms+ on collection pages.<\/p>\n<p>Slow \u2014 no srcset, no dimensions, no lazy loading:<\/p>\n<p>{{ product.featured_image | image_url: width: 800 }}&#8221;><\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Fast:<\/p>\n<p>{{ product.featured_image | image_url: width: 800 | image_tag:<br \/>\n  srcset: &#8220;200,400,600,800&#8221;,<br \/>\n  sizes: &#8220;(max-width: 768px) 100vw, 400px&#8221;,<br \/>\n  loading: &#8220;lazy&#8221;,<br \/>\n  decoding: &#8220;async&#8221;,<br \/>\n  alt: product.featured_image.alt | default: product.title,<br \/>\n  width: 800,<br \/>\n  height: 800<br \/>\n}}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Critical: Your hero and first visible product image should use loading: &#8220;eager&#8221;, not lazy. Lazy-loading your LCP element is one of the most common speed mistakes I see on audits.<\/p>\n<p>To handle this in a grid, conditionally eager-load the first 4:<\/p>\n<p>{% for product in collection.products limit: 24 %}<br \/>\n  {% assign img_loading = forloop.index  4 | iif: &#8220;eager&#8221;, &#8220;lazy&#8221; %}<br \/>\n  {{ product.featured_image | image_url: width: 600 | image_tag:<br \/>\n    loading: img_loading,<br \/>\n    width: 600,<br \/>\n    height: 600<br \/>\n  }}<br \/>\n{% endfor %}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  4. Preload hero image and critical font in theme.liquid<\/p>\n<p>{% if template == &#8216;index&#8217; %}<br \/>\n  {% assign hero_image = section.settings.hero_image %}<br \/>\n  {% if hero_image %}<br \/>\n    {{ hero_image | image_url: width: 1200 }}&#8221;<br \/>\n          imagesrcset=&#8221;https:\/\/dev.to\/mdkaspianfuad\/{{ hero_image | image_url: width: 600 }} 600w, {{ hero_image | image_url: width: 1200 }} 1200w&#8221;<br \/>\n          imagesizes=&#8221;100vw&#8221;><br \/>\n  {% endif %}<br \/>\n{% endif %}<\/p>\n<p>{{ &#8216;your-heading-font.woff2&#8217; | asset_url }}&#8221; crossorigin><\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Wrap third-party preconnects in conditionals so they only fire when the feature is enabled:<\/p>\n<p>{% if settings.enable_reviews %}<\/p>\n<p>{% endif %}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  5. Push dynamic content onto the Section Rendering API<\/p>\n<p>Instead of a full page reload to update one section, fetch just that section&#8217;s HTML.<\/p>\n<p>Slow \u2014 full page reload on filter click:<\/p>\n<p>window.location.href = newUrl;<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Fast \u2014 Section Rendering API:<\/p>\n<p>async function updateCollection(url) {<br \/>\n  const sectionId = &#8216;collection-grid&#8217;;<br \/>\n  const response = await fetch(`${url}?sections=${sectionId}`);<br \/>\n  const data = await response.json();<br \/>\n  document.getElementById(sectionId).innerHTML = data(sectionId);<br \/>\n}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Response size drops from 100KB+ to 5-15KB. Perceived load time drops from 2-3 seconds to 200-400ms.<\/p>\n<p>Good candidates: collection filtering, cart drawer, product recommendations, quick-view modals.<\/p>\n<p>  Before and after numbers<\/p>\n<p>Store<br \/>\nMetric<br \/>\nBefore<br \/>\nAfter<\/p>\n<p>WD Electronics<br \/>\nMobile LCP<br \/>\n9.3s<br \/>\n3.1s<\/p>\n<p>WD Electronics<br \/>\nLighthouse<br \/>\n41<br \/>\n72<\/p>\n<p>WD Electronics<br \/>\nDOM Elements<br \/>\n4,200+<br \/>\n1,100<\/p>\n<p>Factory Direct Blinds<br \/>\nMobile PageSpeed<br \/>\n38<br \/>\n81<\/p>\n<p>Factory Direct Blinds<br \/>\nLCP<br \/>\n22.0s<br \/>\n2.7s<\/p>\n<p>Factory Direct Blinds<br \/>\nLiquid Render<br \/>\n840ms<br \/>\n65ms<\/p>\n<p>Both stores saw measurable conversion improvements within 30 days of deploying the speed fixes.<\/p>\n<p>  How to verify in 5 minutes<\/p>\n<p>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.<br \/>\nOpen PageSpeed Insights on Mobile. Read Field Data first \u2014 that is real Chrome users, the metric Google ranks on.<br \/>\nCheck Search Console > Experience > Core Web Vitals for which URL groups are flagged Poor.<\/p>\n<p>The full post (with more code and the FAQ section) is at kaspianfuad.com.<\/p>\n<p>If you want a professional audit of your theme&#8217;s Liquid performance, I run a Shopify speed-focused CRO audit that covers every pattern above plus the JS and third-party script layer.<\/p>\n<p><br \/>\n<br \/><a href=\"https:\/\/dev.to\/mdkaspianfuad\/shopify-speed-optimization-with-liquid-code-5-patterns-2b29\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1906,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[676],"tags":[761,765,762,763,764,826,823,760,824,825],"class_list":["post-1905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech-ai","tag-coding","tag-community","tag-development","tag-engineering","tag-inclusive","tag-javascript","tag-shopify","tag-software","tag-webdev","tag-webperf"],"_links":{"self":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/posts\/1905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1905"}],"version-history":[{"count":0,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/posts\/1905\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/media\/1906"}],"wp:attachment":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}