Repository navigation
fix(merch): report checkout failures and stop the cart hydration mismatch - #20294
Closed
posthog[bot] wants to merge 1 commit into
Closed
posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
…atch The merch checkout button could fail with no message. createCartQuery caught every error and resolved undefined, the caller then read cart.id, and the TypeError became an unhandled rejection that left the button spinning. The cart store also read local storage during the first client render, which does not match the server HTML. React then discarded the server tree and rendered the page again, which reset every piece of component state, including the cart panel flag. - createCartQuery and getCartQuery now share one request helper that rejects on a failed request, on GraphQL errors, and on cart user errors - the checkout handler catches the rejection, clears the loading state, shows a failure message, and captures merch_checkout_failed - the cart store reads local storage in an effect, behind a hasHydrated flag - the cart panel flag moved into the store, so a re-render cannot close it Generated-By: PostHog Desktop Task-Id: e8b68ab3-4315-4b03-9bc8-adc34ba968e5
Contributor
Deploy preview
|
Contributor
Bundle reportTotal JS (gzip)8.66 MiB (+1.2 KiB / +0.0%) Eager graph (modules shipped in each entrypoint's initial chunks)
Largest modules in the
|
| Module | Size |
|---|---|
./src/data/mcp-tools.json |
1163.3 KiB |
css ./node_modules/.pnpm/css-loader@5.2.7_webpack@5.101.3/node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[8].oneOf[1].use[1]!./node_modules/.pnpm/postcss-loader@4.3.0_postcss@8.5.6_webpack@5.101.3/node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[8].oneOf[1].use[2]!./src/styles/global.css |
773.1 KiB |
./src/components/Stickers/Stickers.tsx |
696.4 KiB |
./node_modules/.pnpm/@radix-ui+react-icons@1.3.2_react@18.3.1/node_modules/@radix-ui/react-icons/dist/react-icons.esm.js |
481.4 KiB |
./node_modules/.pnpm/@posthog+brand@0.8.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/x-ray.mjs |
480.8 KiB |
./node_modules/.pnpm/rehype-raw@7.0.0/node_modules/rehype-raw/lib/index.js + 29 modules |
395.1 KiB |
./node_modules/.pnpm/@posthog+brand@0.8.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/im-the-driver.mjs |
385.7 KiB |
./src/hooks/useCustomers.tsx + 55 modules |
372.5 KiB |
./node_modules/.pnpm/@posthog+icons@0.36.6_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@posthog/icons/dist/posthog-icons.es.js |
354.8 KiB |
./node_modules/.pnpm/react-markdown@8.0.7_@types+react@16.14.66_react@18.3.1/node_modules/react-markdown/lib/react-markdown.js + 88 modules |
351.4 KiB |
./src/components/ProductComparisonTable/index.tsx + 126 modules |
305.8 KiB |
./node_modules/.pnpm/cloudinary-core@2.14.0_lodash@4.17.21/node_modules/cloudinary-core/cloudinary-core.js |
281.9 KiB |
./node_modules/.pnpm/@posthog+brand@0.8.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/doll-house.mjs |
281.7 KiB |
./node_modules/.pnpm/@posthog+brand@0.8.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/director.mjs |
275.6 KiB |
./src/components/SearchUI/index.tsx + 87 modules |
273.7 KiB |
Eager-graph budgets are report-only until a baseline is established. Sizes are gzip of public/**/*.js; eager size is webpack module source bytes for the modules actually shipped in the entrypoint's initial chunks (post-tree-shake).
Contributor
|
This PR hasn't seen activity in two weeks! Should it be merged, closed, or further worked on? If you want to keep it open, please remove the |
Contributor
|
This PR was closed due to lack of activity. Feel free to reopen if it's still relevant. |
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.
Changes
Two defects on
/merchmake the Checkout control fail without saying so. This fixes both.Problem
createCartQuerycaught every error and resolvedundefined. The caller then readcart.id, so the failure surfaced as an unhandledTypeErrorand the button kept spinning./merchagain, and every piece of component state went back to its default value — including the cart panel flag, which was plain local state.Changes
errors, and oncartCreate.userErrors(which the mutation did not even ask for before).merch_checkout_failedwith the error text and the cart size. Silent failures become countable ones.hasHydratedflag, so the first client render matches the server HTML.useStateinto the store, so a re-render of the page cannot close the panel.TypeErroruserErrors(for example an invalid merchandise line)checkoutUrl🗺️ PR tour
Stop 1: The Storefront request layer (diff)
createCartQueryandgetCartQueryduplicated the samefetchchain, and each ended in acatchthat hid the error — one returnedundefined, the other rewrapped the error and lost its message. Both now call one helper that rejects.posthog.com/src/lib/shopify.ts
Lines 162 to 209 in fe611cb
The
CartCreateMutationdocument now also asks foruserErrors, andCartCreateReponseinsrc/templates/merch/types.tsdescribes the shape Shopify actually returns (cartCreate.cart, notcartCreateas the cart).createCartQueryused to resolvePromise<CartCreateReponse | void>and now resolvesPromise<Cart>or rejects.Checkout.tsxis the only caller.Stop 2: The checkout handler (diff)
The mutation keeps its shape. The call site now handles the rejection, so the shopper sees a failure instead of a frozen button.
posthog.com/src/templates/merch/Checkout.tsx
Lines 145 to 165 in fe611cb
Two details worth naming:
checkoutUrlused to make the click do nothing. It now throws, so it reaches the same message.The failure message sits above the button, which doubles as the retry.
posthog.com/src/templates/merch/Checkout.tsx
Lines 173 to 180 in fe611cb
Stop 3: The cart store (diff)
skipHydrationstops the store from reading local storage during render.useCartStoreHydrationdoes the read in an effect and reports when it is done.partializenames what survives a reload: the cart contents, not the panel flag and not the hydration flag.posthog.com/src/templates/merch/store.ts
Lines 67 to 105 in fe611cb
fireImmediatelypass ran before any cart id existed. The stale-cart case it guarded is already covered, because thecartItemssubscription below clearscartIdon every change.Stop 4: The cart panel flag (diff)
Collectionreads the panel flag from the store instead ofuseState, and its URL initialization waits for the cart to load. Theelsebranch now sets the flag from the URL in both directions, which keeps the URL authoritative.posthog.com/src/templates/merch/Collection.tsx
Lines 170 to 177 in fe611cb
Stop 5: The other two cart consumers (diff)
HeaderBarrenders the cart count in text, so it was a hydration mismatch of its own; it now triggers the local storage read.useDesktopBadgeshad a bespokemountedflag for the same reason, and uses the shared flag instead.posthog.com/src/hooks/useDesktopBadges.tsx
Lines 10 to 25 in fe611cb
🔍 Reviewer's guide
Testing done
tsc --noEmit -p tsconfig.jsonsrc/(unchanged frommaster)prettier --writeon the 7 changed filessrc/lib/shopify.ts, drove it with a stubfetchover 8 cases: HTTP 500, GraphQLerrors,userErrors, no cart, network throw, success, unknown cart, HTTP 502Checkoutcomponent in jsdom with a failingfetch, clicked the buttonmerch_checkout_failedcapture with the error text andcart_item_countmasterfetchTypeError: Cannot read properties of undefined (reading 'id')renderToStringwith no local storage, thenhydrateRootwith a cart in local storage, countingonRecoverableErrormastershape: 2 recoverable errors (text content mismatch, then the server HTML replaced — #418 and #423). This branch: 0. The cart count and the cart panel still restore.Not tested: the dev server and the Vercel preview.
pnpm startcannot build/merchin this environment — the page comes fromallShopifyProductnodes, and the Shopify credentials are absent from.env.development.Screenshots
Blocker — not supplied. This environment has no browser binary and cannot build
/merch, so the required grid (light and dark, narrow and wide) could not be captured. The PR stays a draft until a maintainer supplies it. One new visual state needs the grid:To reach the state: open the cart with an item in it, block the request to the Shopify Storefront API in devtools, then click Checkout. The message renders above the button.
What to look at
src/templates/merch/store.ts:70—skipHydrationis the highest-blast-radius line here. Every consumer of the cart now shows an empty cart until the first effect runs, andHeaderBaris global, not merch-only. A consumer that reads persisted cart state and does not calluseCartStoreHydrationwould show an empty cart forever. Three consumers exist today and all three call it.src/templates/merch/Collection.tsx:194— the URL initialization now waits forhasHydrated. It is the same component's own hook that sets the flag, so the wait is short, but if that ever stops being true then?product=,?state=cartand?add=deep links stop working. Worth a second pair of eyes on the ordering.partializedroppingisOpen— my judgment call. Keeping the panel flag out of local storage means the URL alone decides whether the panel is open, and it removes a frame where a storedisOpen: truewould open the panel and the URL would then close it. It also means the stored payload loses a key that older code wrote. Push back if you would rather persist it.Checklist
vercel.json— no page movedCreated with PostHog Desktop from this inbox report.