@commercengine/storefront/nextjs package provides a Next.js-specific wrapper around the core Storefront SDK. It gives you three accessors — publicStorefront(), serverStorefront(), and clientStorefront() — so the right token strategy is used automatically in every rendering context.
NPM Package
@commercengine/storefront
Next.js integration for Commerce Engine Storefront SDK. Import from
@commercengine/storefront/nextjs.Installation
Quick Start
Set Environment Variables
Add your store credentials to
.env.local:.env.local
NEXT_PUBLIC_API_KEY is safe for client-side use — it is scoped to public storefront operations.Create the Storefront Config
lib/storefront.ts
createNextjsStorefront() does not infer credentials from environment variables — you must pass storeId and apiKey explicitly.Create the StorefrontBootstrap Component
A Client Component that establishes the anonymous session on first visit. It renders nothing visible.
components/storefront-bootstrap.tsx
Accessor Rules
Use the right accessor for each rendering context:| Context | Accessor | Why |
|---|---|---|
| Root Layout | storefront.publicStorefront() | Root layouts should stay public — no live user session |
Build time / SSG / generateMetadata | storefront.publicStorefront() | No request context at build time |
| Public Server Component | storefront.publicStorefront() | Public reads that don’t need a session |
| Session-aware Server Component | await storefront.serverStorefront() | Auto-reads cookies via next/headers |
| Server Action / Route Handler | await storefront.serverStorefront() | Can read and write cookies |
| Client Component | storefront.clientStorefront() | Uses the browser-side session client |
serverStorefront() is async — it dynamically imports next/headers and uses React cache() to dedupe within a single request. clientStorefront() throws if called on the server. serverStorefront() throws if called in the browser.Key Patterns
Public Server Component
Fetch catalog data without touching the user session.app/products/page.tsx
Session-Aware Server Component
Read session-scoped data such as account details or wish lists.app/account/page.tsx
Server Actions (Mutations)
Server Actions can read and write cookies, making them ideal for auth and cart mutations.app/actions.ts
Static Site Generation (SSG)
UsepublicStorefront() for generateStaticParams and any build-time rendering — no session is available at build time.
app/products/[slug]/page.tsx
Client Component
UseclientStorefront() for browser-side interactions like add-to-cart buttons.
components/add-to-cart-button.tsx
SEO Metadata
generateMetadata runs at build time or request time without a live session — use publicStorefront().
app/products/[slug]/page.tsx
Session Helpers
The storefront config acceptstokenStorageOptions to control cookie behavior:
| Property | Default | Notes |
|---|---|---|
prefix | "ce_" | Cookie name prefix |
maxAge | 2592000 (30 days) | Cookie max-age in seconds |
path | "/" | Available across all routes |
domain | --- | Defaults to request domain |
secure | auto-detected | true in production (HTTPS) |
sameSite | "lax" | CSRF protection |
Hosted Checkout + Next.js
If you use the Commerce Engine Hosted Checkout, follow this three-step pattern to keep the storefront SDK session and checkout session in sync. No provider wrapper is needed.Add onTokensUpdated to Storefront Config
This callback forwards token updates from the storefront SDK to the checkout iframe.
lib/storefront.ts
Bootstrap + Init Checkout in a Root Client Component
Call both
storefront.bootstrap() and initCheckout() in a single Client Component. The checkout is initialized with authMode: "provided" so the storefront SDK owns the session.components/storefront-bootstrap.tsx
Migration from Deprecated Package
If you are migrating from@commercengine/storefront-sdk-nextjs, use this mapping:
Old (storefront-sdk-nextjs) | New (storefront/nextjs) |
|---|---|
@commercengine/storefront-sdk-nextjs | @commercengine/storefront/nextjs |
createStorefront() | createNextjsStorefront({ storeId, apiKey, ... }) |
storefront.public() | storefront.publicStorefront() |
storefront.session() (client) | storefront.clientStorefront() |
storefront.session(await cookies()) | await storefront.serverStorefront() |
StorefrontSDKInitializer | Custom StorefrontBootstrap component calling storefront.bootstrap() |
| Env var auto-inference | Explicit storeId and apiKey in config |
storefront({ isRootLayout: true }) | storefront.publicStorefront() |
NEXT_BUILD_CACHE_TOKENS env flag | Not needed — use publicStorefront() for build-time reads |
Common Pitfalls
| Level | Issue | Solution |
|---|---|---|
| CRITICAL | Using publicStorefront() for cart, auth, or order flows | Use await serverStorefront() on the server or clientStorefront() on the client |
| CRITICAL | Using clientStorefront() on the server | clientStorefront() throws on the server — use await storefront.serverStorefront() |
| HIGH | Missing StorefrontBootstrap in root layout | Mount a Client Component calling storefront.bootstrap() in the root layout |
| HIGH | Calling serverStorefront() in a Client Component | Client Components must use storefront.clientStorefront() |
| HIGH | Using session accessors in SSG or root layout | Use storefront.publicStorefront() for build-time and root-layout reads |
| MEDIUM | Session-aware data in the root layout | Move it into a nested Server Component and use await storefront.serverStorefront() |
Best Practices
One Config File
Create a single
lib/storefront.ts that calls createNextjsStorefront() and export storefront from it. Import this everywhere.Right Accessor, Right Context
publicStorefront() for public reads, serverStorefront() for session-aware server code, clientStorefront() for browser interactions.Server Actions for Mutations
Auth, cart updates, and order creation should go through Server Actions using
await serverStorefront() so cookies are read and written correctly.Error Handling
Every SDK call returns
{ data, error }. Always check error before using data and implement error boundaries for graceful failures.Cross-References
SDK Installation
Core SDK setup and framework detection guide.
Token Management
Full token lifecycle, cookie hydration, and returning-user flow.
Authentication Guide
OTP, email, and social login patterns for storefronts.
Hosted Checkout
Full hosted checkout integration with the React hook API.