Skip to main content
TanStack Start wrapper for the CommerceEngine Storefront SDK. Provides cookie-backed session management, server function support, and pre-rendering — all wired automatically through @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

1

Environment Variables

Create a .env file in your project root:
.env
TanStack Start uses Vite, so client-exposed variables must use the VITE_ prefix.
2

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.
3

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
The @commercengine/storefront/tanstack-start/server import includes a server-only guard. Never import storefront.server.ts from client code.
4

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
5

Wire Into __root.tsx

Mount StorefrontBootstrap in the TanStack Router root so it runs on every page:
routes/__root.tsx
bootstrap() is deduped internally — concurrent calls resolve to a single operation. You do not need to guard against multiple invocations.

Accessor Rules

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 with publicStorefront() 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
Use publicStorefront() for catalog reads (no session needed). Use clientStorefront() for session-bound operations like wishlist, cart, and account. The session-aware overloads resolve user_id automatically.

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
For session-bound server functions (e.g., server-side wishlist mutations), use serverStorefront():
lib/server-fns/wishlist.ts
Do not manually call sdk.getUserId() or pass user_id for wishlist and cart methods. The session-aware overloads resolve the user automatically.

Head Metadata / SEO

Use the head property in route definitions with loader data:
routes/product/$slug.tsx

Pre-Rendering

Enable pre-rendering in your Vite config. Pre-rendered routes automatically use publicStorefront() 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
Install @commercengine/checkout separately if you use hosted checkout. The authMode: "provided" setting tells checkout to use your storefront SDK tokens instead of managing its own.

Cloudflare Workers

Cloudflare Workers (workerd runtime) does not send a User-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

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