Skip to content

Commit

Permalink
Mutable headers for next() responses in Pages Functions (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble authored Apr 19, 2022
1 parent 60c409a commit 3e0db3b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/large-beans-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: Makes Response Headers object mutable after a call to `next()` in Pages Functions
5 changes: 5 additions & 0 deletions examples/pages-functions-app/functions/_middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const onRequest = async ({ next }) => {
const response = await next();
response.headers.set("x-custom", "header value");
return response;
};
1 change: 1 addition & 0 deletions examples/pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe("Pages Functions", () => {

it("renders static pages", async () => {
const response = await waitUntilReady("http://localhost:8789/");
expect(response.headers.get("x-custom")).toBe("header value");
const text = await response.text();
expect(text).toContain("Hello, world!");
});
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/pages/functions/template-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default {
// https://fetch.spec.whatwg.org/#null-body-status
return new Response(
[101, 204, 205, 304].includes(response.status) ? null : response.body,
response
{ ...response, headers: new Headers([...response.headers.entries()]) }
);
} else if (__FALLBACK_SERVICE__) {
// There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
Expand Down

0 comments on commit 3e0db3b

Please sign in to comment.