From f3ebb67194a6842b051cdc7341a754891fa5bbd4 Mon Sep 17 00:00:00 2001 From: Hai Bui Date: Wed, 4 Nov 2026 10:17:44 -0800 Subject: [PATCH] fix(theme): keep the dev session cookie on passthrough and cookie-auth proxy responses Only store `_shopify_essential` from requests sent with the dev session's credentials. Storefront API passthrough and cart/checkout/account requests get a cookie that isn't bound to the development theme, which made later renders use the live theme. Co-Authored-By: Claude Opus 5.5 --- .changeset/theme-dev-keep-session-cookie.md | 5 ++ .../utilities/theme-environment/proxy.test.ts | 56 +++++++++++++++++++ .../cli/utilities/theme-environment/proxy.ts | 13 ++++- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 .changeset/theme-dev-keep-session-cookie.md diff --git a/.changeset/theme-dev-keep-session-cookie.md b/.changeset/theme-dev-keep-session-cookie.md new file mode 100644 index 00000000000..7003eddcdae --- /dev/null +++ b/.changeset/theme-dev-keep-session-cookie.md @@ -0,0 +1,5 @@ +--- +'@shopify/theme': patch +--- + +Fix `theme dev` switching to the live theme ("Theme ID mismatch", `null` sections) after Storefront API, cart, checkout, or account requests diff --git a/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts b/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts index a14d82d591e..6b2927fc02f 100644 --- a/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts +++ b/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts @@ -721,6 +721,62 @@ describe('dev proxy', () => { }) }) + describe('proxyStorefrontRequest — dev session cookie', () => { + const responseSettingEssential = () => + new Response('OK', { + headers: [ + ['set-cookie', '_shopify_essential=:ROTATED:; path=/; secure; HttpOnly; SameSite=Lax'], + ['set-cookie', 'cart_currency=USD; path=/; SameSite=Lax'], + ], + }) + + const createSessionCtx = () => + ({ + ...ctx, + type: 'theme', + session: { + storeFqdn: 'my-store.myshopify.com', + sessionCookies: {_shopify_essential: ':DEV-THEME-BOUND:'}, + storefrontToken: 'sfr-devtools-token', + }, + }) as unknown as DevServerContext + + beforeEach(() => { + vi.stubGlobal( + 'fetch', + vi.fn().mockImplementation(async () => responseSettingEssential()), + ) + }) + + afterEach(() => { + vi.unstubAllGlobals() + }) + + test.each([ + ['GET', '/cart.js'], + ['POST', '/cart/add.js'], + ['POST', '/cart/change.js?sections=cart-drawer'], + ['GET', '/cart'], + ['GET', '/checkouts/xyz'], + ['GET', '/account/logout'], + ['POST', '/api/2025-01/graphql.json'], + ])('keeps the dev session cookie when %s %s rotates _shopify_essential', async (method, path) => { + const sessionCtx = createSessionCtx() + const response = await proxyStorefrontRequest(createH3Event(method, path), sessionCtx) + + expect(sessionCtx.session.sessionCookies).toHaveProperty('_shopify_essential', ':DEV-THEME-BOUND:') + // The browser still receives the cookie for its own jar: + expect(response.headers.getSetCookie().join(',')).toContain('_shopify_essential=:ROTATED:') + }) + + test('stores _shopify_essential from requests sent with the dev session', async () => { + const sessionCtx = createSessionCtx() + await proxyStorefrontRequest(createH3Event('GET', '/products/some-product.js'), sessionCtx) + + expect(sessionCtx.session.sessionCookies).toHaveProperty('_shopify_essential', ':ROTATED:') + }) + }) + describe('proxyStorefrontRequest — Storefront API passthrough', () => { const passthroughCtx = { ...ctx, diff --git a/packages/theme/src/cli/utilities/theme-environment/proxy.ts b/packages/theme/src/cli/utilities/theme-environment/proxy.ts index 9166dc50508..203c1c9779f 100644 --- a/packages/theme/src/cli/utilities/theme-environment/proxy.ts +++ b/packages/theme/src/cli/utilities/theme-environment/proxy.ts @@ -263,6 +263,11 @@ interface PatchResponseOptions { * Only the storefront exit page needs this: it redirects shoppers to another site. */ keepExternalRedirects?: boolean + /** + * Store the response's `_shopify_essential` as the dev session cookie (default: true). + * Only requests sent with the dev session's credentials get a cookie bound to the development theme. + */ + captureSessionCookie?: boolean } function patchProxiedResponseHeaders(ctx: DevServerContext, rawResponse: Response, options?: PatchResponseOptions) { @@ -301,7 +306,7 @@ function patchProxiedResponseHeaders(ctx: DevServerContext, rawResponse: Respons response.headers.set('Set-Cookie', patchCookieDomains(setCookieHeader, ctx).join(',')) const latestShopifyEssential = setCookieHeader.join(',').match(SESSION_COOKIE_REGEXP)?.[1] - if (latestShopifyEssential) { + if (latestShopifyEssential && options?.captureSessionCookie !== false) { ctx.session.sessionCookies[SESSION_COOKIE_NAME] = latestShopifyEssential } } @@ -407,7 +412,11 @@ export function proxyStorefrontRequest(event: H3Event, ctx: DevServerContext): P redirect: 'manual', headers, } as RequestInit & {duplex?: 'half'}) - .then((response) => patchProxiedResponseHeaders(ctx, response)) + .then((response) => + patchProxiedResponseHeaders(ctx, response, { + captureSessionCookie: !isPassthroughRequest(event) && needsBearerToken(event.path), + }), + ) .catch((error: Error) => { throw createFetchError(recordError(error), url) })