Repository navigation
Conversation
|
@Freddie one gap I'd love your take on, the header badge only reads Also flagging just in case: full HTML responses stay private either way because of the CSP nonce, so the cacheable win here is the .data responses (/collections.data is no longer private, /cart.data still is). |
|
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
| <HydrogenAnalyticsProvider> | ||
| <LocaleProvider locale={data.locale}> | ||
| <CartProvider initialData={data.cartData}> | ||
| <CartProvider initialData={cartRoute?.cartData}> |
There was a problem hiding this comment.
non-blocking: Could we bring the Hydrogen example’s CartMain into parity with the React Router template’s loading behavior? It currently shows “Looks like you haven't added anything yet” while the cart is still loading, even when the buyer already has items.
f88b419 to
c576582
Compare
📦 Bundle size report
Unchanged (10)
How this is measuredEach scenario is bundled from the built package with Rolldown (tree-shaken, minified, dynamic imports inlined), then gzipped. Peer dependencies are external. Scenarios live in 🟢 smaller · 🟡 larger (< 5%) · 🔴 larger (≥ 5%) · 🆕 not in base · ❌ failed to bundle · ⚪ unchanged |
1059c85 to
7f977e2
Compare
…rsonalized useFetch runs /api/cart as its own request with its own request context, so neither the cart route's private headers nor the cart handler's mark reach the page that embeds the result. Mark the page's own request context, including for an empty cart.
A change made while the store loads the cart in the browser used to drop the load and build on an empty cart. The change now shows at once, its request waits until the load lands, returns no cart, or fails, and then it replays on top of the loaded cart. A change sent from code with window.Shopify.actions.updateCart is already on the network, so it still ends the load and refreshes.
A storefront may load the cart in the browser. Holding that first read hides the line already at the stock limit, so the test now holds only the change and the contract no longer requires a server-rendered cart outside the cart page.
Reading the cart in the root loader marked every route's response personalized. Only the cart route reads it on the server now, and the root CartProvider seeds from that route's data when it is present. The drawer keeps showing lines during the first load with a note that the rest of the cart is loading.
Outside /cart the cart loads in the browser, so the aside showed the empty-cart message until the first load landed. Show "Loading cart…" instead, and a note under lines added during the load, matching the template drawer.
…s held behind the first load - Publish the loaded cart on its own before held changes show on it, so the analytics tracker diffs the add against the loaded cart instead of null. - Drop a held change that a newer change replaces at once, so it doesn't count as a request in flight (spurious revalidation, hydrate() ignored). - Re-run a revalidation that a newly adopted cart made stale, so revalidating doesn't stay true. - Hold every change behind the add that creates the cart. - Stop naming a replaced placeholder line as pending. - Assert the payload of the held add in the external-change test.
…confirmed lines - Template header: no count or live-region announcement until the first load lands, then announce only changes. The drawer badge waits too. - Template, Hydrogen, SvelteKit and SolidStart cart lines: disable quantity and remove controls for optimistic: lines, which have no server ID yet. - SvelteKit and SolidStart: show lines added during the load with a note instead of the skeleton. - Hydrogen example: move cart/:lines out from under the cart route, so it doesn't run the cart loader before redirecting. - Stock-limit E2E: hold the first cart read until after the click, so the add always starts during the load. - Skills and changeset: fix the Vue personalized-mark rule and the markets root-loader snippet, and document the loading and placeholder rules.
- Publish the analytics baseline only when a cart is adopted, so a load that finds no cart or a change that ends the load never publishes an idle state. - Cancel a revalidation that an adopted cart supersedes instead of rerunning it, unless one was requested since. - Template header: announce a change made during the first load once the load lands. - SolidStart and SvelteKit headers hide the count during the first load; Next.js and Nuxt lines lock their controls while unconfirmed. - Markets skill: use the locale-prefixed cart route ID. - Stock-limit E2E: hold only the product page's own first cart read.
7f977e2 to
233f8b5
Compare
Stacked on #4344. Follows up on the part 2 review comment.
TL;DR: #4344 marks every cart read as personalized, so a root loader that reads the cart makes every route's response private. This moves the template's server cart read to the
/cartroute. Other pages then load the cart in the browser, which exposes a store bug: a change made during that first load built on an empty cart. The store now waits for the first load before sending the change, and replays the change on top of the loaded cart.Before
Measured in Chromium against the template on mock.shop (its default
.env). The template reads the cart only on/cart, the store is unchanged, and the cart already has 3 items. The firstGET /api/cartis slowed by 3s, the add by 2s, and the buyer clicks Add to cart during the load:Cart (1 item)for 2.3s, and the drawer reports the cart as done loading.POSTgoes out right away. The firstGETlands afterwards and is thrown away, so the store fetches the cart a second time.Expected 49, Received 1, whether or not the test holds the first read.Any storefront that loads the cart in the browser has this today. The template doesn't, only because its root loader reads the cart on every page.
After
Same setup:
Cart (1 item)for 2.4s, and the drawer shows the line with "Loading the rest of your cart…" under it.POSTis sent 17ms after the firstGETlands. The badge goes to 4 when the load lands, and there's no second fetch.What this changes
packages/hydrogen/src/core/cart/cart.ts): a change made during the first load shows at once. Its request waits until the load lands, returns no cart, or fails. The load is no longer cancelled by the change, soloadingandreadyPromisekeep describing it.examples/hydrogen:cartHandlers.getmoves from the root loader to the cart route's loader. The rootCartProviderseeds fromuseRouteLoaderData("routes/cart")when that route is rendered, and loads in the browser everywhere else.useServerRenderedCartmarks the page's own request context personalized.useFetch("/api/cart")runs as a separate request, so the cart handler's mark never reached the page.POSTand lets the first cart read through. The contract now requires a server-rendered cart only on the cart page, and says a change made during the first load must build on the loaded cart.Developer impact
Includes a patch changeset for
@shopify/hydrogen. It's a bug fix in the cart store, and no exports change. Thehydrogen-cart-ui,hydrogen-setup, andmigrate-to-hydrogen-2026-10skills now teach reading the cart in the cart route's loader instead of the root loader. The old pattern still works, but it marks every response personalized.Behavior changes worth knowing:
initialDatathat hasn't settled after 30s counts as a failed load, so held changes are sent.window.Shopify.actions.updateCartduring the first load is already on the network. It can't be held, so it still ends the load and refreshes the cart.UX impact
/cart, the HTML no longer contains the cart. The header badge fills in after hydration and the firstGET /api/cart. Without JavaScript, the badge shows no count outside/cart.Cart (1 item)when the cart really has 3). The drawer marks this as loading, but the header and its screen-reader live region don't.afterSubmit, which runs once the add's request lands. During a slow first load, that request waits for the load, so the drawer opens later than it did before.Out of scope
packages/hydrogen/src/core/csp/index.ts:68). The win here is navigation data: on the template dev server,/collections.datawent from private to noCache-Control, and/cart.datastays private./cartstill runs the cart route's loader, so the server reads the cart even though the browser already has it.Risk
cart.tschanges how the store dispatches changes. It deserves the most review attention.How to Test
pnpm install && pnpm build:pkgs.pnpm --filter @shopify/hydrogen-template-react-router exec vite dev --port 4201. The template's default.envruns on mock.shop, so no store credentials are needed.