Skip to content

Commit 6067118

Browse files
authored
Merge pull request #5997 from Shopify/shopify_essential
Fix `_shopify_essential` issues related to data replication and unstable connections
2 parents ddbf27d + c9c07b5 commit 6067118

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

.changeset/tame-mugs-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme': patch
3+
---
4+
5+
Fix `_shopify_essential` issues related to data replication and unstable connections

packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {type AdminSession} from '@shopify/cli-kit/node/session'
1212

1313
vi.mock('@shopify/cli-kit/node/http')
1414
vi.mock('@shopify/cli-kit/node/themes/api')
15+
vi.mock('@shopify/cli-kit/node/system')
1516

1617
describe('Storefront API', () => {
1718
describe('isStorefrontPasswordProtected', () => {
@@ -76,6 +77,20 @@ describe('Storefront API', () => {
7677
test(`throws an ShopifyEssentialError when _shopify_essential can't be defined`, async () => {
7778
// Given
7879
vi.mocked(shopifyFetch)
80+
.mockResolvedValueOnce(
81+
response({
82+
status: 200,
83+
headers: {'set-cookie': ''},
84+
text: () => Promise.resolve(''),
85+
}),
86+
)
87+
.mockResolvedValueOnce(
88+
response({
89+
status: 200,
90+
headers: {'set-cookie': ''},
91+
text: () => Promise.resolve(''),
92+
}),
93+
)
7994
.mockResolvedValueOnce(
8095
response({
8196
status: 200,
@@ -129,6 +144,39 @@ describe('Storefront API', () => {
129144
),
130145
)
131146
})
147+
148+
test('retries to obtain _shopify_essential cookie and succeeds', async () => {
149+
// Given: first 2 calls return no cookie, 3rd returns the cookie
150+
vi.mocked(shopifyFetch)
151+
.mockResolvedValueOnce(
152+
response({
153+
status: 200,
154+
headers: {'set-cookie': ''},
155+
text: () => Promise.resolve(''),
156+
}),
157+
)
158+
.mockResolvedValueOnce(
159+
response({
160+
status: 200,
161+
headers: {'set-cookie': ''},
162+
text: () => Promise.resolve(''),
163+
}),
164+
)
165+
.mockResolvedValueOnce(
166+
response({
167+
status: 200,
168+
headers: {'set-cookie': '_shopify_essential=:AABBCCDDEEFFGGHH==RETRYCOOKIE:; path=/; HttpOnly'},
169+
text: () => Promise.resolve(''),
170+
}),
171+
)
172+
173+
// When
174+
const cookies = await getStorefrontSessionCookies('https://example-store.myshopify.com', '123456')
175+
176+
// Then
177+
expect(cookies).toEqual({_shopify_essential: ':AABBCCDDEEFFGGHH==RETRYCOOKIE:'})
178+
expect(shopifyFetch).toHaveBeenCalledTimes(3)
179+
})
132180
})
133181

134182
// Tests rely on this function because the 'packages/theme' package cannot

packages/theme/src/cli/utilities/theme-environment/storefront-session.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {AbortError} from '@shopify/cli-kit/node/error'
55
import {outputDebug} from '@shopify/cli-kit/node/output'
66
import {type AdminSession} from '@shopify/cli-kit/node/session'
77
import {passwordProtected} from '@shopify/cli-kit/node/themes/api'
8+
import {sleep} from '@shopify/cli-kit/node/system'
89

910
export class ShopifyEssentialError extends Error {}
1011

@@ -83,7 +84,12 @@ export async function getStorefrontSessionCookies(
8384
return cookieRecord
8485
}
8586

86-
async function sessionEssentialCookie(storeUrl: string, themeId: string, headers: {[key: string]: string}) {
87+
async function sessionEssentialCookie(
88+
storeUrl: string,
89+
themeId: string,
90+
headers: {[key: string]: string},
91+
retries = 1,
92+
) {
8793
const params = new URLSearchParams({
8894
preview_theme_id: themeId,
8995
_fd: '0',
@@ -112,11 +118,20 @@ async function sessionEssentialCookie(storeUrl: string, themeId: string, headers
112118
outputDebug(
113119
`Failed to obtain _shopify_essential cookie.\n
114120
-Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n
115-
-Body: ${await response.text()}`,
116-
)
117-
throw new ShopifyEssentialError(
118-
'Your development session could not be created because the "_shopify_essential" could not be defined. Please, check your internet connection.',
121+
-Body: ${await response.text()}\n
122+
-Status: ${response.status}\n`,
119123
)
124+
125+
if (retries > 3) {
126+
throw new ShopifyEssentialError(
127+
'Your development session could not be created because the "_shopify_essential" could not be defined. Please, check your internet connection.',
128+
)
129+
}
130+
131+
outputDebug('Retrying to obtain the _shopify_essential cookie...')
132+
await sleep(retries)
133+
134+
return sessionEssentialCookie(storeUrl, themeId, headers, retries + 1)
120135
}
121136

122137
return shopifyEssential

0 commit comments

Comments
 (0)