Basket management provides a consistent way to preserve cart state across the server and client while removing the need to fetch or create a basket on every request. Our React Router middleware stores a snapshot of the current basket as a cookie, enabling the basket ID and item count to persist across page refreshes. Full basket state hydration is performed lazily, which helps keep page loads fast.
Dependencies
Package
Version
Purpose
React Router 7
7.12.0
Middleware and context system.
React Context
Built-in
Basket provider state.
Overview
Here’s an overview of the basket management process.
Middleware initializes context with a cookie snapshot on every request.
Root loader passes the snapshot into the BasketProvider without hydrating the full basket.
Client components hydrate lazily via useBasket() when full basket data is required.
Middleware updates or deletes the cookie based on the final basket state after loaders or actions finish.
1sequenceDiagram2 participant Browser3 box Server4 participant Middleware5 participant Loader6 participant SCAPI7 end89 Browser->>Middleware: Request with basket cookie10 Middleware->>Middleware: Parse snapshot into context11 opt Eager mode only12 Middleware->>SCAPI: getOrCreateBasket(basketId)13 SCAPI-->>Middleware: Full basket payload14 end15 Middleware->>Loader: Continue to loader16 Loader-->>Browser: Return basket snapshot for provider17 Middleware->>Middleware: Update cookie snapshot on response
The server-side middleware is the source of truth for basket state and cookie management.
Configuration Options
createBasketMiddleware accepts these optional settings.
mode: 'lazy' (default) or 'eager' hydration.
cookieName: Override the basket cookie name (default __sfdc_basket).
cookieDurationRegistered: Cookie duration (TTL) in ms for registered shoppers.
cookieDurationGuest: Cookie duration (TTL) in ms for guest shoppers.
currency: Currency used when creating a basket.
calculateBasketSnapshot: Add custom fields to the cookie snapshot.
The cookie duration (TTL) default values are set to the same default values for your ECOM instance. If you have customized those values, you’ll want to ensure that you are properly configuring these values to be in alignment.
Note
Context value:
basketResourceContext: snapshot, basket, hydration state, and error.
Modes:
lazy (default): Only the snapshot is set. Full basket is fetched on demand.
eager: Middleware hydrates basket before calling next().
Cookie snapshot characteristics:
The cookie snapshot:
Is stored in __sfdc_basket.
Includes basketId, totalItemCount, and uniqueProductCount.
Contains optional custom fields via calculateBasketSnapshot.
Here’s a minimal cookie snapshot configuration for the default lazy mode.
BasketProvider keeps basket data in React context for the client. It receives the snapshot from the root loader, exposes lazy hydration, and updates through hooks.
Provider Configuration
BasketProvider accepts:
snapshot: Cookie-safe basket snapshot for initial render.
basket: Full basket payload (optional, for eager client hydration).
Provider State Shape
The provider stores:
snapshot: Cookie-safe basket summary.
current: Full basket payload (when hydrated).
hydrated: Whether hydration has been attempted.
error: Hydration error info (if any).
The root loader intentionally passes only basketSnapshot to avoid eager basket creation.
Use these hooks in client components after mutations or checkout completion. Common use cases include sharing updated basket data across the React app and clearing basket state after order completion. You typically call useBasketUpdater after basket actions, such as adding or removing items, or applying or removing promotions. Updates aren’t automatically inferred from route loaders because of the default lazy behavior.
1import{useBasketUpdater, useBasketReset}from "@/providers/basket";23const setBasket = useBasketUpdater();4const resetBasket = useBasketReset();56// After a successful mutation7setBasket(updatedBasket);89// On checkout completion10resetBasket();
Advanced Configuration
Use these options only when needed.
Eager hydration: Set mode: 'eager' if a route always needs a full basket.
Custom snapshot fields: Add metadata (for example, pickup flags) via calculateBasketSnapshot.
Force refresh: Call getBasket(context, { ensureBasket: true }) to hydrate server-side before action logic that requires a full basket.