@commercengine/storefront/sveltekit package provides a SvelteKit-specific wrapper around the core Storefront SDK. It gives you three accessors — publicStorefront(), serverStorefront(), and clientStorefront() — with automatic cookie-backed token management across server and client.
NPM Package
@commercengine/storefront
SvelteKit integration for Commerce Engine Storefront SDK. Import from
@commercengine/storefront/sveltekit.Installation
Quick Start
Set Environment Variables
Add your store credentials to
.env:.env
SvelteKit uses the
PUBLIC_ prefix for client-exposed env vars (accessed via $env/static/public).Create the Storefront Config
A shared config imported by both client and server storefronts:
src/lib/storefront-config.ts
Create the Server Storefront
Place this under
$lib/server/ — SvelteKit enforces that files in this directory are never imported into client code (a build error is thrown if client code imports from it).src/lib/server/storefront.ts
Accessor Rules
Use the right accessor for each context:| Context | Accessor | Import From |
|---|---|---|
| Svelte component — public reads | storefront.publicStorefront() | $lib/storefront |
| Svelte component — session flows | storefront.clientStorefront() | $lib/storefront |
| Client bootstrap | storefront.bootstrap() | $lib/storefront |
Universal load (+page.ts, +layout.ts) | storefront.publicStorefront() | $lib/storefront |
Server load (+page.server.ts, +layout.server.ts) | serverStorefront.serverStorefront(cookies) | $lib/server/storefront |
Server hooks (hooks.server.ts) | serverStorefront.serverStorefront(event.cookies) | $lib/server/storefront |
| Form actions | serverStorefront.serverStorefront(cookies) | $lib/server/storefront |
API routes (+server.ts) | serverStorefront.serverStorefront(cookies) | $lib/server/storefront |
serverStorefront() requires passing cookies (from event.cookies or destructured from the load/action context) because SvelteKit has no global request context. This call is synchronous — no await needed. clientStorefront() throws if called on the server.Key Patterns
Public Reads (Universal Load)
For public catalog data that doesn’t need a session, usepublicStorefront() in universal load functions. These run on both server and client:
src/routes/+page.ts
Session-Aware Server Load
src/routes/account/+page.server.ts
Layout Server Load
src/routes/+layout.server.ts
Form Actions (Mutations)
SvelteKit form actions can read and write cookies, making them ideal for cart and auth mutations:src/routes/cart/+page.server.ts
API Routes
src/routes/api/wishlist/+server.ts
Server Hooks
Use hooks for auth guards and injecting session data intoevent.locals:
src/hooks.server.ts
event.locals.userId type-safe, declare it in app.d.ts:
src/app.d.ts
Client-Side Fetching (After Hydration)
In Svelte components, use the client accessors directly:Static Prerendering
For prerendered pages, usepublicStorefront() in a universal load function:
src/routes/products/[slug]/+page.ts
publicStorefront() never creates sessions, never reads/writes cookies, and is safe for prerender and build-time contexts.Hosted Checkout + SvelteKit
If you use Commerce Engine Hosted Checkout, follow this pattern to keep the storefront SDK session and checkout session 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
Bootstrap + Init Checkout in Root Layout
Call both
storefront.bootstrap() and initCheckout() in the root layout. The checkout is initialized with authMode: "provided" so the storefront SDK owns the session.src/routes/+layout.svelte
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 $lib/server/storefront in a .svelte component | Server storefront must only be used in +page.server.ts, +layout.server.ts, hooks.server.ts, or +server.ts |
| HIGH | Missing bootstrap in root layout | Add onMount bootstrap in +layout.svelte |
| HIGH | Forgetting to pass cookies to serverStorefront() | Always pass cookies from the load/action/hook context — there is no global cookie access |
| HIGH | Using @commercengine/ssr-utils directly | Use the first-party wrapper @commercengine/storefront/sveltekit instead |
| MEDIUM | Using server load when universal load suffices | Use +page.ts (universal) for public catalog reads — they work on both server and client |
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
Place server storefront in
$lib/server/ — SvelteKit enforces this boundary at build time with a compile error if client code imports from it.Universal vs Server Load
Use universal
+page.ts with publicStorefront() for public catalog reads. Use +page.server.ts with serverStorefront(cookies) only when you need session data.Form Actions for Mutations
Auth, cart updates, and order creation should go through form actions using
serverStorefront(cookies) so cookies are read and written correctly.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 (Svelte)
Svelte integration for Commerce Engine Checkout.