Skip to content

Commit

Permalink
fix: dynamic params with underscore not applying correctly (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Sep 14, 2023
1 parent c06b437 commit 1e25a97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/old-sheep-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Fix dynamic parameters with an underscore leading to an incorrect param being provided.
2 changes: 1 addition & 1 deletion packages/next-on-pages/templates/_worker.js/utils/pcre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function applyPCREMatches(
match: RegExpMatchArray,
captureGroupKeys: string[],
): string {
return rawStr.replace(/\$([a-zA-Z0-9]+)/g, (_, key) => {
return rawStr.replace(/\$([a-zA-Z0-9_]+)/g, (_, key) => {
const index = captureGroupKeys.indexOf(key);
// If the extracted key does not exist as a named capture group from the matcher, we can
// reasonably assume it's a number and return the matched index. Fallback to an empty string.
Expand Down
10 changes: 10 additions & 0 deletions packages/next-on-pages/tests/templates/utils/pcre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ describe('applyPCREMatches', () => {
newDest: '/new/nde/dest?id=',
},
},
{
name: 'should process dest for a route with named group containing underscore',
url: 'https://example.com/index',
route: { src: '^/i(?<na_me>nde)x(?:/)?', dest: '/new/$na_me/dest' },
expected: {
match: true,
captureGroupKeys: ['na_me'],
newDest: '/new/nde/dest',
},
},
];

testCases.forEach(testCase => {
Expand Down

0 comments on commit 1e25a97

Please sign in to comment.