Overview
This is the SDK equivalent of Rendering facets in Liquid. The pattern is the same: read facet metaobjects in Liquid, then resolve runtime facet values, counts, and ranges from Layers. The SDK’sfacets() controller handles the HTTP request, request deduplication, caching, abort signals, and filter-group transformation for you, and exposes results as reactive state you can subscribe to.
Use this version when you have already installed @commerce-blocks/sdk in your theme. If you are scripting against the storefront API directly with fetch, see the vanilla Liquid + Fetch guide instead.
Prerequisites
- The Layers Shopify app installed and synced.
- Facets configured in the Layers dashboard with Enable as Storefront Facet turned on.
- The SDK installed and initialized in your theme. See SDK installation.
The pattern
1
Read facet metaobjects in Liquid
Render the sidebar shell from metaobjects exactly like the Fetch version. Each
<section> carries the facet code so the SDK results can be matched to the right group.2
Create a facets controller
sdk.facets() returns a reactive controller. Its state updates every time get() runs, and the SDK deduplicates identical in-flight requests and caches results.3
Render values, counts, and ranges
The render code looks at the same
data-facet-code attributes as the Fetch version.4
Execute the initial load
Trigger the first request. The subscriber above renders the sidebar as soon as data lands.
Refreshing counts as filters are applied
CallfacetsController.get({ filterGroup }) whenever the active selection changes. The SDK reuses the same controller, deduplicates in-flight requests, and updates the state. Then pass the same filterGroup to the collection controller so the product grid updates too.
OR within a facet and AND across facets automatically when you use the filter DSL.
Collection + facets together
You can also request facet counts directly on a collection request by passingfacets and retrieveFacetCount:
data.facets and data.facetRanges alongside the product list. For complex pages, using a dedicated sdk.facets() controller keeps product and facet requests independent and easier to manage.
Why the SDK over Fetch
If you have any one of these needs, the SDK saves you the boilerplate. The Fetch version remains useful when you want zero dependencies.
Troubleshooting
data.facets is empty. Confirm the facets you expect are configured as storefront facets in the Layers dashboard. Also confirm you passed the collectionHandle to sdk.facets().
Counts do not change when filters are applied. Make sure you pass the active filterGroup to both the collection controller and the facets controller. The SDK does not watch the DOM. Subscribe to each controller state and re-render on every emit.
data.facetRanges is empty. Pass includeFacetRanges: true and include the numeric facet code in the facets array.