@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
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).Create the Storefront Config
A shared config imported by both client and server storefronts:
src/lib/storefront-config.ts
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
Accessor Rules
Use the right accessor for each context:| Context | Accessor | Import From |
|---|---|---|
| Frontmatter — public reads (catalog, categories) | storefront.publicStorefront() | src/lib/storefront |
| Browser scripts / client islands | storefront.clientStorefront() | src/lib/storefront |
| Client bootstrap | storefront.bootstrap() | src/lib/storefront |
SSR pages (.astro with output: "server") | serverStorefront.serverStorefront(Astro.cookies) | src/lib/server-storefront |
API routes (src/pages/api/*.ts) | serverStorefront.serverStorefront(context.cookies) | src/lib/server-storefront |
| Middleware | serverStorefront.serverStorefront(context.cookies) | src/lib/server-storefront |
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 intocontext.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.
Add onTokensUpdated to Storefront Config
This callback forwards token updates from the storefront SDK to the checkout iframe.
src/lib/storefront-config.ts
Common Pitfalls
| Level | Issue | Solution |
|---|---|---|
| CRITICAL | Using publicStorefront() for cart, auth, or order flows | Use serverStorefront(cookies) on the server or clientStorefront() in the browser |
| CRITICAL | Importing server-storefront.ts in client scripts | Server storefront must only be used in .astro frontmatter, API routes, or middleware |
| HIGH | Missing bootstrap in root layout | Add bootstrap script with astro:page-load listener |
| HIGH | Forgetting to pass Astro.cookies to serverStorefront() | Always pass Astro.cookies or context.cookies — there is no global cookie access |
| HIGH | Using @commercengine/ssr-utils directly | Use the first-party wrapper @commercengine/storefront/astro instead |
| MEDIUM | Not listening for astro:page-load in bootstrap | Without it, bootstrap won’t re-run after ClientRouter page swaps |
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.