A shopper adds a jacket to the cart on their phone during lunch, opens the laptop at night, and the jacket is gone, or worse, it is still there at yesterday's price and fails at checkout. Most shopping apps get the catalogue and the UI right. Far fewer get state consistency right: cart, stock and price staying correct across devices, sessions and sales.
This post looks at the patterns that keep that state honest.
1. Make the Cart a Server-Side Resource
Storing the cart only in local storage is fine for a prototype and painful in production. A server-side cart keyed to the user (and to an anonymous session ID before login) gives you:
The same cart on every device
Merge on login (anonymous cart + saved cart)
Abandoned-cart reminders that reflect reality
// merge anonymous cart into user cart on login async function mergeCarts ( userId : string , sessionId : string ) { const [ userCart , guestCart ] = await Promise . all ( [ carts . findByUser ( userId ) , carts .…