Skip to content

Commit

Permalink
pages: Fix "worker called response.clone()" warnings in `wrangler pag…
Browse files Browse the repository at this point in the history
…es dev`

Fixes #3259
  • Loading branch information
CarmenPopoviciu committed Oct 16, 2023
1 parent 36d4978 commit 04662e2
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function onRequestPost(context) {
try {
let data = await context.request.json();
return new Response(`[/functions/api/greet]: ${data.greeting}`);
} catch (err) {
return new Response("Error parsing JSON content", { status: 400 });
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export async function onRequest() {
return new Response("[/functions/greeting/hello]: Bonjour le monde!");
export async function onRequest(context) {
const url = new URL(context.request.url);

const response = await fetch(`${url.origin}/api/greet`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ greeting: "Bonjour le monde!" }),
});

return new Response(`[/functions/greeting/hello]: ${await response.text()}`);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"include": ["/", "/greeting/*", "/greeting*", "/date"],
"include": ["/", "/greeting/*", "/greeting*", "/date", "/api/*"],
"exclude": ["/bye*", "/date", "/*.*"]
}
4 changes: 3 additions & 1 deletion fixtures/pages-functions-with-routes-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ describe("Pages Functions with custom _routes.json", () => {
// matches /greeting/* include rule
response = await fetch(`http://${ip}:${port}/greeting/hello`);
text = await response.text();
expect(text).toEqual("[/functions/greeting/hello]: Bonjour le monde!");
expect(text).toEqual(
"[/functions/greeting/hello]: [/functions/api/greet]: Bonjour le monde!"
);

// matches /greeting/* include rule
response = await fetch(`http://${ip}:${port}/greeting/bye`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"include": ["/greeting/*", "/date", "/party*"],
"include": ["/greeting/*", "/date", "/party*", "/api/*"],
"exclude": ["/", "/party"]
}
16 changes: 15 additions & 1 deletion fixtures/pages-workerjs-with-routes-app/public/_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ export default {
}

if (url.pathname === "/greeting/hello") {
return new Response("[/greeting/hello]: Bonjour le monde!");
const url = new URL(request.url);

const response = await fetch(`${url.origin}/api/greet`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ greeting: "Bonjour le monde!" }),
});

return new Response(`[/greeting/hello]: ${await response.text()}`);
}

if (url.pathname === "/greeting/bye") {
Expand All @@ -36,6 +46,10 @@ export default {
return new Response("[/greetings]: Bonjour alligators!");
}

if (url.pathname === "/api/greet") {
return new Response("[/api/greet]: Bonjour le monde!");
}

return env.ASSETS.fetch(request);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("Pages Advanced Mode with custom _routes.json", () => {
// matches /greeting/* include rule
let response = await fetch(`http://${ip}:${port}/greeting/hello`);
let text = await response.text();
expect(text).toEqual("[/greeting/hello]: Bonjour le monde!");
expect(text).toEqual("[/greeting/hello]: [/api/greet]: Bonjour le monde!");

// matches /greeting/* include rule
response = await fetch(`http://${ip}:${port}/greeting/bye`);
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/templates/pages-template-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default function (pluginArgs: unknown) {
if (result.done === false) {
const { handler, params, path } = result.value;
const context = {
request: new Request(request.clone()),
request: new Request(request),
functionPath: workerContext.functionPath + path,
next: pluginNext,
params,
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/templates/pages-template-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default {
if (result.done === false) {
const { handler, params, path } = result.value;
const context = {
request: new Request(request.clone()),
request: new Request(request),
functionPath: path,
next,
params,
Expand Down

0 comments on commit 04662e2

Please sign in to comment.