Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-kiwis-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Avoid creating unnecessary headers instance on every request
15 changes: 8 additions & 7 deletions packages/open-next/src/overrides/wrappers/cloudflare-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ const handler: WrapperHandler<InternalEvent, InternalResult> =
}): Writable {
const { statusCode, cookies, headers } = prelude;

const responseHeaders = new Headers(headers);
for (const cookie of cookies) {
responseHeaders.append("Set-Cookie", cookie);
}

// TODO(vicb): this is a workaround to make PPR work with `wrangler dev`
// See https://github.com/cloudflare/workers-sdk/issues/8004
if (url.hostname === "localhost") {
responseHeaders.set("Content-Encoding", "identity");
headers["content-encoding"] = "identity";
}

// Build headers array - set-cookie must be added separately for each cookie
const headerEntries: [string, string][] = Object.entries(headers);
for (const cookie of cookies) {
headerEntries.push(["set-cookie", cookie]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. while this might be more efficient I'm not convinced this is right. This is going to end up skipping any kind of validation on the value of cookie, which could end up causing problems elsewhere.

}

const { readable, writable } = new TransformStream({
Expand All @@ -60,7 +61,7 @@ const handler: WrapperHandler<InternalEvent, InternalResult> =
const body = NULL_BODY_STATUSES.has(statusCode) ? null : readable;
const response = new Response(body, {
status: statusCode,
headers: responseHeaders,
headers: headerEntries,
});
resolveResponse(response);

Expand Down
Loading