Skip to content

fix(hydrogen): configure Standard Actions after the deferred script runs - #4372

Open
fredericoo wants to merge 8 commits into
mainfrom
fb-standard-actions-deferred-ready
Open

fredericoo wants to merge 8 commits into
mainfrom
fb-standard-actions-deferred-ready

Conversation

@fredericoo

@fredericoo fredericoo commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #4332

TL;DR: The cart store can try to configure Standard Actions after HTML parsing finishes but before standard-actions.js has run. The attempt fails silently and isn't retried, so direct window.Shopify.actions.updateCart calls go to Shopify's default handler instead of the endpoint set with configureCartEndpoint().

This is a smaller alternative to #4337, which fixes the same race with a load/error/timeout state machine.

Before

ShopifyScripts renders standard-actions.js as <script type="module"> with no async, so the browser defers it. The order on page load is:

  1. document.readyState becomes "interactive"
  2. deferred scripts run, including standard-actions.js
  3. DOMContentLoaded fires

getShopifyStandardActions() only waited while readyState === "loading". The app entry is an async module, so on a slow connection it can hydrate between steps 1 and 2. When it does, CartProvider connects the store before window.Shopify.actions exists, setup rejects, and connectCartStore swallows the error.

After

While the document is "interactive" and the actions aren't there yet, Hydrogen waits for DOMContentLoaded or window load, whichever fires first, and then configures as before.

  • DOMContentLoaded is the fast path: it fires right after deferred scripts run.
  • Window load covers the case where DOMContentLoaded already fired. By then the deferred script has either run or failed.
  • If the actions already exist, configuration happens straight away, as it does today.

What this changes

  • One condition in getShopifyStandardActions(), plus cleaning up both listeners with an AbortController.
  • connectCartStore now logs setup failures with log.error instead of swallowing them. Before, this bug left no trace.
  • Error messages and the existing "reject and let the next caller retry" behaviour stay the same. There's no new timeout.

Developer impact

Includes a patch changeset for @shopify/hydrogen. No public API changes. Storefronts without a working Standard Actions script now get a [hydrogen:error:cart] standard actions setup failed log through the default logger.

Out of scope

  • Script tags added to the page after it has loaded. The issue's Node and React Router repros inject the script late, which main doesn't support either. Apps that use ShopifyScripts render the tag in the server HTML.
  • A script that never loads (blocked or failing). Setup still rejects, now at DOMContentLoaded/load rather than straight away.
  • A public way to check or ensure configuration.

Risk

  • If the script is blocked and DOMContentLoaded has already fired, the rejection now waits for window load, which can be slow on pages with lots of images. Cart actions fail in that case anyway, so only the time until the error changes.
  • This assumes every supported framework renders the tag in the initial HTML. A tag inserted from the client behaves like an async script, and neither main nor this PR handles that.

@shopify

shopify Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Oxygen deployed a preview of your fb-standard-actions-deferred-ready branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment November 19, 2026 1:38 PM

Learn more about Hydrogen's GitHub integration.

@fredericoo
fredericoo marked this pull request as ready for review November 12, 2026 20:27
@fredericoo
fredericoo requested a review from a team as a code owner November 12, 2026 20:27
@fredericoo
fredericoo marked this pull request as draft November 12, 2026 20:42
@fredericoo
fredericoo marked this pull request as ready for review November 13, 2026 12:19
Comment thread packages/hydrogen/src/core/cart/cart.ts Resolved
Comment thread packages/hydrogen/src/core/cart/cart.ts Resolved
Comment thread packages/hydrogen/src/core/cart/cart.ts Resolved
@fredericoo
fredericoo force-pushed the fb-standard-actions-deferred-ready branch from 57efba1 to f0fc6e4 Compare November 19, 2026 12:54
@fredericoo
fredericoo enabled auto-merge November 19, 2026 12:54
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

📦 Bundle size report

Status Target Scenario Minified Gzip Δ Minified Δ Gzip
🟡 server entry: @shopify/hydrogen 169.75 kB 53.64 kB +263 B (+0.2%) +78 B (+0.1%)
🟡 client entry: @shopify/hydrogen/react 98.45 kB 31.93 kB +263 B (+0.3%) +44 B (+0.1%)
🟡 client entry: @shopify/hydrogen/vue 98.25 kB 31.75 kB +263 B (+0.3%) +58 B (+0.2%)
🟡 client react cart 48.56 kB 15.15 kB +261 B (+0.5%) +68 B (+0.5%)
🟡 client vanilla cart 37.71 kB 11.66 kB +262 B (+0.7%) +69 B (+0.6%)
Unchanged (10)
Status Target Scenario Minified Gzip Δ Minified Δ Gzip
⚪ server cart handlers 23.85 kB 7.11 kB ±0 ±0
⚪ server customer account 22.91 kB 7.17 kB ±0 ±0
⚪ server entry: @shopify/hydrogen/customer-account 25.41 kB 8.29 kB ±0 ±0
⚪ server predictive search handlers 9.32 kB 3.71 kB ±0 ±0
⚪ server request handling 53.8 kB 17.52 kB ±0 ±0
⚪ client react collection 11.59 kB 3.94 kB ±0 ±0
⚪ client react predictive search 9.39 kB 3.43 kB ±0 ±0
⚪ client react product 14.66 kB 5.23 kB ±0 ±0
⚪ client react shop pay button 10.13 kB 4.45 kB ±0 ±0
⚪ client react shopify scripts 18.66 kB 6.87 kB ±0 ±0
How this is measured

Each scenario is bundled from the built package with Rolldown (tree-shaken, minified, dynamic imports inlined), then gzipped. Peer dependencies are external. Scenarios live in scripts/bundle-size-scenarios.ts.

🟢 smaller · 🟡 larger (< 5%) · 🔴 larger (≥ 5%) · 🆕 not in base · ❌ failed to bundle · ⚪ unchanged

@fredericoo
fredericoo marked this pull request as draft November 19, 2026 12:58
auto-merge was automatically disabled November 19, 2026 12:58

Pull request was converted to draft

…ter DOMContentLoaded

The ready listener runs outside the Promise executor, so a throw from
configureUpdateCartOnce escaped as an uncaught error and left the shared
promise pending forever. Catch it and reject so the next call retries.
Each connect() attached its own log to the shared readiness promise, so
StrictMode remounts and multiple stores logged the same failure several
times. Log from the shared promise's catch instead.
…ogger

Tests that connect a cart store without caring about Standard Actions now
get a minimal window.Shopify.actions stub, so the setup error isn't
produced at all and the real logger stays in place. Tests that exercise a
failed setup inject a test logger and assert the log.
…ard Actions script

Deferred scripts always run before DOMContentLoaded, so by window load the
server-rendered tag has already failed. Drop the resolve-at-load test case,
which covered late-injected tags that are out of scope.
@fredericoo
fredericoo marked this pull request as ready for review November 19, 2026 13:58
@fredericoo
fredericoo requested a review from andguy95 November 19, 2026 13:58

@andguy95 andguy95 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cart store never configures Standard Actions if the script loads after connect; updateCart silently uses the default handler

2 participants