Skip to content
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

Fix Pages Plugins and static asset routing #970

Merged
merged 1 commit into from
May 12, 2022
Merged
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
7 changes: 7 additions & 0 deletions .changeset/honest-pants-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: Fixes Pages Plugins and static asset routing.

There was previously a bug where a relative pathname would be missing the leading slash which would result in routing errors.
12 changes: 12 additions & 0 deletions examples/pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,17 @@ describe("Pages Functions", () => {
expect(text).toContain(
"<h1>Hello from a static asset brought from a Plugin!</h1>"
);

response = await waitUntilReady(
"http://localhost:8789/mounted-plugin/static/foo"
);
text = await response.text();
expect(text).toContain("<h1>foo</h1>");

response = await waitUntilReady(
"http://localhost:8789/mounted-plugin/static/dir/bar"
);
text = await response.text();
expect(text).toContain("<h1>bar</h1>");
});
});
1 change: 0 additions & 1 deletion examples/pages-plugin-example/functions/static.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { onRequest } from "assets:../../public";
9 changes: 9 additions & 0 deletions examples/pages-plugin-example/public/dir/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Static asset from a Plugin!</title>
</head>
<body>
<h1>bar</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions examples/pages-plugin-example/public/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Static asset from a Plugin!</title>
</head>
<body>
<h1>foo</h1>
</body>
</html>
2 changes: 1 addition & 1 deletion packages/wrangler/pages/functions/buildWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function buildWorker({
// TODO: Watch args.path for changes and re-copy when updated
contents: `export const onRequest = ({ request, env, functionPath }) => {
const url = new URL(request.url)
const relativePathname = url.pathname.split(functionPath)[1] || "/";
const relativePathname = \`/\${url.pathname.split(functionPath)[1] || ''}\`.replace(/^\\/\\//, '/');
url.pathname = '/cdn-cgi/pages-plugins/${identifier}' + relativePathname
request = new Request(url.toString(), request)
return env.ASSETS.fetch(request)
Expand Down
5 changes: 3 additions & 2 deletions packages/wrangler/pages/functions/template-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ export default function (pluginArgs) {
const { env, next, data } = workerContext;

const url = new URL(request.url);
const relativePathname =
url.pathname.split(workerContext.functionPath)[1] || "/";
const relativePathname = `/${
url.pathname.split(workerContext.functionPath)[1] || ""
}`.replace(/^\/\//, "/");

const handlerIterator = executeRequest(request, relativePathname);
const pluginNext = async (input?: RequestInfo, init?: RequestInit) => {
Expand Down
9 changes: 8 additions & 1 deletion packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,10 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
default: false,
description: "Build a plugin rather than a Worker script",
},
"build-output-directory": {
type: "string",
description: "The directory to output static assets to",
},
})
.epilogue(pagesBetaWarning),
async ({
Expand All @@ -1587,12 +1591,15 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
fallbackService,
watch,
plugin,
"build-output-directory": buildOutputDirectory,
}) => {
if (!isInPagesCI) {
// Beta message for `wrangler pages <commands>` usage
logger.log(pagesBetaWarning);
}

buildOutputDirectory ??= dirname(outfile);

await buildFunctions({
outfile,
outputConfigPath,
Expand All @@ -1602,7 +1609,7 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
fallbackService,
watch,
plugin,
buildOutputDirectory: dirname(outfile),
buildOutputDirectory,
});
}
)
Expand Down