Shopify Liquid: Handle nil section.index in Three ContextsShopify Liquid: Handle nil section.index in Three Contexts
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
In Shopify Liquid, section.index comes back nil in three places, and one of them is easy to miss.
It is nil when a section is rendered statically, in the theme editor, and through the Section Rendering API. So a rule like "lazy load images when section.index is above 3" behaves one way on first page load and another when filters or load more re-render that section.
Check: filter a collection, inspect the new product images and compare their loading attribute with the first load. If your logic depends on position, give it an explicit fallback for nil, or use a section setting instead.
Ahmar's avatar
That Section Rendering API edge case for section.index catches so many people off guard. Guarding against nil before checking the index saves a lot of silent lazy loading bugs.
Ecom Swift's avatar
Yes, and the theme editor one is sneaky, because the preview can load images differently from the live store. A setting for the first banner avoids the guesswork.
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started