Skip to content

Commit fd3ed80

Browse files
authored
Merge pull request #5328 from Shopify/fd-checkout-workaround
[Theme] Fix local dev proxy for checkout and accounts/logout
2 parents 07623f2 + f3498c3 commit fd3ed80

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

.changeset/giant-crews-act.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 the local dev proxy for `/checkouts` and `/accounts/logout` to avoid 401 and 403 errors.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ describe('dev proxy', () => {
235235
expect(canProxyRequest(event)).toBeTruthy()
236236
})
237237

238+
test('should proxy Checkout requests as they are not supported by the SFR client', () => {
239+
const event = createH3Event('GET', '/checkouts/xyz')
240+
expect(canProxyRequest(event)).toBeTruthy()
241+
})
242+
238243
test('should proxy CDN requests', () => {
239244
const event = createH3Event('GET', '/cdn/some-path')
240245
expect(canProxyRequest(event)).toBeTruthy()
@@ -295,5 +300,10 @@ describe('dev proxy', () => {
295300
const event = createH3Event('GET', '/account/login/multipass/<token>')
296301
expect(canProxyRequest(event)).toBeTruthy()
297302
})
303+
304+
test('should proxy /account/logout requests', () => {
305+
const event = createH3Event('GET', '/account/logout')
306+
expect(canProxyRequest(event)).toBeTruthy()
307+
})
298308
})
299309
})

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export const VANITY_CDN_PREFIX = '/cdn/'
2929
export const EXTENSION_CDN_PREFIX = '/ext/cdn/'
3030

3131
const CART_PATTERN = /^\/cart\//
32-
const ACCOUNT_PATTERN = /^\/account(\/login\/multipass(\/[^/]+)?)?\/?$/
32+
const CHECKOUT_PATTERN = /^\/checkouts\/(?!internal\/)/
33+
const ACCOUNT_PATTERN = /^\/account(\/login\/multipass(\/[^/]+)?|\/logout)?\/?$/
3334
const VANITY_CDN_PATTERN = new RegExp(`^${VANITY_CDN_PREFIX}`)
3435
const EXTENSION_CDN_PATTERN = new RegExp(`^${EXTENSION_CDN_PREFIX}`)
3536

@@ -81,6 +82,7 @@ export function getProxyHandler(_theme: Theme, ctx: DevServerContext) {
8182
export function canProxyRequest(event: H3Event) {
8283
if (event.method !== 'GET') return true
8384
if (event.path.match(CART_PATTERN)) return true
85+
if (event.path.match(CHECKOUT_PATTERN)) return true
8486
if (event.path.match(ACCOUNT_PATTERN)) return true
8587
if (event.path.match(VANITY_CDN_PATTERN)) return true
8688
if (event.path.match(EXTENSION_CDN_PATTERN)) return true
@@ -199,9 +201,11 @@ function patchProxiedResponseHeaders(ctx: DevServerContext, event: H3Event, resp
199201
const locationHeader = response.headers.get('Location')
200202
if (locationHeader) {
201203
const url = new URL(locationHeader, 'https://shopify.dev')
202-
url.searchParams.delete('_fd')
203-
url.searchParams.delete('pb')
204-
setResponseHeader(event, 'Location', url.href.replace(url.origin, ''))
204+
if (!CHECKOUT_PATTERN.test(url.pathname)) {
205+
url.searchParams.delete('_fd')
206+
url.searchParams.delete('pb')
207+
setResponseHeader(event, 'Location', url.href.replace(url.origin, ''))
208+
}
205209
}
206210

207211
// Cookies are set for the vanity domain, fix it for localhost:

0 commit comments

Comments
 (0)