TanStack Start wrapper for the CommerceEngine Storefront SDK. Provides cookie-backed session management, server function support, and pre-rendering — all wired automatically throughDocumentation Index
Fetch the complete documentation index at: https://www.commercengine.io/docs/llms.txt
Use this file to discover all available pages before exploring further.
@commercengine/storefront/tanstack-start.
NPM Package
@commercengine/storefront
TanStack Start integration for Commerce Engine Storefront SDK. Import from
@commercengine/storefront/tanstack-start.Installation
Quick Start
Environment Variables
Create a
.env file in your project root:.env
TanStack Start uses Vite, so client-exposed variables must use the
VITE_ prefix.Create Storefront Config
Create a shared config file that both the client and server storefronts will import:
lib/storefront.ts
storefrontConfig is exported separately so the server storefront can reuse the same configuration without duplicating values.Create Server Storefront
Create a server-only module using the
.server.ts convention. TanStack Start tree-shakes this out of the client bundle automatically.lib/storefront.server.ts
Create StorefrontBootstrap Component
The bootstrap component establishes the client session on first visit. It is a no-op when cookies already exist from a returning user.
components/storefront-bootstrap.tsx
Accessor Rules
| Context | What to Use | Import From |
|---|---|---|
| Client-side public reads (catalog, categories) | storefront.publicStorefront() | lib/storefront |
| Client-side session flows (cart, wishlist, account) | storefront.clientStorefront() | lib/storefront |
| Client bootstrap | storefront.bootstrap() | lib/storefront |
| Route loaders / pre-rendering | storefront.publicStorefront() | lib/storefront |
| Server functions (public reads) | storefront.publicStorefront() | lib/storefront |
| Server functions (session-bound) | serverStorefront() | lib/storefront.server |
Pre-render catalog pages with route loaders for SEO and fast initial loads. Once the app is hydrated, client-side fetching is equally fast and much less complex — use it for all subsequent navigation and interactions. Server functions are optional; the SDK talks to a public API with no secrets to protect.
Key Patterns
Route Loaders (Pre-rendering & SEO)
Use route loaders withpublicStorefront() to pre-render catalog pages for SEO and fast initial loads:
routes/index.tsx
Client-Side Fetching (After Hydration)
Once the app is hydrated, client-side fetching is equally fast and much less complex. Use React Query with the SDK directly — no server functions needed:lib/hooks.ts
Server Functions (Optional)
Server functions are not required for most storefront reads. Use them if you want to run logic at the edge or need specific server-side behavior. Keep them thin:lib/server-fns/catalog.ts
serverStorefront():
lib/server-fns/wishlist.ts
Head Metadata / SEO
Use thehead property in route definitions with loader data:
routes/product/$slug.tsx
Pre-Rendering
Enable pre-rendering in your Vite config. Pre-rendered routes automatically usepublicStorefront() from route loaders.
vite.config.ts
Exclude dynamic routes (search, user-specific pages) from pre-rendering via the
filter option. Only public, cacheable pages should be pre-rendered.Hosted Checkout Integration
If you use Commerce Engine’s hosted checkout, wire token synchronization between the storefront SDK and the checkout iframe.Step 1: Add onTokensUpdated to Config
lib/storefront.ts
Step 2: Initialize Checkout in Bootstrap
components/storefront-bootstrap.tsx
Cloudflare Workers
Cloudflare Workers (workerd runtime) does not send aUser-Agent header by default. The Commerce Engine API requires one. Patch global fetch at the top of your storefront config:
lib/storefront.ts
This patch is only needed when deploying to Cloudflare Workers. Node.js and other runtimes send
User-Agent automatically.Common Pitfalls
| Level | Issue | Solution |
|---|---|---|
| CRITICAL | Importing storefront.server.ts in client code | Server storefront must only be imported in server functions. Use the .server.ts file convention so TanStack Start tree-shakes it out. |
| CRITICAL | Using publicStorefront() for session-bound operations | Use serverStorefront() from lib/storefront.server for auth, cart, and wishlist operations. |
| HIGH | Missing bootstrap in root layout | Mount StorefrontBootstrap in __root.tsx. Without it, first-visit users will not have a session. |
| HIGH | Using clientStorefront() inside createServerFn | Server functions run on the server. Use publicStorefront() for public reads or serverStorefront() for session-bound calls. |
| MEDIUM | Not excluding dynamic routes from pre-rendering | Add a filter in the prerender config to skip search, user-specific, and API routes. |
| MEDIUM | Missing User-Agent on Cloudflare Workers | Patch global fetch to inject a User-Agent header (see above). |
Best Practices
Shared Config
Export
storefrontConfig from lib/storefront.ts and import it in lib/storefront.server.ts. Keep configuration in one place.Server Boundaries
Use the
.server.ts file convention for session-bound code. TanStack Start enforces the boundary at build time.Public vs Session
Use
publicStorefront() for catalog reads and serverStorefront() for user-scoped mutations. Never use clientStorefront() on the server.React Query
Wrap server functions with React Query hooks for client-side caching, refetching, and optimistic updates.
Pre-Rendering
Enable
prerender in vite.config.ts for static catalog pages. Exclude search and user-specific routes with the filter option.Error Handling
Always check the
error property in SDK responses. Throw inside server function handlers so React Query can surface errors to the UI.Cross-References
SDK Client Overview
Core SDK client patterns and architecture
Token Management
How cookie-backed sessions work across client and server
Hosted Checkout (React)
React integration for Commerce Engine Checkout
Authentication Guide
Complete authentication patterns for storefront implementations