Skip to main content

Capstone — commerce and mutation workflows

Build a storefront whose product data, inventory, pricing, cart mutations, promo rules, and checkout validation make the server-authority vs immediate-UI problem explicit.

Core authority model​

Optimistic UI is a temporary projection around server authority
1Browser intent
2Optimistic projection
3Server mutation
4Validate + authorize
5Canonical result
6UI reconciliation

Do not let optimistic state become a second permanent source of truth.

State categories​

Who owns this commerce value?
Price, stock, availability, promotion eligibilityServer authority
Authenticated cartOften server-owned with client projection/cache
Guest cartMay be local/persisted, with explicit later reconciliation
Form draft before submitForm/local owner
Shareable search/filter/product optionsURL when navigation semantics matter
Permission to mutateServer authorization, never client UI state

Document a hybrid cart deliberately if you use one.

Server and Client Component split​

Keep server-owned reads on the server and interaction in narrow client islands
ProductPage — Server
authoritative read + server-friendly structure
Media
server or client as needed
Information
server read-only
VariantPicker
client interaction
AddToCart
client interaction + mutation intent

Only serializable data needed by the client should cross the boundary. Database connections, privileged SDKs, and normal functions stay server-side unless represented through supported Server Function transport.

Add-to-cart mutation​

The server validates product/variant existence, quantity, current stock, authenticated cart relationship, and server-owned pricing.

Never trust client-calculated commerce authority
Client submits
productId · variantId · quantity
Trusted calculation
stock · price · permissions · promotion
Canonical cart
versioned/authoritative state

Model Action state explicitly​

type CartActionState =
| { status: 'idle' }
| { status: 'error'; message: string }
| { status: 'success'; cartVersion: string };

Prefer mutually exclusive states over unrelated loading/error/success booleans that permit contradictions.

Optimistic quantity updates​

Optimistic projection must converge or roll back
Canonical quantity 1
server-confirmed
Projected quantity 2
immediate UI
2 or rollback to 1
reconcile truth

Simulate inventory changing between render and mutation to prove that client state and TypeScript do not enforce real stock truth.

Concurrent mutation challenge​

Test rapid updates such as 1 → 2 → 3 → 4.

How are concurrent cart mutations coordinated?
Requests are serializedDocument queue/order semantics
Requests may complete out of orderUse versions/request ordering and canonical reconciliation
Rapid UI changes can be coalescedDefine debounce/coalescing semantics deliberately
Unknown framework ordering behaviorDo not assume; test and document it

Promo code workflow​

The client submits a code; the server decides existence, expiry, eligibility, combinability, and actual discount.

The browser expresses intent; the server computes trusted business value
SUMMER20
untrusted intent
Promotion service
eligibility + discount rules
UI feedback
human-readable outcome

Checkout form architecture​

Build shipping/contact/delivery/notes with visible labels, runtime server validation, field errors, an error summary, pending state, and deliberate focus movement after failure.

1Edit draft
2Client UX validation
3Submit
4Server trust validation
5Accessible error or success

If the framework supports progressive enhancement, test behavior before hydration and identify which optimistic enhancements require client JavaScript.

Suspense and failure architecture​

Keep the store shell stable while independent regions reveal
Store shell
navigation + primary context
Product info
primary data
Recommendations
optional boundary
Reviews
independent boundary
Cart summary
mutation-aware boundary

Classify product-not-found routing errors, optional recommendation failure, mutation feedback, unexpected render errors, and whole-app initialization failures separately.

Cache layers are not interchangeable​

Name each cache and its authority/lifetime
CDN/page
response/static layer
Server request/render
request/render lifetime
Application data
domain freshness/invalidation
Client data library
browser server-state cache
HTTP browser
protocol cache
React cache()
render-lifetime RSC memoization, not durable Redis-equivalent storage

Mutation invalidation and revalidation belong to the data/framework architecture, not optimistic UI alone.

Security review​

Threat-model client-submitted prices/IDs, authorization, promo abuse, duplicate checkout, cross-user cart access, CSRF/framework transport, XSS in product content, unsafe redirects, logging, and third-party payment/provider boundaries.

Testing portfolio​

Domain/unit
pricing/promo validation adapters · reducers · parsers
Integration
forms · pending · optimistic success/rollback · cache refresh · a11y
E2E
product → cart → checkout · stale inventory · unauthorized/duplicate mutation

A strong commerce capstone shows that immediate UX, server authority, accessibility, caching, and failure recovery can coexist without duplicating truth.