Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router: add layout and other file supports to parallel routes #51413

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 18 additions & 14 deletions packages/next/src/build/webpack/loaders/next-app-loader.ts
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
@@ -0,0 +1,3 @@
export default function Page() {
return 'intercepted parallel layout slug'
}
@@ -0,0 +1,8 @@
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<>
<h1> intercepted layout</h1>
{children}
</>
)
}
@@ -0,0 +1,8 @@
export default function Layout({ children }) {
return (
<div>
<h1>Parallel group slot Layout</h1>
{children}
</div>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return 'group slot children'
}
@@ -0,0 +1,8 @@
export default function Layout({ children }) {
return (
<div>
<h1 id="parallel-layout">parallel layout</h1>
{children}
</div>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return 'slot children'
}
@@ -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>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return 'children'
}
@@ -0,0 +1,3 @@
export default function Page() {
return 'parallel layout slug'
}
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