Skip to content

Instantly share code, notes, and snippets.

@hanford
Created January 4, 2023 19:36
Show Gist options
  • Save hanford/ede056b57f7716f42c8a7e7d70bae166 to your computer and use it in GitHub Desktop.
Save hanford/ede056b57f7716f42c8a7e7d70bae166 to your computer and use it in GitHub Desktop.
diff --git a/node_modules/@cloudflare/next-on-pages/templates/_worker.js/index.ts b/node_modules/@cloudflare/next-on-pages/templates/_worker.js/index.ts
index 42bf66a..9ee30f2 100644
--- a/node_modules/@cloudflare/next-on-pages/templates/_worker.js/index.ts
+++ b/node_modules/@cloudflare/next-on-pages/templates/_worker.js/index.ts
@@ -140,7 +140,8 @@ export default {
}
}
- for (const { matchers, entrypoint } of Object.values(__FUNCTIONS__)) {
+ for (const [key, value] of Object.entries(__FUNCTIONS__)) {
+ const { matchers, entrypoint } = value;
let found = false;
for (const matcher of matchers) {
if (matcher.regexp) {
@@ -156,10 +157,20 @@ export default {
}
if (found) {
- return entrypoint.default(request, context);
+ return entrypoint.default(cloneRequest(request, key), context);
}
}
return env.ASSETS.fetch(request);
},
} as ExportedHandler<{ ASSETS: Fetcher }>;
+
+function cloneRequest(request: Request, matchedPath: string): Request {
+ const requestInit = {
+ ...request,
+ // Ensure `x-matched-path` is set so Next.js#handleRequest can properly recognize dynamic routes
+ headers: { ...request.headers, "x-matched-path": matchedPath },
+ };
+
+ return new Request(request.url, requestInit);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment