diff --git a/.changeset/old-sheep-visit.md b/.changeset/old-sheep-visit.md new file mode 100644 index 000000000..4120d3384 --- /dev/null +++ b/.changeset/old-sheep-visit.md @@ -0,0 +1,5 @@ +--- +'@cloudflare/next-on-pages': patch +--- + +Fix dynamic parameters with an underscore leading to an incorrect param being provided. diff --git a/packages/next-on-pages/templates/_worker.js/utils/pcre.ts b/packages/next-on-pages/templates/_worker.js/utils/pcre.ts index 108e27820..a4accd8f9 100644 --- a/packages/next-on-pages/templates/_worker.js/utils/pcre.ts +++ b/packages/next-on-pages/templates/_worker.js/utils/pcre.ts @@ -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. diff --git a/packages/next-on-pages/tests/templates/utils/pcre.test.ts b/packages/next-on-pages/tests/templates/utils/pcre.test.ts index f8f7a7580..5a1ee32a6 100644 --- a/packages/next-on-pages/tests/templates/utils/pcre.test.ts +++ b/packages/next-on-pages/tests/templates/utils/pcre.test.ts @@ -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(?nde)x(?:/)?', dest: '/new/$na_me/dest' }, + expected: { + match: true, + captureGroupKeys: ['na_me'], + newDest: '/new/nde/dest', + }, + }, ]; testCases.forEach(testCase => {