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,50 +54,54 @@ function fastRefreshReducerImpl( ...@@ -54,50 +54,54 @@ 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
// FlightDataPath with more than two items means unexpected Flight data was returned for (const flightDataPath of flightData) {
if (flightDataPath.length !== 3) { // FlightDataPath with more than two items means unexpected Flight data was returned
// TODO-APP: handle this case better if (flightDataPath.length !== 3) {
console.log('REFRESH FAILED') // TODO-APP: handle this case better
return state console.log('REFRESH FAILED')
} return state
}
// Given the path can only have two items the items are only the router state and subTreeData for the root.
const [treePatch] = flightDataPath
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
[''],
currentTree,
treePatch
)
// Given the path can only have two items the items are only the router state and subTreeData for the root. if (newTree === null) {
const [treePatch] = flightDataPath throw new Error('SEGMENT MISMATCH')
const newTree = applyRouterStatePatchToTree( }
// TODO-APP: remove ''
[''],
state.tree,
treePatch
)
if (newTree === null) {
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)
} }
const canonicalUrlOverrideHref = canonicalUrlOverride const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride) ? createHrefFromUrl(canonicalUrlOverride)
: undefined : undefined
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,74 +74,86 @@ export function navigateReducer( ...@@ -74,74 +74,86 @@ 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
const flightSegmentPath = flightDataPath.slice(
0,
-3
) as unknown as FlightSegmentPath
// The one before last item is the router state tree patch
const [treePatch] = flightDataPath.slice(-3)
// Create new tree based on the flightSegmentPath and router state patch for (const flightDataPath of flightData) {
let newTree = applyRouterStatePatchToTree( const flightSegmentPath = flightDataPath.slice(
// TODO-APP: remove '' 0,
['', ...flightSegmentPath], -3
state.tree, ) as unknown as FlightSegmentPath
treePatch
)
// If the tree patch can't be applied to the current tree then we use the tree at time of prefetch // The one before last item is the router state tree patch
// TODO-APP: This should instead fill in the missing pieces in `state.tree` with the data from `treeAtTimeOfPrefetch`, then apply the patch. const [treePatch] = flightDataPath.slice(-3)
if (newTree === null) {
newTree = applyRouterStatePatchToTree( // Create new tree based on the flightSegmentPath and router state patch
let newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
treeAtTimeOfPrefetch, currentTree,
treePatch treePatch
) )
}
if (newTree !== null) { // If the tree patch can't be applied to the current tree then we use the tree at time of prefetch
if (isNavigatingToNewRootLayout(state.tree, newTree)) { // TODO-APP: This should instead fill in the missing pieces in `currentTree` with the data from `treeAtTimeOfPrefetch`, then apply the patch.
return handleExternalUrl(state, mutable, href, pendingPush) if (newTree === null) {
newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
treeAtTimeOfPrefetch,
treePatch
)
} }
const applied = applyFlightData(state, cache, flightDataPath, true) if (newTree !== null) {
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush)
}
const hardNavigate = shouldHardNavigate( const applied = applyFlightData(
// TODO-APP: remove '' currentCache,
['', ...flightSegmentPath],
state.tree
)
if (hardNavigate) {
cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache.
cache.subTreeData = state.cache.subTreeData
invalidateCacheBelowFlightSegmentPath(
cache, cache,
state.cache, flightDataPath,
flightSegmentPath true
) )
// Ensure the existing cache value is used when the cache was not invalidated.
mutable.cache = cache
} else if (applied) {
mutable.cache = cache
}
mutable.previousTree = state.tree const hardNavigate = shouldHardNavigate(
mutable.patchedTree = newTree // TODO-APP: remove ''
mutable.applyFocusAndScroll = true ['', ...flightSegmentPath],
mutable.canonicalUrl = canonicalUrlOverride currentTree
? createHrefFromUrl(canonicalUrlOverride) )
: href
mutable.pendingPush = pendingPush
mutable.hashFragment = hash
return handleMutable(state, mutable) if (hardNavigate) {
cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache.
cache.subTreeData = currentCache.subTreeData
invalidateCacheBelowFlightSegmentPath(
cache,
currentCache,
flightSegmentPath
)
// Ensure the existing cache value is used when the cache was not invalidated.
mutable.cache = cache
} else if (applied) {
mutable.cache = cache
}
currentCache = cache
currentTree = newTree
}
} }
mutable.previousTree = state.tree
mutable.patchedTree = currentTree
mutable.applyFocusAndScroll = true
mutable.canonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: href
mutable.pendingPush = pendingPush
mutable.hashFragment = hash
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
...@@ -206,44 +218,49 @@ export function navigateReducer( ...@@ -206,44 +218,49 @@ 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
// The one before last item is the router state tree patch for (const flightDataPath of flightData) {
const [treePatch] = flightDataPath.slice(-3, -2) // The one before last item is the router state tree patch
const [treePatch] = flightDataPath.slice(-3, -2)
// Path without the last segment, router state, and the subTreeData // Path without the last segment, router state, and the subTreeData
const flightSegmentPath = flightDataPath.slice(0, -4) const flightSegmentPath = flightDataPath.slice(0, -4)
// Create new tree based on the flightSegmentPath and router state patch // Create new tree based on the flightSegmentPath and router state patch
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
state.tree, currentTree,
treePatch treePatch
) )
if (newTree === null) { if (newTree === null) {
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)
} }
mutable.canonicalUrl = canonicalUrlOverride mutable.canonicalUrl = canonicalUrlOverride
? 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(currentCache, cache, flightDataPath)
if (applied) {
mutable.cache = cache
currentCache = cache
}
const applied = applyFlightData(state, cache, flightDataPath) currentTree = newTree
if (applied) {
mutable.cache = cache
} }
return handleMutable(state, mutable) return handleMutable(state, mutable)
......
...@@ -54,62 +54,65 @@ export function refreshReducer( ...@@ -54,62 +54,65 @@ 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
console.log('REFRESH FAILED') console.log('REFRESH FAILED')
return state return state
} }
// Given the path can only have two items the items are only the router state and subTreeData for the root. // Given the path can only have two items the items are only the router state and subTreeData for the root.
const [treePatch] = flightDataPath const [treePatch] = flightDataPath
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
[''], [''],
state.tree, currentTree,
treePatch treePatch
) )
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (isNavigatingToNewRootLayout(state.tree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
}
const canonicalUrlOverrideHref = canonicalUrlOverride if (newTree === null) {
? createHrefFromUrl(canonicalUrlOverride) throw new Error('SEGMENT MISMATCH')
: undefined }
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
}
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
if (canonicalUrlOverride) {
mutable.canonicalUrl = canonicalUrlOverrideHref
}
// The one before last item is the router state tree patch
const [subTreeData, head] = flightDataPath.slice(-2)
// Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) {
cache.status = CacheStates.READY
cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(
cache,
// Existing cache is not passed in as `router.refresh()` has to invalidate the entire cache.
undefined,
treePatch,
head
)
mutable.cache = cache
mutable.prefetchCache = new Map()
}
if (canonicalUrlOverride) { mutable.previousTree = currentTree
mutable.canonicalUrl = canonicalUrlOverrideHref mutable.patchedTree = newTree
} mutable.canonicalUrl = href
// The one before last item is the router state tree patch currentTree = newTree
const [subTreeData, head] = flightDataPath.slice(-2)
// Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) {
cache.status = CacheStates.READY
cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(
cache,
// Existing cache is not passed in as `router.refresh()` has to invalidate the entire cache.
undefined,
treePatch,
head
)
mutable.cache = cache
mutable.prefetchCache = new Map()
} }
mutable.previousTree = state.tree
mutable.patchedTree = newTree
mutable.canonicalUrl = href
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
...@@ -43,46 +43,51 @@ export function serverPatchReducer( ...@@ -43,46 +43,51 @@ 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
// 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 [treePatch] = flightDataPath.slice(-3, -2)
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
state.tree,
treePatch
)
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (isNavigatingToNewRootLayout(state.tree, newTree)) { for (const flightDataPath of flightData) {
return handleExternalUrl( // Slices off the last segment (which is at -4) as it doesn't exist in the tree yet
state, const flightSegmentPath = flightDataPath.slice(0, -4)
mutable,
state.canonicalUrl, const [treePatch] = flightDataPath.slice(-3, -2)
state.pushRef.pendingPush const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
currentTree,
treePatch
) )
}
const canonicalUrlOverrideHref = overrideCanonicalUrl if (newTree === null) {
? createHrefFromUrl(overrideCanonicalUrl) throw new Error('SEGMENT MISMATCH')
: undefined }
if (canonicalUrlOverrideHref) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
mutable.canonicalUrl = canonicalUrlOverrideHref return handleExternalUrl(
} state,
mutable,
state.canonicalUrl,
state.pushRef.pendingPush
)
}
const canonicalUrlOverrideHref = overrideCanonicalUrl
? createHrefFromUrl(overrideCanonicalUrl)
: undefined
applyFlightData(state, cache, flightDataPath) if (canonicalUrlOverrideHref) {
mutable.canonicalUrl = canonicalUrlOverrideHref
}
mutable.previousTree = state.tree applyFlightData(currentCache, cache, flightDataPath)
mutable.patchedTree = newTree
mutable.cache = cache mutable.previousTree = currentTree
mutable.patchedTree = newTree
mutable.cache = cache
currentCache = cache
currentTree = newTree
}
return handleMutable(state, mutable) return handleMutable(state, mutable)
} }
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