Unverified Commit 17e44d29 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

app-router: add batching support for RSC router payloads (#48253)

This PR basically adds support for processing multiple router payloads.
Previously we were only handling one payload at a time but now that we
introduced parallel routes, we need to be able to render and return
separate parts of the layout separately.

Before, the single payload was generated in a DFS manner: we traversed
the router state and rendered the first segment of the tree that we
found needed rendering. In practice, this meant that we could miss
adjacent segments that might need to be re-rendered as well.

Now, we iterate all branches of the tree and return an array of flight
patches to be applied on the client correctly.

fixes NEXT-971

<!-- 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 or adding/fixing 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?

Closes NEXT-
Fixes #

-->
parent b9618ea6
...@@ -2,10 +2,9 @@ import { CacheNode, CacheStates } from '../../../shared/lib/app-router-context' ...@@ -2,10 +2,9 @@ import { CacheNode, CacheStates } from '../../../shared/lib/app-router-context'
import { FlightDataPath } from '../../../server/app-render/types' import { FlightDataPath } from '../../../server/app-render/types'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head' import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data' import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data'
import { ReadonlyReducerState } from './router-reducer-types'
export function applyFlightData( export function applyFlightData(
state: ReadonlyReducerState, existingCache: CacheNode,
cache: CacheNode, cache: CacheNode,
flightDataPath: FlightDataPath, flightDataPath: FlightDataPath,
wasPrefetched?: boolean wasPrefetched?: boolean
...@@ -23,7 +22,7 @@ export function applyFlightData( ...@@ -23,7 +22,7 @@ export function applyFlightData(
cache.subTreeData = subTreeData cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead( fillLazyItemsTillLeafWithHead(
cache, cache,
state.cache, existingCache,
treePatch, treePatch,
head, head,
wasPrefetched wasPrefetched
...@@ -31,10 +30,10 @@ export function applyFlightData( ...@@ -31,10 +30,10 @@ export function applyFlightData(
} else { } else {
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.status = CacheStates.READY cache.status = CacheStates.READY
cache.subTreeData = state.cache.subTreeData cache.subTreeData = existingCache.subTreeData
cache.parallelRoutes = new Map(state.cache.parallelRoutes) cache.parallelRoutes = new Map(existingCache.parallelRoutes)
// Create a copy of the existing cache with the subTreeData applied. // Create a copy of the existing cache with the subTreeData applied.
fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath) fillCacheWithNewSubTreeData(cache, existingCache, flightDataPath)
} }
return true return true
......
...@@ -54,9 +54,10 @@ function fastRefreshReducerImpl( ...@@ -54,9 +54,10 @@ function fastRefreshReducerImpl(
// Remove cache.data as it has been resolved at this point. // Remove cache.data as it has been resolved at this point.
cache.data = null cache.data = null
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths. let currentTree = state.tree
const flightDataPath = flightData[0] let currentCache = state.cache
for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned // FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) { if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better // TODO-APP: handle this case better
...@@ -69,7 +70,7 @@ function fastRefreshReducerImpl( ...@@ -69,7 +70,7 @@ function fastRefreshReducerImpl(
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
[''], [''],
state.tree, currentTree,
treePatch treePatch
) )
...@@ -77,7 +78,7 @@ function fastRefreshReducerImpl( ...@@ -77,7 +78,7 @@ function fastRefreshReducerImpl(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
if (isNavigatingToNewRootLayout(state.tree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush) return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
} }
...@@ -88,16 +89,19 @@ function fastRefreshReducerImpl( ...@@ -88,16 +89,19 @@ function fastRefreshReducerImpl(
if (canonicalUrlOverride) { if (canonicalUrlOverride) {
mutable.canonicalUrl = canonicalUrlOverrideHref mutable.canonicalUrl = canonicalUrlOverrideHref
} }
const applied = applyFlightData(state, cache, flightDataPath) const applied = applyFlightData(currentCache, cache, flightDataPath)
if (applied) { if (applied) {
mutable.cache = cache mutable.cache = cache
currentCache = cache
} }
mutable.previousTree = state.tree mutable.previousTree = currentTree
mutable.patchedTree = newTree mutable.patchedTree = newTree
mutable.canonicalUrl = href mutable.canonicalUrl = href
currentTree = newTree
}
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
......
...@@ -74,12 +74,15 @@ export function navigateReducer( ...@@ -74,12 +74,15 @@ export function navigateReducer(
return handleExternalUrl(state, mutable, flightData, pendingPush) return handleExternalUrl(state, mutable, flightData, pendingPush)
} }
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths. let currentTree = state.tree
const flightDataPath = flightData[0] let currentCache = state.cache
for (const flightDataPath of flightData) {
const flightSegmentPath = flightDataPath.slice( const flightSegmentPath = flightDataPath.slice(
0, 0,
-3 -3
) as unknown as FlightSegmentPath ) as unknown as FlightSegmentPath
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const [treePatch] = flightDataPath.slice(-3) const [treePatch] = flightDataPath.slice(-3)
...@@ -87,12 +90,12 @@ export function navigateReducer( ...@@ -87,12 +90,12 @@ export function navigateReducer(
let newTree = applyRouterStatePatchToTree( let newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
state.tree, currentTree,
treePatch treePatch
) )
// If the tree patch can't be applied to the current tree then we use the tree at time of prefetch // If the tree patch can't be applied to the current tree then we use the tree at time of prefetch
// TODO-APP: This should instead fill in the missing pieces in `state.tree` with the data from `treeAtTimeOfPrefetch`, then apply the patch. // TODO-APP: This should instead fill in the missing pieces in `currentTree` with the data from `treeAtTimeOfPrefetch`, then apply the patch.
if (newTree === null) { if (newTree === null) {
newTree = applyRouterStatePatchToTree( newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
...@@ -103,26 +106,31 @@ export function navigateReducer( ...@@ -103,26 +106,31 @@ export function navigateReducer(
} }
if (newTree !== null) { if (newTree !== null) {
if (isNavigatingToNewRootLayout(state.tree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush) return handleExternalUrl(state, mutable, href, pendingPush)
} }
const applied = applyFlightData(state, cache, flightDataPath, true) const applied = applyFlightData(
currentCache,
cache,
flightDataPath,
true
)
const hardNavigate = shouldHardNavigate( const hardNavigate = shouldHardNavigate(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
state.tree currentTree
) )
if (hardNavigate) { if (hardNavigate) {
cache.status = CacheStates.READY cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache. // Copy subTreeData for the root node of the cache.
cache.subTreeData = state.cache.subTreeData cache.subTreeData = currentCache.subTreeData
invalidateCacheBelowFlightSegmentPath( invalidateCacheBelowFlightSegmentPath(
cache, cache,
state.cache, currentCache,
flightSegmentPath flightSegmentPath
) )
// Ensure the existing cache value is used when the cache was not invalidated. // Ensure the existing cache value is used when the cache was not invalidated.
...@@ -131,8 +139,13 @@ export function navigateReducer( ...@@ -131,8 +139,13 @@ export function navigateReducer(
mutable.cache = cache mutable.cache = cache
} }
currentCache = cache
currentTree = newTree
}
}
mutable.previousTree = state.tree mutable.previousTree = state.tree
mutable.patchedTree = newTree mutable.patchedTree = currentTree
mutable.applyFocusAndScroll = true mutable.applyFocusAndScroll = true
mutable.canonicalUrl = canonicalUrlOverride mutable.canonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride) ? createHrefFromUrl(canonicalUrlOverride)
...@@ -142,7 +155,6 @@ export function navigateReducer( ...@@ -142,7 +155,6 @@ export function navigateReducer(
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
}
// When doing a hard push there can be two cases: with optimistic tree and without // When doing a hard push there can be two cases: with optimistic tree and without
// The with optimistic tree case only happens when the layouts have a loading state (loading.js) // The with optimistic tree case only happens when the layouts have a loading state (loading.js)
...@@ -206,9 +218,10 @@ export function navigateReducer( ...@@ -206,9 +218,10 @@ export function navigateReducer(
// Remove cache.data as it has been resolved at this point. // Remove cache.data as it has been resolved at this point.
cache.data = null cache.data = null
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths. let currentTree = state.tree
const flightDataPath = flightData[0] let currentCache = state.cache
for (const flightDataPath of flightData) {
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const [treePatch] = flightDataPath.slice(-3, -2) const [treePatch] = flightDataPath.slice(-3, -2)
...@@ -219,7 +232,7 @@ export function navigateReducer( ...@@ -219,7 +232,7 @@ export function navigateReducer(
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
state.tree, currentTree,
treePatch treePatch
) )
...@@ -227,7 +240,7 @@ export function navigateReducer( ...@@ -227,7 +240,7 @@ export function navigateReducer(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
if (isNavigatingToNewRootLayout(state.tree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush) return handleExternalUrl(state, mutable, href, pendingPush)
} }
...@@ -235,15 +248,19 @@ export function navigateReducer( ...@@ -235,15 +248,19 @@ export function navigateReducer(
? createHrefFromUrl(canonicalUrlOverride) ? createHrefFromUrl(canonicalUrlOverride)
: href : href
mutable.previousTree = state.tree mutable.previousTree = currentTree
mutable.patchedTree = newTree mutable.patchedTree = newTree
mutable.applyFocusAndScroll = true mutable.applyFocusAndScroll = true
mutable.pendingPush = pendingPush mutable.pendingPush = pendingPush
mutable.hashFragment = hash mutable.hashFragment = hash
const applied = applyFlightData(state, cache, flightDataPath) const applied = applyFlightData(currentCache, cache, flightDataPath)
if (applied) { if (applied) {
mutable.cache = cache mutable.cache = cache
currentCache = cache
}
currentTree = newTree
} }
return handleMutable(state, mutable) return handleMutable(state, mutable)
......
...@@ -54,9 +54,9 @@ export function refreshReducer( ...@@ -54,9 +54,9 @@ export function refreshReducer(
// Remove cache.data as it has been resolved at this point. // Remove cache.data as it has been resolved at this point.
cache.data = null cache.data = null
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths. let currentTree = state.tree
const flightDataPath = flightData[0]
for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned // FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) { if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better // TODO-APP: handle this case better
...@@ -69,7 +69,7 @@ export function refreshReducer( ...@@ -69,7 +69,7 @@ export function refreshReducer(
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
[''], [''],
state.tree, currentTree,
treePatch treePatch
) )
...@@ -77,7 +77,7 @@ export function refreshReducer( ...@@ -77,7 +77,7 @@ export function refreshReducer(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
if (isNavigatingToNewRootLayout(state.tree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush) return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
} }
...@@ -107,9 +107,12 @@ export function refreshReducer( ...@@ -107,9 +107,12 @@ export function refreshReducer(
mutable.prefetchCache = new Map() mutable.prefetchCache = new Map()
} }
mutable.previousTree = state.tree mutable.previousTree = currentTree
mutable.patchedTree = newTree mutable.patchedTree = newTree
mutable.canonicalUrl = href mutable.canonicalUrl = href
currentTree = newTree
}
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
...@@ -43,17 +43,18 @@ export function serverPatchReducer( ...@@ -43,17 +43,18 @@ export function serverPatchReducer(
) )
} }
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths. let currentTree = state.tree
const flightDataPath = flightData[0] let currentCache = state.cache
for (const flightDataPath of flightData) {
// Slices off the last segment (which is at -4) as it doesn't exist in the tree yet // Slices off the last segment (which is at -4) as it doesn't exist in the tree yet
const flightSegmentPath = flightDataPath.slice(0, -4) const flightSegmentPath = flightDataPath.slice(0, -4)
const [treePatch] = flightDataPath.slice(-3, -2)
const [treePatch] = flightDataPath.slice(-3, -2)
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
state.tree, currentTree,
treePatch treePatch
) )
...@@ -61,7 +62,7 @@ export function serverPatchReducer( ...@@ -61,7 +62,7 @@ export function serverPatchReducer(
throw new Error('SEGMENT MISMATCH') throw new Error('SEGMENT MISMATCH')
} }
if (isNavigatingToNewRootLayout(state.tree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl( return handleExternalUrl(
state, state,
mutable, mutable,
...@@ -78,11 +79,15 @@ export function serverPatchReducer( ...@@ -78,11 +79,15 @@ export function serverPatchReducer(
mutable.canonicalUrl = canonicalUrlOverrideHref mutable.canonicalUrl = canonicalUrlOverrideHref
} }
applyFlightData(state, cache, flightDataPath) applyFlightData(currentCache, cache, flightDataPath)
mutable.previousTree = state.tree mutable.previousTree = currentTree
mutable.patchedTree = newTree mutable.patchedTree = newTree
mutable.cache = cache mutable.cache = cache
currentCache = cache
currentTree = newTree
}
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
...@@ -988,24 +988,10 @@ export async function renderToHTMLOrFlight( ...@@ -988,24 +988,10 @@ export async function renderToHTMLOrFlight(
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
rootLayoutIncluded: boolean rootLayoutIncluded: boolean
asNotFound?: boolean asNotFound?: boolean
}): Promise<FlightDataPath> => { }): Promise<FlightDataPath[]> => {
const [segment, parallelRoutes, components] = loaderTreeToFilter const [segment, parallelRoutes, components] = loaderTreeToFilter
// if there's a refetch key, it'll probably take precedence when rendering const parallelRoutesKeys = Object.keys(parallelRoutes)
// so we sort the parallel routes so that the refetch key is first
// Sort the routes so that a route marked for refetching is at the beginning of the array
const parallelRoutesKeys = Object.keys(parallelRoutes).sort(
(parallelRouteKeyA, parallelRouteKeyB) => {
if (flightRouterState?.[1][parallelRouteKeyA][3] === 'refetch') {
return -1
}
if (flightRouterState?.[1][parallelRouteKeyB][3] === 'refetch') {
return 1
}
return 0
}
)
const { layout } = components const { layout } = components
const isLayout = typeof layout !== 'undefined' const isLayout = typeof layout !== 'undefined'
...@@ -1046,19 +1032,7 @@ export async function renderToHTMLOrFlight( ...@@ -1046,19 +1032,7 @@ export async function renderToHTMLOrFlight(
// Last item in the tree // Last item in the tree
parallelRoutesKeys.length === 0 || parallelRoutesKeys.length === 0 ||
// Explicit refresh // Explicit refresh
flightRouterState[3] === 'refetch' || flightRouterState[3] === 'refetch'
// if any of the parallel routes segments differ or have a refresh marker
Object.entries(parallelRoutes).some(
([parallelRouteKey, [parallelRouteSegment]]) => {
const [incomingParallelRouteSegment] =
flightRouterState[1][parallelRouteKey] ?? []
return (
incomingParallelRouteSegment === '__DEFAULT__' &&
parallelRouteSegment !== '__DEFAULT__'
)
}
)
if (!parentRendered && renderComponentsOnThisLevel) { if (!parentRendered && renderComponentsOnThisLevel) {
const overriddenSegment = const overriddenSegment =
...@@ -1066,8 +1040,8 @@ export async function renderToHTMLOrFlight( ...@@ -1066,8 +1040,8 @@ export async function renderToHTMLOrFlight(
canSegmentBeOverridden(actualSegment, flightRouterState[0]) canSegmentBeOverridden(actualSegment, flightRouterState[0])
? flightRouterState[0] ? flightRouterState[0]
: null : null
return [ return [
[
overriddenSegment ?? actualSegment, overriddenSegment ?? actualSegment,
createFlightRouterStateFromLoaderTree( createFlightRouterStateFromLoaderTree(
// Create router state using the slice of the loaderTree // Create router state using the slice of the loaderTree
...@@ -1100,7 +1074,10 @@ export async function renderToHTMLOrFlight( ...@@ -1100,7 +1074,10 @@ export async function renderToHTMLOrFlight(
return <Component /> return <Component />
}), }),
isPrefetch && !Boolean(components.loading) ? null : rscPayloadHead, isPrefetch && !Boolean(components.loading)
? null
: rscPayloadHead,
],
] ]
} }
...@@ -1131,7 +1108,10 @@ export async function renderToHTMLOrFlight( ...@@ -1131,7 +1108,10 @@ export async function renderToHTMLOrFlight(
} }
// Walk through all parallel routes. // Walk through all parallel routes.
for (const parallelRouteKey of parallelRoutesKeys) { const paths: FlightDataPath[] = (
await Promise.all(
parallelRoutesKeys.map(async (parallelRouteKey) => {
// for (const parallelRouteKey of parallelRoutesKeys) {
const parallelRoute = parallelRoutes[parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey]
const currentSegmentPath: FlightSegmentPath = isFirst const currentSegmentPath: FlightSegmentPath = isFirst
...@@ -1150,17 +1130,31 @@ export async function renderToHTMLOrFlight( ...@@ -1150,17 +1130,31 @@ export async function renderToHTMLOrFlight(
isFirst: false, isFirst: false,
rscPayloadHead, rscPayloadHead,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags:
injectedFontPreloadTagsWithCurrentLayout,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
asNotFound, asNotFound,
}) })
if (typeof path[path.length - 1] !== 'string') { return path
return [actualSegment, parallelRouteKey, ...path] .map((item) => {
} // we don't need to send over default routes in the flight data
// because they are always ignored by the client
if (
item[0] === '__DEFAULT__' &&
flightRouterState &&
!!flightRouterState[1][parallelRouteKey][0]
) {
return null
} }
return [actualSegment, parallelRouteKey, ...item]
})
.filter(Boolean) as FlightDataPath[]
})
)
).flat()
return [actualSegment] return paths
} }
const metadataItems = await resolveMetadata({ const metadataItems = await resolveMetadata({
...@@ -1170,8 +1164,7 @@ export async function renderToHTMLOrFlight( ...@@ -1170,8 +1164,7 @@ export async function renderToHTMLOrFlight(
}) })
// Flight data that is going to be passed to the browser. // Flight data that is going to be passed to the browser.
// Currently a single item array but in the future multiple patches might be combined in a single request. // Currently a single item array but in the future multiple patches might be combined in a single request.
const flightData: FlightData = [ const flightData: FlightData = (
(
await walkTreeWithFlightRouterState({ await walkTreeWithFlightRouterState({
createSegmentPath: (child) => child, createSegmentPath: (child) => child,
loaderTreeToFilter: loaderTree, loaderTreeToFilter: loaderTree,
...@@ -1195,8 +1188,7 @@ export async function renderToHTMLOrFlight( ...@@ -1195,8 +1188,7 @@ export async function renderToHTMLOrFlight(
rootLayoutIncluded: false, rootLayoutIncluded: false,
asNotFound: pathname === '/404', asNotFound: pathname === '/404',
}) })
).slice(1), ).map((path) => path.slice(1)) // remove the '' (root) segment
]
// For app dir, use the bundled version of Fizz renderer (renderToReadableStream) // For app dir, use the bundled version of Fizz renderer (renderToReadableStream)
// which contains the subset React. // which contains the subset React.
......
export default function Sidebar() {
return <aside id="nested-deeper-sidebar">Nested deeper sidebar here</aside>
}
export default function Sidebar() {
return <aside id="nested-sidebar">Nested sidebar here</aside>
}
export default function Sidebar() {
return <aside id="sidebar-main">root sidebar here</aside>
}
import React from 'react'
import Link from 'next/link'
export default function Layout({
children,
sidebar,
}: {
children: React.ReactNode
sidebar: React.ReactNode
}) {
return (
<>
<Link className="mr-4" href="/parallel-side-bar">
Home
</Link>
<Link className="mr-4" href="/parallel-side-bar/nested">
Nested
</Link>
<Link className="mr-4" href="/parallel-side-bar/nested/deeper">
Nested Deeper
</Link>
{sidebar}
{children}
</>
)
}
import React from 'react'
export default function Page() {
return <main id="nested-deeper-main">Nested deeper page</main>
}
import React from 'react'
export default function Page() {
return <main id="nested-main">Nested page</main>
}
import React from 'react'
export default function Home() {
return <main id="main">homepage</main>
}
...@@ -206,6 +206,43 @@ createNextDescribe( ...@@ -206,6 +206,43 @@ createNextDescribe(
const html = await res.text() const html = await res.text()
expect(html).toContain('page could not be found') expect(html).toContain('page could not be found')
}) })
it('should render nested parallel routes', async () => {
const browser = await next.browser('/parallel-side-bar/nested/deeper')
await check(
() => browser.waitForElementByCss('#nested-deeper-main').text(),
'Nested deeper page'
)
await check(
() => browser.waitForElementByCss('#nested-deeper-sidebar').text(),
'Nested deeper sidebar here'
)
await browser.elementByCss('[href="/parallel-side-bar/nested"]').click()
await check(
() => browser.waitForElementByCss('#nested-main').text(),
'Nested page'
)
await check(
() => browser.waitForElementByCss('#nested-sidebar').text(),
'Nested sidebar here'
)
await browser.elementByCss('[href="/parallel-side-bar"]').click()
await check(
() => browser.waitForElementByCss('#main').text(),
'homepage'
)
await check(
() => browser.waitForElementByCss('#sidebar-main').text(),
'root sidebar here'
)
})
}) })
describe('route intercepting', () => { describe('route intercepting', () => {
......
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment