From b3966d2d443ae433da7316c151703b0a588d9fe4 Mon Sep 17 00:00:00 2001 From: conico974 Date: Fri, 24 May 2024 13:53:17 +0200 Subject: [PATCH] Fix duplicate cookies (#415) * fix duplicate cookies in all cases * Create shaggy-chicken-rhyme.md --- .changeset/shaggy-chicken-rhyme.md | 5 +++++ packages/open-next/src/http/openNextResponse.ts | 13 +++---------- 2 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 .changeset/shaggy-chicken-rhyme.md diff --git a/.changeset/shaggy-chicken-rhyme.md b/.changeset/shaggy-chicken-rhyme.md new file mode 100644 index 00000000..1070c469 --- /dev/null +++ b/.changeset/shaggy-chicken-rhyme.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +Fix duplicate cookies diff --git a/packages/open-next/src/http/openNextResponse.ts b/packages/open-next/src/http/openNextResponse.ts index 3dbc179c..aa8bb446 100644 --- a/packages/open-next/src/http/openNextResponse.ts +++ b/packages/open-next/src/http/openNextResponse.ts @@ -148,6 +148,8 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { getFixedHeaders(): OutgoingHttpHeaders { // Do we want to apply this on writeHead? this.fixHeaders(this.headers); + // This way we ensure that the cookies are correct + this.headers[SET_COOKIE_HEADER] = this._cookies; return this.headers; } @@ -178,16 +180,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { this.fixHeaders(this.headers); if (this._cookies.length > 0) { // For cookies we cannot do the same as for other headers - // We need to merge the cookies, and in this case, cookies generated by the routes or pages - // should be added after the ones generated by the middleware - // This prevents the middleware from overriding the cookies, especially for server actions - // which uses the same pathnames as the pages they're being called on - this.headers[SET_COOKIE_HEADER] = [ - ...(parseCookies( - this.initialHeaders?.[SET_COOKIE_HEADER] as string | string[], - ) ?? []), - ...this._cookies, - ]; + this.headers[SET_COOKIE_HEADER] = this._cookies; } if (this.streamCreator) {