Skip to main content
The @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.
@commercengine/storefront-sdk-nextjs is deprecated. See the migration table below if you are upgrading.

Installation

Quick Start

1

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

Create the Storefront Config

lib/storefront.ts
createNextjsStorefront() does not infer credentials from environment variables — you must pass storeId and apiKey explicitly.
3

Create the StorefrontBootstrap Component

A Client Component that establishes the anonymous session on first visit. It renders nothing visible.
components/storefront-bootstrap.tsx
bootstrap() is deduped and idempotent. If session cookies already exist (returning user), it is a no-op.
4

Mount in Root Layout

The root layout stays a Server Component — StorefrontBootstrap is a Client Component child rendered inside it.
app/layout.tsx
Always use publicStorefront() in the root layout. Do not call serverStorefront() or clientStorefront() here — the root layout should not participate in the user session.

Accessor Rules

Use the right accessor for each rendering context:
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)

Use publicStorefront() for generateStaticParams and any build-time rendering — no session is available at build time.
app/products/[slug]/page.tsx

Client Component

Use clientStorefront() 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 accepts tokenStorageOptions to control cookie behavior: Client and server cookie configurations are automatically aligned — you do not need to configure them separately. For the full token lifecycle and returning-user flow, see Token Management.

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

Add onTokensUpdated to Storefront Config

This callback forwards token updates from the storefront SDK to the checkout iframe.
lib/storefront.ts
2

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
3

Mount in Root Layout

app/layout.tsx
Then use useCheckout() in any Client Component — no provider needed:
components/cart-button.tsx

Migration from Deprecated Package

If you are migrating from @commercengine/storefront-sdk-nextjs, use this mapping:

Common Pitfalls

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.