Skip to main content
The @commercengine/storefront/astro package provides an Astro-specific wrapper around the core Storefront SDK. It gives you three accessors — publicStorefront(), serverStorefront(), and clientStorefront() — with automatic cookie-backed token management for SSR and hybrid rendering.

NPM Package

@commercengine/storefront

Astro integration for Commerce Engine Storefront SDK. Import from @commercengine/storefront/astro.

Installation

Quick Start

1

Set Environment Variables

Add your store credentials to .env:
.env
Astro uses the PUBLIC_ prefix for client-exposed env vars (accessed via import.meta.env).
2

Create the Storefront Config

A shared config imported by both client and server storefronts:
src/lib/storefront-config.ts
3

Create the Client Storefront

src/lib/storefront.ts
4

Create the Server Storefront

The server entry is isolated from the client entry to prevent server code from leaking into browser bundles.
src/lib/server-storefront.ts
Never import server-storefront.ts in client scripts or framework islands. Server storefront must only be used in .astro frontmatter, API routes, or middleware.
5

Bootstrap in Root Layout

src/layouts/Layout.astro
The astro:page-load listener is important because Astro’s ClientRouter runs bundled scripts once and then swaps pages on later navigations. Listening for this event ensures bootstrap runs after each navigation.

Accessor Rules

Use the right accessor for each context:
serverStorefront() requires passing Astro.cookies (or context.cookies) because Astro has no global request context. This call is synchronous — no await needed. clientStorefront() throws if called on the server.

Key Patterns

Public Reads in Frontmatter

Session-Aware Server Page

src/pages/account.astro

API Routes (Session-Bound)

src/pages/api/wishlist.ts

Middleware

Use Astro middleware for auth guards and injecting session data into context.locals:
src/middleware.ts

Client-Side Fetching (After Hydration)

Once the app is hydrated, use the client accessors directly in scripts or framework islands:

Hosted Checkout + Astro

Astro does not need a dedicated checkout binding — use the root @commercengine/checkout helpers. If you use Commerce Engine Hosted Checkout, follow this pattern to keep the storefront SDK session and checkout in sync.
1

Add onTokensUpdated to Storefront Config

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

Bootstrap + Init Checkout in Root Layout

src/layouts/Layout.astro
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.

Common Pitfalls

Best Practices

Shared Config

Export storefrontConfig from storefront-config.ts and import it in both client and server storefront modules. Keep configuration in one place.

Server Boundaries

Keep server-storefront.ts out of client scripts and islands. Only import it in .astro frontmatter, API routes, and middleware.

Public vs Session

Use publicStorefront() for catalog reads in frontmatter and serverStorefront(cookies) for user-scoped data in SSR pages. Never use clientStorefront() on the server.

Middleware for Auth

Use Astro middleware with serverStorefront(context.cookies) for route protection. Inject user data into context.locals for downstream pages.

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

Hosted checkout integration guides for all frameworks.