Client State & Feature Slices
Separation of server cache from ephemeral UI state, thin route entrypoints, vertical feature slices, and client error boundaries.
Purpose & Scope
This reference governs internal application structure and client-side state management across Storefront, Commerce, and Backoffice. It defines how route entrypoints stay thin, how vertical feature slices mirror backend bounded contexts, and how server cache is strictly separated from ephemeral UI state.
Architectural Principles & Rules
- Thin route entrypoints: Application route files only declare page metadata, loading/error boundaries, and server-side data prefetching, delegating all presentation and interaction logic to domain-aligned feature slices.
- Vertical feature slices: Inside each surface, UI components, filters, forms, and state are grouped by business capability (Catalog, Shopping, Ordering, Reviews) rather than technical file type, mirroring backend service boundaries.
- Strict state separation: Remote domain data lives exclusively in the server-state query cache (hydrated from server prefetching); ephemeral client state (session-derived flags, feature toggles, derived basket badge counts) lives in lightweight atomic state.
- Auth-gated background queries: Customer-specific queries such as basket state are gated on authenticated session status so anonymous visitors never trigger failing unauthorized requests.
- Explicit error & loading boundaries: Every route segment defines declarative loading skeletons and error boundaries so transient failures degrade gracefully within the affected slice without crashing the entire page shell.
Never duplicate backend entities (books, orders, reviews, or basket items) into a manual global client store. Use the centralized query cache so deduplication, SSR hydration, stale-while-revalidate, and invalidation remain consistent.
Patterns & Boundaries
| Concern / Surface | Architectural Approach | Responsibility Boundary | Key Trade-off |
|---|---|---|---|
| Route Entrypoints | Thin server wrappers handling metadata, prefetch hydration, and error/loading boundaries | Route layer owns URL mapping and SSR handoff; feature slices own UI and interactions | Adds one layer of indirection, repaid by clean Server/Client Component boundaries |
| Server State Cache | Centralized query client (per-request on server, singleton in browser) with shared cache keys | Shared query layer owns keys, stale windows, and hydration; feature slices consume hooks | Requires disciplined cache key invalidation on mutations |
| Ephemeral Client State | Minimal atomic state for auth flags, derived basket counts, and feature toggles | Customer shell owns cross-zone session/basket atoms; apps own local feature flags | Contributors must distinguish server state from client-only UI state |
| Conversational AI State | Streaming AG-UI client runtime with declarative generative UI cards (Storefront only) | Storefront manages chat session UI and live announcements; Chat API owns agent state | Real-time token streaming requires dedicated accessibility pacing |
- Basket Mutations: Interactive basket updates invalidate the shared basket query key and update derived badge counts immediately, rolling back cleanly if the backend rejects the change.
- Staff Edits (Backoffice): Resource updates avoid blind optimistic overwrites; instead, they pair
ETagpreconditions with explicit conflict banners so operators can inspect and reload concurrent changes. - Boundary Isolation: Route-level error boundaries capture unexpected rendering errors and offer retry actions without losing top-level navigation chrome.
Quality & Operational Impact
- Predictability: Single-source cache keys prevent hydration mismatches between server-rendered HTML and client-side query caches.
- Performance: Per-request server query clients prevent cross-user data leaks while pre-hydrating initial views for fast first paint.
- Maintainability: Vertical feature slices allow teams to evolve Catalog, Shopping, Ordering, or Reviews UI independently without cross-feature coupling.