Skip to content

Commit f8da51a

Browse files
authored
Merge pull request #6924 from Shopify/fix-request-path-theme-dev
Fix theme dev shortcut to editor tracking non page requests
2 parents f43cea5 + ae77bc6 commit f8da51a

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

.changeset/eighty-papers-hunt.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 theme editor shortcut tracking fetch requests instead of page navigation

packages/theme/src/cli/utilities/theme-environment/html.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ describe('getHtmlHandler', async () => {
3939
lastRequestedPath: '',
4040
} as unknown as DevServerContext
4141

42-
test('sets lastRequestedPath to the rendering URL', async () => {
42+
test('sets lastRequestedPath when Sec-Fetch-Mode is navigate', async () => {
4343
const handler = getHtmlHandler(theme, ctx)
4444

4545
expect(ctx.lastRequestedPath).toStrictEqual('')
4646

47-
const event = createH3Event('GET', '/search?q=foo&options%5Bprefix%5D=last')
47+
const event = createH3Event('GET', '/search?q=foo&options%5Bprefix%5D=last', {'sec-fetch-mode': 'navigate'})
4848

4949
vi.mocked(render).mockResolvedValueOnce(
5050
new Response('', {
@@ -60,6 +60,24 @@ describe('getHtmlHandler', async () => {
6060
expect(ctx.lastRequestedPath).toStrictEqual('/search?q=foo&options%5Bprefix%5D=last')
6161
})
6262

63+
test('does not update lastRequestedPath when Sec-Fetch-Mode is not navigate', async () => {
64+
const handler = getHtmlHandler(theme, ctx)
65+
ctx.lastRequestedPath = '/previous-page'
66+
67+
const event = createH3Event('GET', '/search/suggest?q=foo&resources[type]=product', {'sec-fetch-mode': 'cors'})
68+
69+
vi.mocked(render).mockResolvedValueOnce(
70+
new Response('', {
71+
status: 200,
72+
headers: {'x-request-id': 'test-request-id'},
73+
}),
74+
)
75+
76+
await handler(event)
77+
78+
expect(ctx.lastRequestedPath).toStrictEqual('/previous-page')
79+
})
80+
6381
test('the development server session recovers when a theme id mismatch occurs', async () => {
6482
// Given
6583
const handler = getHtmlHandler(theme, ctx)

packages/theme/src/cli/utilities/theme-environment/html.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext): EventHandle
2424
return defineEventHandler((event) => {
2525
const [browserPathname = '/', browserSearch = ''] = event.path.split('?')
2626

27-
ctx.lastRequestedPath = event.path
27+
if (isNavigationRequest(event)) {
28+
ctx.lastRequestedPath = event.path
29+
}
2830

2931
const shouldRenderUploadErrorPage =
3032
ctx.options.errorOverlay !== 'silent' && ctx.localThemeFileSystem.uploadErrors.size > 0
@@ -160,6 +162,11 @@ function isKnownRenderingRequest(event: H3Event) {
160162
return ['section_id', 'sections', 'app_block_id'].some((key) => searchParams.has(key))
161163
}
162164

165+
/** Determines if a request is a user navigation type via sec-fetch-mode header. */
166+
function isNavigationRequest(event: H3Event): boolean {
167+
return event.headers.get('sec-fetch-mode') === 'navigate'
168+
}
169+
163170
async function tryProxyRequest(event: H3Event, ctx: DevServerContext, response: Response) {
164171
outputDebug(
165172
`Render failed for ${event.path} with ${response.status} (x-request-id: ${response.headers.get(

0 commit comments

Comments
 (0)