From a07e09ec0951322e998b0be87fb95be6dbd9dd81 Mon Sep 17 00:00:00 2001 From: Josh Faigan Date: Tue, 13 Oct 2026 10:34:20 -0400 Subject: [PATCH 1/2] fix(theme): keep the dev theme on stores whose markets all use subfolders --- .../theme-dev-market-redirect-preview.md | 5 + .../storefront-session.test.ts | 103 ++++++++++++++++++ .../theme-environment/storefront-session.ts | 36 +++++- 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 .changeset/theme-dev-market-redirect-preview.md diff --git a/.changeset/theme-dev-market-redirect-preview.md b/.changeset/theme-dev-market-redirect-preview.md new file mode 100644 index 00000000000..50753603447 --- /dev/null +++ b/.changeset/theme-dev-market-redirect-preview.md @@ -0,0 +1,5 @@ +--- +'@shopify/theme': patch +--- + +Fix `theme dev` showing the live theme instead of the development theme on stores where every market uses a subfolder diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts index a40f0b3395e..9f3e0da7728 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts @@ -390,6 +390,109 @@ describe('Storefront API', () => { ), ) }) + + test('follows a same-origin redirect that still carries preview_theme_id and keeps the final cookie', async () => { + // Given: every market uses a subfolder, so SFR redirects before storing the preview + vi.mocked(shopifyFetch) + .mockResolvedValueOnce( + response({ + status: 302, + headers: { + 'set-cookie': '_shopify_essential=:FIRST:; path=/; HttpOnly', + location: '/en-us?preview_theme_id=123456&_fd=0&pb=0', + }, + }), + ) + .mockResolvedValueOnce( + response({ + status: 302, + headers: { + 'set-cookie': '_shopify_essential=:SECOND:; path=/; HttpOnly', + location: '/en-us?_fd=0&pb=0', + }, + }), + ) + + // When + const cookies = await getStorefrontSessionCookies( + 'https://example-store.myshopify.com', + 'example-store.myshopify.com', + '123456', + ) + + // Then + expect(cookies).toEqual({_shopify_essential: ':SECOND:'}) + expect(shopifyFetch).toHaveBeenNthCalledWith( + 2, + 'https://example-store.myshopify.com/en-us?preview_theme_id=123456&_fd=0&pb=0', + expect.objectContaining({ + method: 'HEAD', + headers: expect.objectContaining({Cookie: '_shopify_essential=:FIRST:'}), + }), + ) + }) + + test('does not follow a redirect to another host', async () => { + // Given + vi.mocked(shopifyFetch) + .mockResolvedValueOnce( + response({ + status: 302, + headers: { + 'set-cookie': '_shopify_essential=:FIRST:; path=/; HttpOnly', + location: '/en-us?preview_theme_id=123456&_fd=0&pb=0', + }, + }), + ) + .mockResolvedValueOnce( + response({ + status: 302, + headers: {location: 'https://www.example-store.com/en-us?preview_theme_id=123456&_fd=0&pb=0'}, + }), + ) + + // When + const cookies = await getStorefrontSessionCookies( + 'https://example-store.myshopify.com', + 'example-store.myshopify.com', + '123456', + ) + + // Then + expect(cookies).toEqual({_shopify_essential: ':FIRST:'}) + expect(shopifyFetch).toHaveBeenCalledTimes(2) + }) + + test('stops following once the redirect no longer carries preview_theme_id', async () => { + // Given: hop 2 is ThemeSwitcher storing the preview, which strips the param and sets no cookie + vi.mocked(shopifyFetch) + .mockResolvedValueOnce( + response({ + status: 302, + headers: { + 'set-cookie': '_shopify_essential=:FIRST:; path=/; HttpOnly', + location: '/en-us?preview_theme_id=123456&_fd=0&pb=0', + }, + }), + ) + .mockResolvedValueOnce( + response({ + status: 302, + headers: {location: '/en-us?_fd=0&pb=0'}, + }), + ) + + // When + const cookies = await getStorefrontSessionCookies( + 'https://example-store.myshopify.com', + 'example-store.myshopify.com', + '123456', + ) + + // Then + expect(cookies).toEqual({_shopify_essential: ':FIRST:'}) + expect(shopifyFetch).toHaveBeenCalledTimes(2) + }) }) // Tests rely on this function because the 'packages/theme' package cannot diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts index e6030f0d881..78d8786e905 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts @@ -91,6 +91,8 @@ export async function getStorefrontSessionCookies( return {...cookieRecord, ...additionalCookies} } +const MAX_PREVIEW_REDIRECTS = 3 + async function sessionEssentialCookie(storeUrl: string, themeId: string, headers: Record, retries = 1) { const params = new URLSearchParams({ preview_theme_id: themeId, @@ -142,7 +144,39 @@ async function sessionEssentialCookie(storeUrl: string, themeId: string, headers return sessionEssentialCookie(storeUrl, themeId, headers, retries + 1) } - return shopifyEssential + return followPreviewRedirects(response, url, shopifyEssential, requestHeaders) +} + +/** + * SFR may redirect before it stores the preview theme, e.g. when every market + * uses a subfolder. Follow same-origin redirects that still carry + * preview_theme_id so the session ends up previewing the theme. + */ +async function followPreviewRedirects( + response: Response, + url: string, + shopifyEssential: string, + headers: Record, + hops = 0, +): Promise { + const location = response.headers.get('location') + if (!location || hops >= MAX_PREVIEW_REDIRECTS) return shopifyEssential + + const nextUrl = new URL(location, url) + if (nextUrl.origin !== new URL(url).origin || !nextUrl.searchParams.has('preview_theme_id')) { + return shopifyEssential + } + + const nextResponse = await shopifyFetch(nextUrl.toString(), { + method: 'HEAD', + redirect: 'manual', + headers: {...headers, Cookie: serializeCookies({_shopify_essential: shopifyEssential})}, + }) + + const setCookies = nextResponse.headers.raw()['set-cookie'] ?? [] + const nextShopifyEssential = getCookie(setCookies, '_shopify_essential') ?? shopifyEssential + + return followPreviewRedirects(nextResponse, nextUrl.toString(), nextShopifyEssential, headers, hops + 1) } async function enrichSessionWithStorefrontPassword( From c9013cf9002ef2cc3251d3dee725ea5e5cc1e217 Mon Sep 17 00:00:00 2001 From: Josh Faigan Date: Tue, 13 Oct 2026 11:12:35 -0400 Subject: [PATCH 2/2] fix(theme): follow only the one redirect storefront sends before storing the preview --- .../theme-dev-market-redirect-preview.md | 2 +- .../storefront-session.test.ts | 96 +++++++++++++++++-- .../theme-environment/storefront-session.ts | 17 +++- 3 files changed, 105 insertions(+), 10 deletions(-) diff --git a/.changeset/theme-dev-market-redirect-preview.md b/.changeset/theme-dev-market-redirect-preview.md index 50753603447..a6cd2691197 100644 --- a/.changeset/theme-dev-market-redirect-preview.md +++ b/.changeset/theme-dev-market-redirect-preview.md @@ -2,4 +2,4 @@ '@shopify/theme': patch --- -Fix `theme dev` showing the live theme instead of the development theme on stores where every market uses a subfolder +Fix `theme dev` showing the live theme instead of the development theme on stores where every market uses a subfolder. Sessions using a Theme Access password are not yet covered. diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts index 9f3e0da7728..b388db6f15c 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts @@ -10,10 +10,18 @@ import {AbortError} from '@shopify/cli-kit/node/error' import {passwordProtected} from '@shopify/cli-kit/node/themes/api' import {type AdminSession} from '@shopify/cli-kit/node/session' import {getThemeKitAccessDomain} from '@shopify/cli-kit/node/context/local' +import {outputDebug} from '@shopify/cli-kit/node/output' vi.mock('@shopify/cli-kit/node/http') vi.mock('@shopify/cli-kit/node/themes/api') vi.mock('@shopify/cli-kit/node/system') +vi.mock('@shopify/cli-kit/node/output', async (realImport) => { + const realModule = await realImport() + return { + ...realModule, + outputDebug: vi.fn(), + } +}) describe('Storefront API', () => { describe('isStorefrontPasswordProtected', () => { @@ -418,6 +426,12 @@ describe('Storefront API', () => { 'https://example-store.myshopify.com', 'example-store.myshopify.com', '123456', + undefined, + { + Signature: 'signature-value', + 'Signature-Input': 'signature-input-value', + 'Signature-Agent': 'signature-agent-value', + }, ) // Then @@ -427,12 +441,41 @@ describe('Storefront API', () => { 'https://example-store.myshopify.com/en-us?preview_theme_id=123456&_fd=0&pb=0', expect.objectContaining({ method: 'HEAD', - headers: expect.objectContaining({Cookie: '_shopify_essential=:FIRST:'}), + headers: expect.objectContaining({ + Cookie: '_shopify_essential=:FIRST:', + Signature: 'signature-value', + 'Signature-Input': 'signature-input-value', + 'Signature-Agent': 'signature-agent-value', + }), }), ) }) test('does not follow a redirect to another host', async () => { + // Given + vi.mocked(shopifyFetch).mockResolvedValueOnce( + response({ + status: 302, + headers: { + 'set-cookie': '_shopify_essential=:FIRST:; path=/; HttpOnly', + location: 'https://www.example-store.com/en-us?preview_theme_id=123456&_fd=0&pb=0', + }, + }), + ) + + // When + const cookies = await getStorefrontSessionCookies( + 'https://example-store.myshopify.com', + 'example-store.myshopify.com', + '123456', + ) + + // Then + expect(cookies).toEqual({_shopify_essential: ':FIRST:'}) + expect(shopifyFetch).toHaveBeenCalledTimes(1) + }) + + test('stops following once the redirect no longer carries preview_theme_id', async () => { // Given vi.mocked(shopifyFetch) .mockResolvedValueOnce( @@ -447,7 +490,7 @@ describe('Storefront API', () => { .mockResolvedValueOnce( response({ status: 302, - headers: {location: 'https://www.example-store.com/en-us?preview_theme_id=123456&_fd=0&pb=0'}, + headers: {location: '/en-us?_fd=0&pb=0'}, }), ) @@ -461,10 +504,42 @@ describe('Storefront API', () => { // Then expect(cookies).toEqual({_shopify_essential: ':FIRST:'}) expect(shopifyFetch).toHaveBeenCalledTimes(2) + expect(outputDebug).toHaveBeenCalledWith(expect.stringContaining('did not set a new _shopify_essential cookie')) }) - test('stops following once the redirect no longer carries preview_theme_id', async () => { - // Given: hop 2 is ThemeSwitcher storing the preview, which strips the param and sets no cookie + test('does not follow a redirect that does not carry preview_theme_id', async () => { + // Given + vi.mocked(shopifyFetch) + .mockResolvedValueOnce( + response({ + status: 302, + headers: { + 'set-cookie': '_shopify_essential=:FIRST:; path=/; HttpOnly', + location: '/en-us?_fd=0&pb=0', + }, + }), + ) + .mockResolvedValueOnce( + response({ + status: 200, + headers: {'set-cookie': '_shopify_essential=:SECOND:; path=/; HttpOnly'}, + }), + ) + + // When + const cookies = await getStorefrontSessionCookies( + 'https://example-store.myshopify.com', + 'example-store.myshopify.com', + '123456', + ) + + // Then + expect(cookies).toEqual({_shopify_essential: ':FIRST:'}) + expect(shopifyFetch).toHaveBeenCalledTimes(1) + }) + + test('stops after one followed redirect even if the next still carries preview_theme_id', async () => { + // Given vi.mocked(shopifyFetch) .mockResolvedValueOnce( response({ @@ -478,7 +553,16 @@ describe('Storefront API', () => { .mockResolvedValueOnce( response({ status: 302, - headers: {location: '/en-us?_fd=0&pb=0'}, + headers: { + 'set-cookie': '_shopify_essential=:SECOND:; path=/; HttpOnly', + location: '/en-us?preview_theme_id=123456&_fd=0&pb=0', + }, + }), + ) + .mockResolvedValueOnce( + response({ + status: 200, + headers: {'set-cookie': '_shopify_essential=:THIRD:; path=/; HttpOnly'}, }), ) @@ -490,8 +574,8 @@ describe('Storefront API', () => { ) // Then - expect(cookies).toEqual({_shopify_essential: ':FIRST:'}) expect(shopifyFetch).toHaveBeenCalledTimes(2) + expect(cookies).toEqual({_shopify_essential: ':SECOND:'}) }) }) diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts index 78d8786e905..46f6f9f804e 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts @@ -91,7 +91,7 @@ export async function getStorefrontSessionCookies( return {...cookieRecord, ...additionalCookies} } -const MAX_PREVIEW_REDIRECTS = 3 +const MAX_PREVIEW_REDIRECTS = 1 async function sessionEssentialCookie(storeUrl: string, themeId: string, headers: Record, retries = 1) { const params = new URLSearchParams({ @@ -174,9 +174,20 @@ async function followPreviewRedirects( }) const setCookies = nextResponse.headers.raw()['set-cookie'] ?? [] - const nextShopifyEssential = getCookie(setCookies, '_shopify_essential') ?? shopifyEssential + const nextShopifyEssential = getCookie(setCookies, '_shopify_essential') + if (!nextShopifyEssential) { + outputDebug( + `Preview redirect to ${nextUrl.pathname} did not set a new _shopify_essential cookie; keeping the previous one.`, + ) + } - return followPreviewRedirects(nextResponse, nextUrl.toString(), nextShopifyEssential, headers, hops + 1) + return followPreviewRedirects( + nextResponse, + nextUrl.toString(), + nextShopifyEssential ?? shopifyEssential, + headers, + hops + 1, + ) } async function enrichSessionWithStorefrontPassword(