Repository navigation
feat(hydrogen): collect marketing consent in the account sheet's sign-in form - #4401
fredericoo wants to merge 2 commits into
Conversation
…n route The login route passes marketing_consent to Shopify's authorize URL, and the widget data route reads the shop's two marketing consent settings from the unstable Storefront API so the sign-in form can show the checkbox.
Replaces the client copy and its args registry with an internal option on graphql(), stops logging aborts as query failures, and documents the cost of depending on unstable.
📦 Bundle size report
Unchanged (11)
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 |
|
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
| const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]); | ||
| const CACHE_ENABLED_GRAPHQL_CLIENTS = new WeakSet<StorefrontClient["graphql"]>(); | ||
| const SHOULD_CACHE_RESULT = Symbol(); | ||
| const API_VERSION = Symbol(); |
There was a problem hiding this comment.
blocking: this symbol doesn't survive the dev build, so in development the consent query goes to the bundled version instead of unstable.
Under the development export condition, @shopify/hydrogen resolves to dist/development/core/development.mjs, which pulls in its own copy of client.mjs (dist/development/client/client.mjs). @shopify/hydrogen/customer-account has no development condition, so it always loads dist/client/client.mjs. Each copy creates its own Symbol():
- merchant creates the client from
@shopify/hydrogen(dev copy) widget-data.mjscallswithStorefrontApiVersionfrom the prod copy and sets the prodAPI_VERSIONkey- the dev
graphql()readsopts[API_VERSION]with its own symbol, getsundefined, falls back toapiUrl - the query hits
2026-10, the fields don't exist, every widget-data request logs a warning and the checkbox never shows
I built the package and called queryAccountWidgetShop with a client from the package entry:
node probe.mjs -> [".../api/2026-10/...", ".../api/unstable/..."]
node --conditions=development probe.mjs -> [".../api/2026-10/...", ".../api/2026-10/..."]
Vite resolves the development condition in dev IIRC, so nobody can test this locally. The session tests import from source, so they can't catch it.
Symbol.for("@shopify/hydrogen/storefront-api-version") would make both copies agree. If I'm not mistaken SHOULD_CACHE_RESULT and CACHE_ENABLED_GRAPHQL_CLIENTS have the same split already, but that's out of scope here.
| ); | ||
| // The response waits for both requests, so a slow marketing consent request delays the account | ||
| // sheet until the route's deadline. | ||
| const [result, marketingConsent] = await Promise.all([ |
There was a problem hiding this comment.
blocking: a slow unstable request makes the cart sync fail.
The consent query shares the route's 8s deadline, and the client's own timeout is 30s, so the deadline always fires first. Because Promise.all waits on it:
unstablehangsawait shopRequestinhandleWidgetDataRoutedoesn't resolve until the deadline abortssignalcommitSessionWithCartSync(..., signal)runs with an already-aborted signalupdateBuyerIdentitythrows before sending anything, sosyncCartBuyerIdentitylogs and returnsfalse
After a token refresh the cart never gets attached to the customer. After a 401 the detach fails, so the cart cookie gets expired and the visitor loses their cart. Before this PR only the cached main shop query could do that - now an API version with no stability guarantee can, on every request.
Repro: the existing "attaches the cart to the customer after a token refresh" test, with the unstable fetch hanging until abort and vi.advanceTimersByTimeAsync(8_000). It returns 200 with cart mutations []. Without the hang you get the attach mutation.
Let's give the consent query its own short budget so it can't use up the route's deadline:
const consentSignal = AbortSignal.any([signal, AbortSignal.timeout(MARKETING_CONSENT_TIMEOUT_IN_MS)]);and add the refresh and 401 cases above as regression tests.
| }; | ||
| } catch (error) { | ||
| // An abort is the visitor leaving or the route's deadline, not a failed query. | ||
| if (!signal.aborted) log.warn("account widget marketing consent query failed", { error }); |
There was a problem hiding this comment.
non-blocking: signal.aborted is also true when the route's own 8s deadline fires, so a slow unstable request gives an 8s account sheet and zero logs. That looks the same as the visitor leaving.
The deadline aborts with a CustomerAccountTimeoutError, so we can tell the two apart:
if (!signal.aborted || signal.reason instanceof CustomerAccountTimeoutError) {
log.warn("account widget marketing consent query failed", { error });
}If the consent query gets its own timeout (comment above), that timeout firing should warn too. The deadline test at session.test.ts asserts logger.warn is not called, so that would need flipping.
TL;DR: The
<shopify-account>sign-in form can now collect marketing consent on Hydrogen storefronts. The widget data route returns the shop's two marketing consent settings, and the login route forwards the customer's choice to Shopify.Stacked on #4399, which added
GET /account/widget-datawith both settings hard-coded tofalsebecause the login route dropped the choice.When
marketingConsentSigninEnabledis true, the component shows a checkbox and appendsmarketing_consent=true|falseto itssign-in-url. Shopify's authorize endpoint accepts that param and subscribes a consenting customer to email marketing, but only when the shop collects marketing consent at sign-in. There are no public docs for it; the source isAuthorizeControllerand itsLoginPageconcern in Core, which accept onlytrueorfalse.Before
After
What this changes
Login route
/account/loginforwardsmarketing_consentto Shopify's authorize URL when the value istrueorfalse. Any other value is dropped.prepareLoginUrlaccepts the same choice asmarketingConsent?: boolean.Widget data route
Shop.marketingConsentSigninEnabledandShop.marketingConsentPreselectedexist only in theunstableStorefront API version (API changemarketing_consent_signin_web_component_fields). They are not in2026-10or2027-01, and the Customer Account API has no equivalent.unstablefor those two fields, next to the existing shop query. It uses the request's country, because preselection depends on the visitor's country.false, and still responds200. An aborted request (the visitor leaving, or the route's 8-second deadline) returnsfalsewithout a warning.marketingConsentPreselectedisfalsewhenevermarketingConsentSigninEnabledisfalse.Storefront client (internal)
unstable.client.tsgets an internalwithStorefrontApiVersion(options, apiVersion)helper that sends onegraphql()call to another version. It is not exported from the package.docs/tech-debt.mdrecorded a per-query version override as rejected for the public API. It now notes this internal exception.Developer impact
Includes a minor changeset for
@shopify/hydrogen:prepareLoginUrlgains themarketingConsentoption, andAccountWidgetShop's two marketing consent fields widen fromfalsetoboolean. No setup changes are needed; storefronts on #4399 get the checkbox when the shop has the setting on.UX impact
On shops that collect marketing consent at sign-in, the account sheet's sign-in form shows the marketing consent checkbox, pre-checked where the shop's settings allow it for the visitor's country.
Risk
unstable. This version has no stability guarantee. If Shopify renames or removes the fields, deployed storefronts lose the checkbox, and every widget data request still sends the extra request (uncached, because the client doesn't cache results with errors) and logs a warning until Hydrogen is upgraded. There is precedent:useShopifyCookieson the2026-04branch queriesconsentManagementonunstable.unstablerequest delays the account sheet, up to the route's deadline./account/login?marketing_consent=truecan be linked to directly, so a crafted link sends consent without the customer seeing the checkbox. Shopify's authorize URL has the same property.How to Test
/account/widget-datareturnsmarketingConsentSigninEnabled: true./account/loginto Shopify carriesmarketing_consent=true.