The cart behavior in Hydrogen 2 + Remix often relates to how Shopify handles cart sessions and optimistic UI updates. Below is a summary of potential reasons and troubleshooting techniques:
Optimistic UI kicks in immediately after CartForm.ACTIONS.LinesAdd, so your cart state temporarily shows the added line and updated quantity.
After the server processes the cart update, your app fetches the real cart state from Shopify.
If your server-side action or Shopify's API returns a cart with totalQuantity: 0, the optimistic state gets replaced by that empty cart, causing prices to show $0 and quantity to flip back.
The cart session is not properly persisted between client and server (Hydrogen relies on cart cookies or session).
Your server-side cart.addLines() call might be using an empty or expired cart ID, causing Shopify to create a new empty cart silently.
The cart context on the server side might be missing or not properly wired, so your cart object in the action is invalid.
( Ensure that the action receives a valid cart from your server-side context )
export async function loader({ request, context }: LoaderFunctionArgs) {
const cart = await getCartFromRequest(request, context);
return json({ cart });
}
Confirm Cart Session is Present
In browser DevTools:
-- Check cookies for cart or similar session identifier
-- Ensure the same cart ID is used between optimistic state and server response
Log Cart ID and Server Cart Object ( Make changes to your action to record what occurs: )
export async function action({ request, context }: ActionFunctionArgs) {
const { cart } = context;
console.log('Cart context on server:', cart);
const formData = await request.formData();
const { action, inputs } = CartForm.getFormInput(formData);
if (action === CartForm.ACTIONS.LinesAdd) {
const result = await cart.addLines(inputs.lines);
console.log('Cart result from server:', result.cart);
return result;
}
}
Search for:
is cart.id
valid?
Is totalQuantity
correct on the server response?
Are prices correct?
useEffect(() => {
refetchCart();
}, [optimisticCart?.totalQuantity]);
(Replace refetchCart with your method for forcing a fresh cart query from server after changes)
Sometimes old cookies or dev server cache cause weird cart behavior. Try:
Clear cookies
Restart dev server
Test in private/incognito window
I hope this is useful! If you need assistance debugging logs or reviewing your server-side context configuration, please let me know.