Skip to content

Commit d52afbd

Browse files
committed
fix(shop): restore onSuccess for add-to-cart so new line appears
Add-to-cart can't optimistically render the new line item (onMutate only has the variant ID, not the full product snapshot). Without onSuccess setting the server response, the drawer opened empty until the background refetch landed. Restore onSuccess for add only — the concurrent-flicker risk is minimal here since rapid-fire adds are rare, and the worst case is a brief badge-count wobble.
1 parent 1cc2274 commit d52afbd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/hooks/useCart.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,13 @@ export function useAddToCart() {
9595
qc.setQueryData(CART_QUERY_KEY, ctx.previous)
9696
},
9797

98-
// No onSuccess — don't overwrite the cache with the server response
99-
// while other mutations may be in flight. settleWhenIdle will refetch
100-
// from the server once all mutations have landed.
98+
// Add-to-cart needs onSuccess to populate the new line item in the
99+
// cache — onMutate can only bump totalQuantity since it doesn't have
100+
// the full product data. Unlike remove/update, rapid-fire adds are
101+
// rare, and the worst case is a brief badge-count fluctuation.
102+
onSuccess: (cart) => {
103+
qc.setQueryData(CART_QUERY_KEY, cart)
104+
},
101105

102106
onSettled: () => settleWhenIdle(qc),
103107
})

0 commit comments

Comments
 (0)