Skip to main content

Overview

This guide is the SDK equivalent of Rendering banners in Liquid. The browse payload, layout selection, and grid math are identical. With @commerce-blocks/sdk, sdk.collection() makes the HTTP call, deduplicates requests, caches the response, and exposes data.banners on reactive state you can subscribe to. Use this version when you have already installed @commerce-blocks/sdk in your theme. For a no-dependencies version, see the Liquid + Fetch guide. Hero banners are horizontal banners spanning the entire product grid above the first product row. They are not restricted to inline tile sizes. Inline banners can span one or more columns, bounded by the grid rather than a fixed 1×1 or 2×2 preset. Inline sizes are grid-cell spans, not image dimensions in pixels. The editor can resize inline banners across columns and up to four rows.

Prerequisites

  • The Layers Shopify app installed and the theme app extension enabled in Online Store → Themes → Customize → App embeds.
  • The SDK installed and initialized in your theme. See SDK installation.
  • At least one merchandising rule with banners attached. See Add a banner to a rule.

The banner payload

Banners ship as a top-level, pre-sorted array on the collection controller’s data. The API selects at most one banner per slot before planning the grid. It checks enabled conditional banners in their saved order and returns the first whose contextual conditions match. If none match, it returns the slot’s enabled default if the default’s own conditions match. Empty slots and slots with no eligible banner return nothing. Your renderer does not need to evaluate conditions or choose between alternatives. Each returned banner has a slot_id identifying its shared position and per-device size. Its id still identifies the selected banner. Keep using banner.id, not slot_id, for data-banner-id so impressions and clicks are attributed to the banner shoppers actually see. Existing single banners work as slots with only a default.
Each banner carries the rule_id of the merchandising rule that produced it. data._meta.appliedRules (a flat array of rule ID strings) still lists every applied rule. Four parts of the banner payload matter for rendering:
  • Per-device media and layout. Pick web_* on desktop and mobile_* on mobile. They’re independent. A hero on web can be an inline tile on mobile.
  • Mode. overtake replaces the product card at position. inject inserts the banner and shifts trailing products by one cell.
  • Link. On the built-in link type, only inject banners can carry a top-level link. Wrap them in an anchor. Render overtake banners as plain media. Custom banner types put their click-through URL inside data instead.
  • Type and data. type is the banner type slug: 'link' for the built-in type, or the slug of one of your custom types. data carries the values of that type’s field schema. Switch renderers on type and read fields out of data.

Step 1: Subscribe to the collection controller

sdk.collection() returns a reactive controller. Subscribe once and read data.products and data.banners from the same payload.
The SDK dedupes identical requests and caches responses in memory for the life of the page. It re-runs the subscriber whenever you call get() with new filter or pagination args.

Step 2: Pick the layout for the current device

A banner only has a usable layout when both the media slot and the layout for the active device are present. Skip banners that don’t have both.
Listen for mql.addEventListener('change', ...) and re-render when the breakpoint flips. Banners can have completely different layouts per device. A 2×2 web tile may be a hero on mobile.

Step 3: Place banners around the products

Hero banners render across the full grid width above the products. Ignore their inline width and height cell spans; your theme controls the hero’s height and image cropping. Inline banners slot into the product grid by position (row-major, 0-indexed). overtake replaces the product at that index; inject splices the banner in and shifts trailing products. The response is already sorted by sort_index, so no extra sort is needed.
Use grid-column: span <width>; grid-row: span <height> (or the --span-x / --span-y properties) on banner cells so 2×2 tiles actually cover four grid cells.

Re-running on filter or pagination changes

Reuse the same collection controller and subscriber, and call get() with new args. Layers recomputes the banner payload on every request, so banners are always in sync with the current filter set.

Custom banner types

The rendering steps on this page work unchanged for the built-in Link type. If you’ve defined custom banner types, each banner also carries a type slug and a data object with the type’s saved field values. Switch renderers on type and read fields out of data. The envelope (media, layout, mode) is the same across all types.
Reference fields (product_reference, collection_reference, variant_reference) arrive pre-resolved to { id, handle }. Unpublished references are dropped from the payload, so you don’t need to guard against dead handles. Retyped or removed fields on the banner type are dropped too. Treat any field on data as optional and fall back cleanly.

Metaobject reference fields

Use metaobject_reference for a single Shopify metaobject entry, or list.metaobject_reference for a list. Each field is bound to one Shopify metaobject definition. That definition must have Storefront access enabled in Shopify. See the setup guide. The browse response resolves each entry to { id, type, handle, display_name, fields }. Unlike product references, id is a Shopify metaobject GID. fields is a key/value object, not the raw Shopify field list. For example, this fragment of a banner’s data shows a single promotion field and a list of badges:
Layers translates fields and the display name for the request’s shopper locale and market. Per field, a market-specific translation wins over a language-wide translation; without either, Layers keeps the source value. The example retains the source color and leaves the nested product reference as a Shopify GID. Resolution is one level deep: your renderer must handle nested references separately. If an entry is draft, deleted, belongs to the wrong definition, or its definition no longer allows Storefront API access, Layers omits a single reference field and filters unavailable entries out of a list. An entirely unavailable list becomes empty. Treat these fields as optional and provide a fallback. Text entered directly into ordinary banner text fields is not translated.

Moast widget content

The built-in Moast Widget type resolves its data.widget field to embed content:
Mount the returned html, or construct the custom element using tag_name, script_src, and ref. Deleted widgets or a disabled Moast integration resolve to null; fall back to the banner’s media when no widget is available.

Fallback rules

Per Banner Injection:
  • A banner without both the device’s media slot and layout is treated as disabled. layoutForDevice returns null and you skip it.
  • A banner without web_media or mobile_media filled is never enabled by Layers, so it won’t appear in the payload in the first place.
  • Banners with mode: 'overtake' ignore any link value. Only inject banners are clickable.

What the app embed handles for you

The Layers theme app extension does not render banners. Your theme owns that. The embed does handle:
  • Authenticating the browse request (token, cart context, identity).
  • Sending the storefront pixel events that track shopper engagement on the underlying grid.
The storefront pixel also tracks banner_impression and banner_click events automatically for any rendered element whose data-banner-id matches a banner id from the Layers response, as in the examples above. Keep that attribute on your banner markup so these events fire.

Why the SDK over Fetch

Troubleshooting

data.banners is empty. No merchandising rule matched the request, or no slot had an eligible banner. Confirm at least one active rule on this collection has banners attached, both device images are filled, and its banner conditions and schedule allow it to serve. Rule preview shows slot defaults without evaluating contextual conditions; it does not show every conditional alternative. Banners only appear on first load, not after filtering. Your subscriber probably re-renders products but not banners. Re-read data.banners from the same payload on every state update. They ship together. Inline banner is at the wrong cell. position is row-major and 0-indexed. Verify the device’s *_layout.position matches the cell index you expect after flattening the grid to a single array. 2×2 inline tile renders as a 1×1. Your CSS isn’t honoring width/height. Apply grid-column: span <width>; grid-row: span <height> on banner cells.

See also