Skip to content

Commit

Permalink
refactor: include new option for including prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Jan 17, 2025
1 parent 6615138 commit 3caf1d4
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 47 deletions.
6 changes: 3 additions & 3 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3241,7 +3241,7 @@ export default async function build(
: normalizeRouteRegex(
getNamedRouteRegex(dataRoute, {
prefixRouteKeys: false,
includeExtraParts: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
}).re.source
),
Expand All @@ -3251,7 +3251,7 @@ export default async function build(
: normalizeRouteRegex(
getNamedRouteRegex(prefetchDataRoute, {
prefixRouteKeys: false,
includeExtraParts: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
}).re.source
),
Expand Down Expand Up @@ -3669,7 +3669,7 @@ export default async function build(
dataRouteRegex: normalizeRouteRegex(
getNamedRouteRegex(dataRoute, {
prefixRouteKeys: true,
includeExtraParts: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
}).re.source
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function buildDataRoute(page: string, buildId: string) {
if (isDynamicRoute(page)) {
const routeRegex = getNamedRouteRegex(dataRoute, {
prefixRouteKeys: true,
includeExtraParts: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
})

Expand Down
84 changes: 77 additions & 7 deletions packages/next/src/shared/lib/router/utils/route-regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ describe('getNamedRouteRegex', () => {
optional: false,
})

expect(regex.re.test('/photos/(.)next/123')).toBe(true)
expect(regex.re.exec('/photos/(.)next/123')).toMatchInlineSnapshot(`
[
"/photos/(.)next/123",
"next",
"123",
]
`)
})

it('should match named routes correctly when interception markers are adjacent to dynamic segments', () => {
Expand Down Expand Up @@ -68,15 +74,25 @@ describe('getNamedRouteRegex', () => {
optional: false,
})

expect(regex.re.test('/photos/(..)(..)next/123')).toBe(true)
expect(regex.re.source).toMatchInlineSnapshot(
`"^\\/photos\\/\\(\\.\\.\\)\\(\\.\\.\\)([^/]+?)\\/([^/]+?)(?:\\/)?$"`
)

expect(regex.re.exec('/photos/(..)(..)next/123')).toMatchInlineSnapshot(`
[
"/photos/(..)(..)next/123",
"next",
"123",
]
`)
})

it('should not remove extra parts beside the param segments', () => {
const { re, namedRegex, routeKeys } = getNamedRouteRegex(
'/[locale]/about.segments/[...segmentPath].segment.rsc',
{
prefixRouteKeys: true,
includeExtraParts: true,
includeSuffix: true,
}
)
expect(routeKeys).toEqual({
Expand All @@ -91,11 +107,45 @@ describe('getNamedRouteRegex', () => {
)
})

it('should not remove extra parts in front of the param segments', () => {
const { re, namedRegex, routeKeys } = getNamedRouteRegex(
'/[locale]/about.segments/$dname$d[name].segment.rsc',
{
prefixRouteKeys: true,
includeSuffix: true,
includePrefix: true,
}
)
expect(routeKeys).toEqual({
nxtPlocale: 'nxtPlocale',
nxtPname: 'nxtPname',
})
expect(namedRegex).toEqual(
'^/(?<nxtPlocale>[^/]+?)/about\\.segments/\\$dname\\$d(?<nxtPname>[^/]+?)\\.segment\\.rsc(?:/)?$'
)
expect(re.source).toEqual(
'^\\/([^/]+?)\\/about\\.segments\\/\\$dname\\$d([^/]+?)\\.segment\\.rsc(?:\\/)?$'
)

expect('/en/about.segments/$dname$dwyatt.segment.rsc'.match(re))
.toMatchInlineSnapshot(`
[
"/en/about.segments/$dname$dwyatt.segment.rsc",
"en",
"wyatt",
]
`)
})

it('should handle interception markers not adjacent to dynamic path segments', () => {
const regex = getNamedRouteRegex('/photos/(.)author/[id]', {
prefixRouteKeys: true,
})

expect(regex.namedRegex).toMatchInlineSnapshot(
`"^/photos/\\(\\.\\)author/(?<nxtPid>[^/]+?)(?:/)?$"`
)

expect(regex.routeKeys).toEqual({
nxtPid: 'nxtPid',
})
Expand All @@ -108,7 +158,12 @@ describe('getNamedRouteRegex', () => {
optional: false,
})

expect(regex.re.test('/photos/(.)author/123')).toBe(true)
expect(regex.re.exec('/photos/(.)author/123')).toMatchInlineSnapshot(`
[
"/photos/(.)author/123",
"123",
]
`)
})

it('should handle optional dynamic path segments', () => {
Expand Down Expand Up @@ -142,9 +197,24 @@ describe('getNamedRouteRegex', () => {
optional: true,
})

expect(regex.re.test('/photos/1')).toBe(true)
expect(regex.re.test('/photos/1/2/3')).toBe(true)
expect(regex.re.test('/photos')).toBe(true)
expect(regex.re.exec('/photos/1')).toMatchInlineSnapshot(`
[
"/photos/1",
"1",
]
`)
expect(regex.re.exec('/photos/1/2/3')).toMatchInlineSnapshot(`
[
"/photos/1/2/3",
"1/2/3",
]
`)
expect(regex.re.exec('/photos')).toMatchInlineSnapshot(`
[
"/photos",
undefined,
]
`)
})
})

Expand Down
Loading

0 comments on commit 3caf1d4

Please sign in to comment.