-
Notifications
You must be signed in to change notification settings - Fork 176
replace delete with assigning to undefined #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opennextjs/aws": patch | ||
--- | ||
|
||
Replaces delete usage with assignment operator to undefined to avoid going into slowest mode in objects |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { | |
if (key === SET_COOKIE_HEADER) { | ||
this._cookies = []; | ||
} else { | ||
delete this.headers[key]; | ||
this.headers[key] = undefined; | ||
} | ||
return this; | ||
} | ||
|
@@ -188,10 +188,6 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { | |
|
||
const parsedHeaders = parseHeaders(this.headers); | ||
|
||
// We need to remove the set-cookie header from the parsed headers because | ||
// it does not handle multiple set-cookie headers properly | ||
delete parsedHeaders[SET_COOKIE_HEADER]; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this is removed but not replaced? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I see, you're removing it in the util.ts change instead. |
||
if (this.streamCreator) { | ||
this.responseStream = this.streamCreator?.writeHeaders({ | ||
statusCode: this.statusCode ?? 200, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,12 @@ export const parseHeaders = ( | |
continue; | ||
} | ||
const keyLower = key.toLowerCase(); | ||
if (keyLower === "set-cookie") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that a change in behavior? |
||
// We need to remove the set-cookie header from the parsed headers because | ||
// it does not handle multiple set-cookie headers properly | ||
continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. side note... I keep thinking that it would be nice if Object.entries(headers, filter); ...that could optimize this pattern more and make it so the collection only had to be iterated through once... That's for another time tho. |
||
} | ||
|
||
/** | ||
* Next can return an Array for the Location header when you return null from a get in the cacheHandler on a page that has a redirect() | ||
* We dont want to merge that into a comma-separated string | ||
|
@@ -22,7 +28,7 @@ export const parseHeaders = ( | |
* See: https://github.com/opennextjs/opennextjs-cloudflare/issues/875#issuecomment-3258248276 | ||
* and https://github.com/opennextjs/opennextjs-aws/pull/977#issuecomment-3261763114 | ||
*/ | ||
if (keyLower === "location" && Array.isArray(value)) { | ||
if (Array.isArray(value) && keyLower === "location") { | ||
if (value[0] === value[1]) { | ||
result[keyLower] = value[0]; | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment above explicitely says "the property exists"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth an upstream pr to next.js tho I suspect it's been tried before.
The key challenge with using
delete
here is that it immediately putsreq
into a slower / non-optimized mode for all property accesses. Whether or not that moves the needle or not in performance depends on usage pattern. In this particular case, let's leave this asdelete req.body
and see if we can try to fix upstream.but as a general rule, we should be avoiding
delete
like the plague.