feat: add cart refresh API - #3967
Draft
andguy95 wants to merge 2 commits into
Draft
Conversation
refresh() previously no-oped when the store had no cart id. That left a gap for out-of-band mutations that create a cart server-side (e.g. cartCreate writing the cart cookie): the new cart would never reach the client store. Fall back to a full load in that case so refresh discovers the freshly created cart, while keeping same-cart revalidation for carts that already exist. Updates the changeset, cart-ui skill, and tests accordingly.
andguy95
marked this pull request as draft
August 21, 2026 23:22
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR: Gives developers a way to refresh the client-side cart after something outside Hydrogen changes it — like a third-party app, a direct API call, or a webhook.
Today, if the cart gets changed outside of Hydrogen's built-in cart forms, the client has no idea until the next full page load.
refresh()fixes that.Before
After an out-of-band cart change, the client-side cart is stale. No way to pull the latest state without a page reload.
After
refresh()is fire-and-forget. You can watchstate.revalidatingfor progress andstate.errors.networkfor failures. Existing cart data is preserved if the refresh fails.What this changes
CartStore.refresh()to the core cart store. It waits for any in-flight optimistic mutations to finish, fetches the cart from the configured endpoint, and merges the response — including custom fragment fields like metafields.useCartActions()hook for React and Vue. Both return{ refresh }and throw a clear error if used outside<CartProvider>.useCartActionsand theCartActionstype from@shopify/hydrogen/reactand@shopify/hydrogen/vue.useCartActionsin thecreateCartComponents()factory so typed cart setups get it automatically.hydrogen-cart-uiskill docs and React/Vue references with usage guidance.Developer impact
Includes a minor changeset for
@shopify/hydrogen. New exports only, nothing breaks.Don't call
refresh()after normal Hydrogen cart form submissions — those already sync the store automatically.Out of scope
refresh()returnsvoid, not aPromise. This is intentional — a newerrefresh()call cancels the previous one, so there's no clear value a Promise would resolve to. Watchstate.revalidatinginstead.state.errors.network. Fine for now; a separate error field could be added later without breaking anything.Risk
Low. The refresh reuses the store's existing revalidation pipeline — same concurrency guards, same abort handling, same staleness checks. No new state machine paths. Calling
refresh()during an in-flight mutation is safe; it queues up and runs after.