Skip to content

Commit

Permalink
router: add layout and other file supports to parallel routes (#51413)
Browse files Browse the repository at this point in the history
This PR fixes a bug in which the layout files were not picked up if they were direct children of a parallel route slot.

Note: there's a bunch of other files that I've used for debugging that are not used for the test but I'm leaving them for future me.




link NEXT-969
  • Loading branch information
feedthejim committed Jun 19, 2023
1 parent bd46684 commit 5b7b91f
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/next/src/build/webpack/loaders/next-app-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,26 @@ async function createTreeCodeFromPath(
continue
}

const subSegmentPath = [...segments]
if (parallelKey !== 'children') {
subSegmentPath.push(parallelKey)
}

const normalizedParallelSegments = Array.isArray(parallelSegment)
? parallelSegment.slice(0, 1)
: [parallelSegment]

subSegmentPath.push(
...normalizedParallelSegments.filter(
(segment) => segment !== PAGE_SEGMENT
)
)

const { treeCode: subtreeCode } = await createSubtreePropsFromSegmentPath(
[
...segments,
...(parallelKey === 'children' ? [] : [parallelKey]),
Array.isArray(parallelSegment) ? parallelSegment[0] : parallelSegment,
]
subSegmentPath
)

const parallelSegmentPath =
segmentPath +
'/' +
(parallelKey === 'children' ? '' : `${parallelKey}/`) +
(Array.isArray(parallelSegment) ? parallelSegment[0] : parallelSegment)
const parallelSegmentPath = subSegmentPath.join('/')

// `page` is not included here as it's added above.
const filePaths = await Promise.all(
Expand Down Expand Up @@ -394,7 +401,6 @@ async function createTreeCodeFromPath(
]`
}
}

return {
treeCode: `{
${Object.entries(props)
Expand Down Expand Up @@ -473,10 +479,9 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {

const isParallelRoute = rest[0].startsWith('@')
if (isParallelRoute && rest.length === 2 && rest[1] === 'page') {
matched[rest[0]] = PAGE_SEGMENT
matched[rest[0]] = [PAGE_SEGMENT]
continue
}

if (isParallelRoute) {
matched[rest[0]] = rest.slice(1)
continue
Expand All @@ -485,7 +490,6 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
matched.children = rest[0]
}
}

return Object.entries(matched)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'intercepted parallel layout slug'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<>
<h1> intercepted layout</h1>
{children}
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Layout({ children }) {
return (
<div>
<h1>Parallel group slot Layout</h1>
{children}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'group slot children'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Layout({ children }) {
return (
<div>
<h1 id="parallel-layout">parallel layout</h1>
{children}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'slot children'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Link from 'next/link'

export default function Layout({ children, slot, groupslot }) {
return (
<div>
<h1>Main Layout</h1>
{children}
<div id="slot">{slot}</div>
<div id="groupslot">{groupslot}</div>
<Link href="/parallel-layout/sub/route">/sub/route</Link>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'children'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'parallel layout slug'
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ createNextDescribe(
)
})

it('should support layout files in parallel routes', async () => {
const browser = await next.browser('/parallel-layout')
await check(
() => browser.waitForElementByCss('#parallel-layout').text(),
'parallel layout'
)
})

it('should only scroll to the parallel route that was navigated to', async () => {
const browser = await next.browser('/parallel-scroll')

Expand Down

0 comments on commit 5b7b91f

Please sign in to comment.