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 2 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
31 changes: 15 additions & 16 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,24 @@ 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')
)

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 +399,6 @@ async function createTreeCodeFromPath(
]`
}
}

return {
treeCode: `{
${Object.entries(props)
Expand Down Expand Up @@ -472,10 +476,6 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
}

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

if (isParallelRoute) {
matched[rest[0]] = rest.slice(1)
Expand All @@ -485,7 +485,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