Skip to content

Commit 12a4037

Browse files
committed
Ignore the API collect endpoint in the shopify theme dev and shopify app dev commands
1 parent 409b322 commit 12a4037

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

.changeset/quiet-toys-admire.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+
Ignore API collect endpoint in `shopify theme dev` and `shopify app dev` commands

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,50 @@ describe('dev proxy', () => {
126126
`"<https://cdn.shopify.com>; rel=\\"preconnect\\", <https://cdn.shopify.com>; rel=\\"preconnect\\"; crossorigin,</cdn/shop/t/10/assets/component-localization-form.css?v=120620094879297847921723560016>; as=\\"style\\"; rel=\\"preload\\""`,
127127
)
128128
})
129+
130+
test('proxies beacon endpoint URLs to local server', () => {
131+
const content = html`<html>
132+
<head></head>
133+
<body>
134+
<div data-shs-beacon-endpoint="https://my-store.myshopify.com/api/collect"></div>
135+
</body>
136+
</html>`
137+
138+
expect(injectCdnProxy(content, ctx)).toMatchInlineSnapshot(`
139+
"<html>
140+
<head></head>
141+
<body>
142+
<div data-shs-beacon-endpoint=\\"/api/collect\\"></div>
143+
</body>
144+
</html>"
145+
`)
146+
})
147+
148+
test('proxies beacon endpoint URLs with single quotes', () => {
149+
const content = `<div data-shs-beacon-endpoint='https://my-store.myshopify.com/api/collect'></div>`
150+
151+
expect(injectCdnProxy(content, ctx)).toMatchInlineSnapshot(
152+
`"<div data-shs-beacon-endpoint='/api/collect'></div>"`,
153+
)
154+
})
155+
156+
test('proxies multiple beacon endpoint URLs in the same content', () => {
157+
const content = html`<html>
158+
<body>
159+
<div data-shs-beacon-endpoint="https://my-store.myshopify.com/api/collect"></div>
160+
<span data-shs-beacon-endpoint="https://my-store.myshopify.com/api/collect"></span>
161+
</body>
162+
</html>`
163+
164+
expect(injectCdnProxy(content, ctx)).toMatchInlineSnapshot(`
165+
"<html>
166+
<body>
167+
<div data-shs-beacon-endpoint=\\"/api/collect\\"></div>
168+
<span data-shs-beacon-endpoint=\\"/api/collect\\"></span>
169+
</body>
170+
</html>"
171+
`)
172+
})
129173
})
130174

131175
describe('patchRenderingResponse', () => {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {DevServerContext} from './types.js'
1414
export const VANITY_CDN_PREFIX = '/cdn/'
1515
export const EXTENSION_CDN_PREFIX = '/ext/cdn/'
1616

17+
const API_COLLECT_PATH = '/api/collect'
1718
const CART_PATTERN = /^\/cart\//
1819
const CHECKOUT_PATTERN = /^\/checkouts\/(?!internal\/)/
1920
const ACCOUNT_PATTERN = /^\/account(\/login\/multipass(\/[^/]+)?|\/logout)?\/?$/
@@ -28,7 +29,7 @@ const IGNORED_ENDPOINTS = [
2829
'/web-pixels@',
2930
'/wpm',
3031
'/services/',
31-
'/api/collect',
32+
API_COLLECT_PATH,
3233
// Cloudflare's turnstile challenge #6416
3334
'/cdn-cgi/challenge-platform',
3435
]
@@ -125,6 +126,11 @@ function getStoreFqdnForRegEx(ctx: DevServerContext) {
125126
export function injectCdnProxy(originalContent: string, ctx: DevServerContext) {
126127
let content = originalContent
127128

129+
// -- The beacon endpoint is patched in injectCdnProxy to be proxied and ignored locally
130+
const beaconEndpointREStr = `(data-shs-beacon-endpoint=["'])https://${getStoreFqdnForRegEx(ctx)}${API_COLLECT_PATH}`
131+
const beaconEndpointRE = new RegExp(beaconEndpointREStr, 'g')
132+
content = content.replace(beaconEndpointRE, `$1${API_COLLECT_PATH}`)
133+
128134
// -- Redirect all usages to the vanity CDN to the local server:
129135
const vanityCdnRE = new RegExp(`(https?:)?//${getStoreFqdnForRegEx(ctx)}${VANITY_CDN_PREFIX}`, 'g')
130136
content = content.replace(vanityCdnRE, VANITY_CDN_PREFIX)

0 commit comments

Comments
 (0)