Skip to content

Commit

Permalink
Fix next(param) in Pages Plugins (#2789)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Feb 23, 2023
1 parent 1335007 commit 4ca8c0b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-stingrays-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: Support overriding the next URL in subsequent executions with `next("/new-path")` from within a Pages Plugin
1 change: 1 addition & 0 deletions fixtures/pages-functions-app/public/here.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Successful!</h1>
10 changes: 10 additions & 0 deletions fixtures/pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ describe.concurrent("Pages Functions", () => {
const text = await response.text();
expect(text).toContain("I'm a fixed response");
});

it("should support proxying through to next(request)", async ({
expect,
}) => {
const response = await fetch(
`http://${ip}:${port}/mounted-plugin/proxy-me-somewhere-else`
);
const text = await response.text();
expect(text).toContain("Successful!");
});
});

describe.concurrent("can import static assets", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const onRequest = ({ next }) => next("/here");
10 changes: 7 additions & 3 deletions packages/wrangler/templates/pages-template-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,19 @@ export default function (pluginArgs: unknown) {
const handlerIterator = executeRequest(request, relativePathname);
const pluginNext = async (input?: RequestInfo, init?: RequestInit) => {
if (input !== undefined) {
request = new Request(input, init);
let url = input;
if (typeof input === "string") {
url = new URL(input, request.url).toString();
}
request = new Request(url, init);
}

const result = handlerIterator.next();
// Note we can't use `!result.done` because this doesn't narrow to the correct type
if (result.done === false) {
const { handler, params, path } = result.value;
const context = {
request,
request: new Request(request.clone()),
functionPath: workerContext.functionPath + path,
next: pluginNext,
params,
Expand All @@ -157,7 +161,7 @@ export default function (pluginArgs: unknown) {

return cloneResponse(response);
} else {
return next();
return next(request);
}
};

Expand Down

0 comments on commit 4ca8c0b

Please sign in to comment.