From a7ad809520facbc1294c1677faa735227819dd6a Mon Sep 17 00:00:00 2001 From: Salvador Girones Gil Date: Thu, 5 Dec 2024 15:10:40 +0100 Subject: [PATCH] fix(core/js-sdk) Add the credentials field in the fetch() only if supported (#10446) fixes https://github.com/medusajs/nextjs-starter-medusa/issues/421 The root cause of the issue is that credentials is not supported by CloudFlare workers. --- packages/core/js-sdk/src/client.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/core/js-sdk/src/client.ts b/packages/core/js-sdk/src/client.ts index 03794630f8..025e0b7b0a 100644 --- a/packages/core/js-sdk/src/client.ts +++ b/packages/core/js-sdk/src/client.ts @@ -58,11 +58,17 @@ const normalizeRequest = ( body = JSON.stringify(body) } + const isFetchCredentialsSupported = "credentials" in Request.prototype + return { ...init, headers, // TODO: Setting this to "include" poses some security risks, as it will send cookies to any domain. We should consider making this configurable. - credentials: config.auth?.type === "session" ? "include" : "omit", + credentials: isFetchCredentialsSupported + ? config.auth?.type === "session" + ? "include" + : "omit" + : undefined, ...(body ? { body: body as RequestInit["body"] } : {}), } as RequestInit }