Request deduplication
When multiple components request the same data, the SDK automatically deduplicates requests:Local storage restoration
Cached results are restored from local storage on back navigation:Cache invalidation
Invalidate cached queries by pattern:Search cache
The prepare endpoint caches expensive operations (embeddings, query expansions):search_id, the server maintains the cache for 15 minutes:
Storage adapters
Cache persistence is pluggable via aStorageAdapter. The SDK ships two adapters and accepts any custom implementation, so the cache survives reloads in the browser, page navigations in SSR frameworks, or process restarts in Node.
| Adapter | Environment | Import |
|---|---|---|
localStorageAdapter(key) | Browser (default) | @commerce-blocks/sdk |
fileStorage(path, fs) | Node.js / build scripts | @commerce-blocks/sdk |
Custom StorageAdapter | Any (sessionStorage, IndexedDB, Redis, etc.) | Bring your own |
Browser (localStorage)
localStorage is used automatically when the SDK runs in a browser. Pass an explicit adapter only if you want to customize the storage key:
localStorageAdapter returns null in environments without window.localStorage (e.g. SSR), so the SDK silently falls back to in-memory caching — safe to use in isomorphic code.
Node.js (file system)
UsefileStorage to persist the cache to disk for build scripts, server-rendered pages, or long-running Node processes:
FileSystem interface (readFileSync, writeFileSync, unlinkSync), so you can substitute a mock in tests or wrap an alternative file system.
Custom adapter
ImplementStorageAdapter to back the cache with anything else — sessionStorage, IndexedDB, Redis, Cloudflare KV, an HTTP service, etc.
Fetch adapter
The SDK calls the globalfetch by default. Provide a custom implementation via the fetch option for SSR runtimes, testing, or environments where fetch is unavailable or blocked.
xhrFetch (browser fallback)
xhrFetch is an XMLHttpRequest-based adapter shipped with the SDK. Use it when ad blockers, browser extensions, or restrictive network policies intercept fetch calls:
Custom fetch
Any function matching theCustomFetch signature can be passed. Common uses:
- SSR / Node: inject a Node-compatible fetch (e.g.
undici,node-fetch) on older runtimes. - Testing: stub responses without intercepting global
fetch. - Instrumentation: add tracing, retries, or auth headers around the request.
fetch returns — your adapter only needs to issue the request and return a Response.
Next steps
Performance
Learn performance optimization techniques.
Best Practices
Review SDK best practices and common pitfalls.