Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/theme-dev-market-redirect-preview.md
Original file line number Diff line number Diff line change
@@ -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. Sessions using a Theme Access password are not yet covered.
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import('@shopify/cli-kit/node/output')>()
return {
...realModule,
outputDebug: vi.fn(),
}
})

describe('Storefront API', () => {
describe('isStorefrontPasswordProtected', () => {
Expand Down Expand Up @@ -390,6 +398,185 @@ 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',
undefined,
{
Signature: 'signature-value',
'Signature-Input': 'signature-input-value',
'Signature-Agent': 'signature-agent-value',
},
)

// 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:',
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(
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)
expect(outputDebug).toHaveBeenCalledWith(expect.stringContaining('did not set a new _shopify_essential 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({
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?preview_theme_id=123456&_fd=0&pb=0',
},
}),
)
.mockResolvedValueOnce(
response({
status: 200,
headers: {'set-cookie': '_shopify_essential=:THIRD:; path=/; HttpOnly'},
}),
)

// When
const cookies = await getStorefrontSessionCookies(
'https://example-store.myshopify.com',
'example-store.myshopify.com',
'123456',
)

// Then
expect(shopifyFetch).toHaveBeenCalledTimes(2)
expect(cookies).toEqual({_shopify_essential: ':SECOND:'})
})
})

// Tests rely on this function because the 'packages/theme' package cannot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export async function getStorefrontSessionCookies(
return {...cookieRecord, ...additionalCookies}
}

const MAX_PREVIEW_REDIRECTS = 1

async function sessionEssentialCookie(storeUrl: string, themeId: string, headers: Record<string, string>, retries = 1) {
const params = new URLSearchParams({
preview_theme_id: themeId,
Expand Down Expand Up @@ -142,7 +144,50 @@ 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<string, string>,
hops = 0,
): Promise<string> {
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
}
Comment on lines +165 to +168

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This isn't something we expect to get from upstream in practice, so I'm keeping this scoped to the issue we're fixing rather than adding a guard for it.


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')
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 ?? shopifyEssential,
headers,
hops + 1,
)
}

async function enrichSessionWithStorefrontPassword(
Expand Down
Loading