Skip to content

Commit e2c4350

Browse files
authored
fix: add dynamic route params to request url (#87)
* fix: add dynamic route params to request url * chore: changeset * fix: when path matches is null * chore: use object.entries instead. * fix: prettier error
1 parent f462227 commit e2c4350

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

.changeset/hip-days-remember.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudflare/next-on-pages': patch
3+
---
4+
5+
Add support for dynamic route params

templates/_worker.js/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,21 @@ export default {
159159
pathname.replace(__BASE_PATH__, '/').replace('//', '/')
160160
: pathname;
161161

162+
const nextPathnameMatcher = nextPathname.match(regexp);
163+
162164
if (
163-
nextPathname.match(regexp) ||
165+
nextPathnameMatcher ||
164166
`${nextPathname}/page`.replace('//page', '/page').match(regexp)
165167
) {
168+
if (nextPathnameMatcher?.groups) {
169+
const params = Object.entries(nextPathnameMatcher.groups);
170+
const urlWithParams = new URL(request.url);
171+
for (const [key, value] of params) {
172+
urlWithParams.searchParams.set(key, value);
173+
}
174+
request = new Request(urlWithParams.toString(), request);
175+
}
176+
166177
found = true;
167178
break;
168179
}

0 commit comments

Comments
 (0)