From 853b20234c1b2f6e3d39ecb62649ec9b3fb95e64 Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Fri, 20 Sep 2024 21:52:31 +0900 Subject: [PATCH 1/4] Prevent removing Accept header in proxy to fix cart/add request --- packages/theme/src/cli/utilities/theme-environment/proxy.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/theme/src/cli/utilities/theme-environment/proxy.ts b/packages/theme/src/cli/utilities/theme-environment/proxy.ts index 619411574f..43fdf5603b 100644 --- a/packages/theme/src/cli/utilities/theme-environment/proxy.ts +++ b/packages/theme/src/cli/utilities/theme-environment/proxy.ts @@ -4,7 +4,7 @@ import { defineEventHandler, clearResponseHeaders, sendProxy, - getProxyRequestHeaders, + getRequestHeaders, getRequestWebStream, getRequestIP, type H3Event, @@ -167,7 +167,9 @@ const HOP_BY_HOP_HEADERS = [ 'trailer', 'transfer-encoding', 'upgrade', + 'expect', 'content-security-policy', + 'host', ] function patchProxiedResponseHeaders(ctx: DevServerContext, event: H3Event, response: Response | NodeResponse) { @@ -203,7 +205,7 @@ function patchProxiedResponseHeaders(ctx: DevServerContext, event: H3Event, resp * Filters headers to forward to SFR. */ export function getProxyStorefrontHeaders(event: H3Event) { - const proxyRequestHeaders = getProxyRequestHeaders(event) as {[key: string]: string} + const proxyRequestHeaders = getRequestHeaders(event) as {[key: string]: string} // H3 already removes most hop-by-hop request headers: // https://github.com/unjs/h3/blob/ac6d83de2abe5411d4eaea8ecf2165ace16a65f3/src/utils/proxy.ts#L25 From 492225c47489a6a35d7821b30e22b249f86db3ed Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Fri, 20 Sep 2024 21:53:10 +0900 Subject: [PATCH 2/4] Changesets --- .changeset/sharp-eggs-remember.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sharp-eggs-remember.md diff --git a/.changeset/sharp-eggs-remember.md b/.changeset/sharp-eggs-remember.md new file mode 100644 index 0000000000..b38333b4d8 --- /dev/null +++ b/.changeset/sharp-eggs-remember.md @@ -0,0 +1,5 @@ +--- +'@shopify/theme': patch +--- + +Fix cart/add request in development. From 5957ef08c3dd9623e36691f4ca934d2ff1b5af12 Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Fri, 20 Sep 2024 22:01:43 +0900 Subject: [PATCH 3/4] Cleanup --- packages/theme/src/cli/utilities/theme-environment/proxy.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/theme/src/cli/utilities/theme-environment/proxy.ts b/packages/theme/src/cli/utilities/theme-environment/proxy.ts index 43fdf5603b..4387ce0e48 100644 --- a/packages/theme/src/cli/utilities/theme-environment/proxy.ts +++ b/packages/theme/src/cli/utilities/theme-environment/proxy.ts @@ -207,8 +207,6 @@ function patchProxiedResponseHeaders(ctx: DevServerContext, event: H3Event, resp export function getProxyStorefrontHeaders(event: H3Event) { const proxyRequestHeaders = getRequestHeaders(event) as {[key: string]: string} - // H3 already removes most hop-by-hop request headers: - // https://github.com/unjs/h3/blob/ac6d83de2abe5411d4eaea8ecf2165ace16a65f3/src/utils/proxy.ts#L25 for (const headerKey of HOP_BY_HOP_HEADERS) { delete proxyRequestHeaders[headerKey] } From fa91638660d16820048fc3551e37c3519480adf2 Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Fri, 20 Sep 2024 22:03:17 +0900 Subject: [PATCH 4/4] Fix unit tests --- .../theme/src/cli/utilities/theme-environment/proxy.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts b/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts index 3f01315cfc..d6e9ba9550 100644 --- a/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts +++ b/packages/theme/src/cli/utilities/theme-environment/proxy.test.ts @@ -180,9 +180,9 @@ describe('dev proxy', () => { // Removed: event.node.req.headers.connection = '...' event.node.req.headers['proxy-authenticate'] = '...' - event.node.req.headers.accept = 'text/html' event.node.req.headers.host = 'abnb' // Kept: + event.node.req.headers.accept = 'text/html' event.node.req.headers.cookie = 'oreo' event.node.req.headers['user-agent'] = 'vitest' event.node.req.headers['x-custom'] = 'true' @@ -190,6 +190,7 @@ describe('dev proxy', () => { expect(getProxyStorefrontHeaders(event)).toMatchInlineSnapshot(` { "X-Forwarded-For": "42", + "accept": "text/html", "cookie": "oreo", "user-agent": "vitest", "x-custom": "true",