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 45bb5add6a..5d00ac6a27 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 @@ -80,12 +80,14 @@ describe('Storefront API', () => { response({ status: 200, headers: {'set-cookie': ''}, + text: () => Promise.resolve(''), }), ) .mockResolvedValueOnce( response({ status: 200, headers: {'set-cookie': 'storefront_digest=digest-value; path=/; HttpOnly'}, + text: () => Promise.resolve(''), }), ) @@ -105,9 +107,15 @@ describe('Storefront API', () => { response({ status: 200, headers: {'set-cookie': '_shopify_essential=:AABBCCDDEEFFGGHH==123:; path=/; HttpOnly'}, + text: () => Promise.resolve(''), + }), + ) + .mockResolvedValueOnce( + response({ + status: 401, + text: () => Promise.resolve(''), }), ) - .mockResolvedValueOnce(response({status: 401})) // When const cookies = getStorefrontSessionCookies('https://example-store.myshopify.com', '123456', 'wrongpassword') @@ -121,7 +129,7 @@ describe('Storefront API', () => { // Tests rely on this function because the 'packages/theme' package cannot // directly access node-fetch and they use: new Response('OK', {status: 200}) - function response(mock: {status: number; headers?: {[key: string]: string}}) { + function response(mock: {status: number; headers?: {[key: string]: string}; text?: () => Promise}) { const setCookieHeader = (mock.headers ?? {})['set-cookie'] ?? '' const setCookieArray = [setCookieHeader] 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 1ec68e338d..40748a12a3 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts @@ -2,6 +2,7 @@ import {parseCookies, serializeCookies} from './cookies.js' import {defaultHeaders} from './storefront-utils.js' import {fetch} from '@shopify/cli-kit/node/http' import {AbortError} from '@shopify/cli-kit/node/error' +import {outputDebug} from '@shopify/cli-kit/node/output' export async function isStorefrontPasswordProtected(storeURL: string): Promise { const response = await fetch(prependHttps(storeURL), { @@ -104,6 +105,14 @@ async function sessionEssentialCookie(storeUrl: string, themeId: string, headers const setCookies = response.headers.raw()['set-cookie'] ?? [] const shopifyEssential = getCookie(setCookies, '_shopify_essential') + if (!shopifyEssential) { + outputDebug( + `Failed to obtain _shopify_essential cookie.\n + -Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n + -Body: ${await response.text()}`, + ) + } + return shopifyEssential } @@ -129,6 +138,14 @@ async function enrichSessionWithStorefrontPassword( const setCookies = response.headers.raw()['set-cookie'] ?? [] const storefrontDigest = getCookie(setCookies, 'storefront_digest') + if (!storefrontDigest) { + outputDebug( + `Failed to obtain storefront_digest cookie.\n + -Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n + -Body: ${await response.text()}`, + ) + } + return storefrontDigest }