Basket Management
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.
| Package | Version | Purpose |
|---|---|---|
| React Router 7 | 7.12.0 | Middleware and context system. |
| React Context | Built-in | Basket provider state. |
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
BasketProviderwithout 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.
The server-side middleware is the source of truth for basket state and cookie management.
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.
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 callingnext().
Cookie snapshot characteristics: The cookie snapshot:
- Is stored in
__sfdc_basket. - Includes
basketId,totalItemCount, anduniqueProductCount. - Contains optional custom fields via
calculateBasketSnapshot.
Here’s a minimal cookie snapshot configuration for the default lazy mode.
Use these helpers in loaders or actions (server) to interact with the request-scoped basket.
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.
BasketProvider accepts:
snapshot: Cookie-safe basket snapshot for initial render.basket: Full basket payload (optional, for eager client hydration).
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.
useBasket() triggers a client-side fetch when:
- A snapshot exists (has
basketId). - No full basket is yet in context.
The client-side fetch is useful for components, such as the minicart, that render outside a full page loader and still need full basket data.
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.
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.
Example custom snapshot:
| Phase | Behavior | Example |
|---|---|---|
| Initial request | Middleware reads cookie → snapshot only | Cart badge shows count. |
| Full basket data | Loaded when the route or component explicitly needs it. | Cart page loader. |
| Client-side hydration | Happens during basket actions or when the UI needs data. | <CartSheet /> component. |
| Updates and cleanup | Mutations update context/cookie. destroyBasket() clears state. | After checkout completion. |