Skip to content

Commit

Permalink
Fix duplicate cookies (#415)
Browse files Browse the repository at this point in the history
* fix duplicate cookies in all cases

* Create shaggy-chicken-rhyme.md
  • Loading branch information
conico974 authored May 24, 2024
1 parent e98e014 commit b3966d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-chicken-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Fix duplicate cookies
13 changes: 3 additions & 10 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b3966d2

Please sign in to comment.