Documentation Index
Fetch the complete documentation index at: https://docs.uselayers.com/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This is the SDK equivalent of Implementing search in Liquid. The same four moving pieces apply — autocomplete, prepare, execute, facets/sorts — but each is a method on a controller instead of a hand-rolledfetch. The SDK handles request deduplication, the search_id lifecycle, abort signals, and reactive state for you.
Use this version when you’ve already installed @commerce-blocks/sdk in your theme. For a no-dependencies version, see the Liquid + Fetch guide.
Prerequisites
- The Layers Shopify app installed and synced.
- The SDK installed with sorts and facets mirrored from metaobjects — see the setup snippet.
- The Storefront Pixel installed so
deviceId/sessionIdare populated automatically.
Controllers used
| Controller | Purpose |
|---|---|
client.suggest() | Typeahead suggestions, including semantic redirects |
client.search() | Prepare + execute the full search request |
state signal and subscribe() shape as the collection controller.
1. Autocomplete in the header
client.suggest() debounces input changes, aborts the previous request, and emits suggestions on its state signal. It returns the same redirect metadata as the Autocomplete API — when a typed query matches a semantic redirect, _meta.redirect.url is populated and you should navigate the shopper directly.
AbortController plumbing.
2. Prepare the search before the results page
prepare() is a first-class method on client.search(). Call it the moment a shopper commits to a search — the SDK kicks off the request, caches the resulting search_id against the query, and execute() automatically reuses it on the next page.
Option A — Prepare on submit. Best when you can intercept the form submit so prepare keeps running during navigation.
3. Execute the search
On the search results page,execute() reuses the prepared search_id automatically — you don’t pass it. The SDK falls back to standard processing if the 15-minute cache has expired or no prepare ran.
4. Render facet and sort controls from Liquid
The control surfaces are identical to the collection-page equivalents — see Rendering facets with the SDK and Rendering sort orders with the SDK. On a search page, filter sort metaobjects by thesearch scope:
Recommended request flow
The end-to-end timeline:Header submit
Shopper hits enter. Call
search.prepare({ query }) (non-blocking) and navigate to /search?q=.... The SDK persists the resulting search_id so the next page can use it.Results page render
Liquid renders the sort dropdown and empty facet groups from metaobjects, including the active sort/filter state from the URL.
Execute
Page JS calls
search.execute({ query, sort, filters, page }). The SDK reuses the prepared search_id automatically. The subscriber renders the grid, facets, and pagination as soon as data lands.Why the SDK over Fetch
| Concern | Fetch version | SDK version |
|---|---|---|
| Debounced autocomplete | DIY debounce + AbortController | client.suggest({ debounce }) |
search_id lifecycle | sessionStorage plumbing across pages | Handled internally |
| Re-renders | DIY DOM updates | Signal subscriber |
| Sticky options | Track manually | Merged across execute() calls |
| Click feedback / attribution | Read attributionToken, POST to feedback endpoint | Storefront Pixel handles standard themes; SDK exposes attributionToken on results for custom flows |
| Errors | Hand try/catch | Structured ClientError with isRetryable() |
Troubleshooting
Suggestions show but redirects don’t fire. The redirect lives atdata._meta.redirect.url on the suggest state. Make sure your subscriber checks for it before rendering the suggestion list.
execute is slow on the results page. Prepare wasn’t called, or more than 15 minutes have passed. The SDK falls back to normal processing — confirm prepare runs before navigation.
Filters don’t apply. Aliases must be registered in filterAliases on the client. URL-friendly keys (color) only resolve to API codes (options.color) if the alias is configured.
See also
- Implementing search in Liquid (Fetch) — the no-dependency equivalent
- Rendering facets with the SDK
- Rendering sort orders with the SDK
- Rendering blocks with the SDK — recommendation blocks anchored to search/collection pages
- Client methods —
client.search()andclient.suggest() - Filtering — filter DSL reference