From 5afef823087af191ffbcf601b429512726c230a0 Mon Sep 17 00:00:00 2001 From: Janka Uryga Date: Tue, 9 Jul 2024 15:55:48 +0200 Subject: [PATCH] fix: don't clobber set-cookie headers if multiple are present --- packages/next/src/server/next-server.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index fab8eca0381a8b..03fec9ca506082 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -1616,16 +1616,20 @@ export default class NextNodeServer extends BaseServer< return { finished: true } } - for (let [key, value] of result.response.headers) { - if (key.toLowerCase() !== 'set-cookie') continue + // Split compound (comma-separated) set-cookie headers + if (result.response.headers.has('set-cookie')) { + const cookies = result.response.headers + .getSetCookie() + .flatMap((maybeCompoundCookie) => + splitCookiesString(maybeCompoundCookie) + ) - // Clear existing header. - result.response.headers.delete(key) + // Clear existing header(s) + result.response.headers.delete('set-cookie') // Append each cookie individually. - const cookies = splitCookiesString(value) for (const cookie of cookies) { - result.response.headers.append(key, cookie) + result.response.headers.append('set-cookie', cookie) } // Add cookies to request meta.