Skip to content

Commit 4fe968b

Browse files
nonoakijfeedthejim
andauthored
Fix: expected "catch all routes" are not matched in “parallel routes" (#58368)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? --> This PR fixes a bug where the expected catch-all route would not match if there were multiple "catch-all routes" under the "parallel route”. The page on the non-parallel routes path matches the catch all routes. that exist on the more detailed path. However, under parallel routes, it matches the most parent page of all the pages with catch all routes. For example, if there are files like below: ``` app/@sidebar/[...catchall]/page.tsx app/@sidebar/dashboard/[...catchall]/page.tsx ``` When accessing `/foo`, it should match `app/@sidebar/[...catchall]/page.tsx`, and this is working correctly. However, when accessing `/dashboard/foo`, it should match `app/@sidebar/dashboard/[...catchall]/page.tsx`, but `app/@sidebar/[...catchall]/page.tsx` is being matched instead. ## Repository to reproduce https://github.com/nonoakij/fix-parallel-routes-with-catch-all ## Related PR #58215 --------- Co-authored-by: Jimmy Lai <[email protected]>
1 parent dc59d3c commit 4fe968b

File tree

12 files changed

+100
-2
lines changed

12 files changed

+100
-2
lines changed

packages/next/src/build/normalize-catchall-routes.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ export function normalizeCatchAllRoutes(
1212
normalizer = new AppPathnameNormalizer()
1313
) {
1414
const catchAllRoutes = [
15-
...new Set(Object.values(appPaths).flat().filter(isCatchAllRoute)),
15+
...new Set(
16+
Object.values(appPaths)
17+
.flat()
18+
.filter(isCatchAllRoute)
19+
// Sorting is important because we want to match the most specific path.
20+
.sort((a, b) => b.split('/').length - a.split('/').length)
21+
),
1622
]
1723

1824
for (const appPath of Object.keys(appPaths)) {
1925
for (const catchAllRoute of catchAllRoutes) {
2026
const normalizedCatchAllRoute = normalizer.normalize(catchAllRoute)
2127
const normalizedCatchAllRouteBasePath = normalizedCatchAllRoute.slice(
2228
0,
23-
normalizedCatchAllRoute.indexOf('[')
29+
normalizedCatchAllRoute.search(catchAllRouteRegex)
2430
)
2531

2632
if (
@@ -48,6 +54,8 @@ function hasMatchedSlots(path1: string, path2: string): boolean {
4854
return true
4955
}
5056

57+
const catchAllRouteRegex = /\[?\[\.\.\./
58+
5159
function isCatchAllRoute(pathname: string): boolean {
5260
return pathname.includes('[...') || pathname.includes('[[...')
5361
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return 'slot catchall'
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Default() {
2+
return (
3+
<div>
4+
<div>Default</div>
5+
</div>
6+
)
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Foo() {
2+
return 'foo id catchAll'
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Foo() {
2+
return 'foo slot'
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return 'main catchall'
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Bar() {
2+
return 'bar'
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return 'foo id'
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return 'foo'
3+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Link from 'next/link'
2+
3+
export default function Layout({ children, slot }) {
4+
return (
5+
<div>
6+
<div>Main content</div>
7+
<div id="main">{children}</div>
8+
<div>
9+
Slot content:
10+
<div id="slot-content">{slot}</div>
11+
</div>
12+
13+
<div>
14+
<Link href="/parallel-nested-catchall/foo">foo</Link>
15+
</div>
16+
<div>
17+
<Link href="/parallel-nested-catchall/bar">catchall bar</Link>
18+
</div>
19+
<div>
20+
<Link href="/parallel-nested-catchall/foo/123">catchall foo id</Link>
21+
</div>
22+
</div>
23+
)
24+
}

0 commit comments

Comments
 (0)