Skip to main content
This guide covers best practices for SDK setup, controller lifecycle management, and common mistakes to avoid.

SDK initialization

Create the SDK once

Create a single SDK instance and share it across your application. Creating multiple instances wastes resources and breaks caching.

Required configuration

storefrontAccessToken and trackingToken are required. All other options are optional.
sorts and facets arrays are no longer passed to createSDK(). Fetch them at runtime with sdk.sortOrders() and sdk.facets().

Controller lifecycle

Always dispose controllers

Controllers maintain subscriptions and state. Dispose them when components unmount to prevent memory leaks and abort stale requests.

Use get() for shallow updates

In v3, query parameters are set when the controller is created and shallow-merged by get(). Use get() for pagination, sort, and filter changes.
Do not create a new controller for every small update if the same query context can be reused. Create a new controller only when the core query changes (for example, a new search term).

Create new controllers for new search queries

A search controller is created for a specific query. To search for a different term, create a new controller.

Filter handling

Use the filter DSL for complex filters

For explicit control, use the filter DSL instead of hand-writing filter objects.
Replace your-attribute and your-value with the attribute codes configured in your Layers dashboard.

Fetch facets and sort orders at runtime

In v3, facets and sort orders are not configured at SDK init. Fetch them at runtime.

Error handling

Handle errors gracefully

Always check for errors in the Result object:

Prepare failures are non-fatal

If search.prepare() fails, search.get() still works, but it may take longer because the query is prepared on demand.

Caching

Use storage for cache persistence

The SDK caches data in memory by default. For cache persistence across page loads, pass a storage backend.

Next steps

Search patterns

Learn advanced search patterns including prepare/get flows.

Caching

Understand SDK caching behavior and optimization.