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'
import { FlightDataPath } from '../../../server/app-render/types'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data'
import { ReadonlyReducerState } from './router-reducer-types'
export function applyFlightData(
state: ReadonlyReducerState,
existingCache: CacheNode,
cache: CacheNode,
flightDataPath: FlightDataPath,
wasPrefetched?: boolean
......@@ -23,7 +22,7 @@ export function applyFlightData(
cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead(
cache,
state.cache,
existingCache,
treePatch,
head,
wasPrefetched
......@@ -31,10 +30,10 @@ export function applyFlightData(
} else {
// Copy subTreeData for the root node of the cache.
cache.status = CacheStates.READY
cache.subTreeData = state.cache.subTreeData
cache.parallelRoutes = new Map(state.cache.parallelRoutes)
cache.subTreeData = existingCache.subTreeData
cache.parallelRoutes = new Map(existingCache.parallelRoutes)
// Create a copy of the existing cache with the subTreeData applied.
fillCacheWithNewSubTreeData(cache, state.cache, flightDataPath)
fillCacheWithNewSubTreeData(cache, existingCache, flightDataPath)
}
return true
......
......@@ -54,50 +54,54 @@ function fastRefreshReducerImpl(
// Remove cache.data as it has been resolved at this point.
cache.data = null
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths.
const flightDataPath = flightData[0]
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
console.log('REFRESH FAILED')
return state
}
let currentTree = state.tree
let currentCache = state.cache
for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
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.
const [treePatch] = flightDataPath
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
[''],
state.tree,
treePatch
)
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (isNavigatingToNewRootLayout(state.tree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
}
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
if (canonicalUrlOverride) {
mutable.canonicalUrl = canonicalUrlOverrideHref
}
const applied = applyFlightData(state, cache, flightDataPath)
if (canonicalUrlOverride) {
mutable.canonicalUrl = canonicalUrlOverrideHref
}
const applied = applyFlightData(currentCache, cache, flightDataPath)
if (applied) {
mutable.cache = cache
}
if (applied) {
mutable.cache = cache
currentCache = cache
}
mutable.previousTree = state.tree
mutable.patchedTree = newTree
mutable.canonicalUrl = href
mutable.previousTree = currentTree
mutable.patchedTree = newTree
mutable.canonicalUrl = href
currentTree = newTree
}
return handleMutable(state, mutable)
}
......
......@@ -74,74 +74,86 @@ export function navigateReducer(
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.
const flightDataPath = flightData[0]
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)
let currentTree = state.tree
let currentCache = state.cache
// Create new tree based on the flightSegmentPath and router state patch
let newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
state.tree,
treePatch
)
for (const flightDataPath of flightData) {
const flightSegmentPath = flightDataPath.slice(
0,
-3
) as unknown as FlightSegmentPath
// 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.
if (newTree === null) {
newTree = applyRouterStatePatchToTree(
// 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
let newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
treeAtTimeOfPrefetch,
currentTree,
treePatch
)
}
if (newTree !== null) {
if (isNavigatingToNewRootLayout(state.tree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush)
// 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 `currentTree` with the data from `treeAtTimeOfPrefetch`, then apply the patch.
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(
// TODO-APP: remove ''
['', ...flightSegmentPath],
state.tree
)
if (hardNavigate) {
cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache.
cache.subTreeData = state.cache.subTreeData
invalidateCacheBelowFlightSegmentPath(
const applied = applyFlightData(
currentCache,
cache,
state.cache,
flightSegmentPath
flightDataPath,
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
mutable.patchedTree = newTree
mutable.applyFocusAndScroll = true
mutable.canonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: href
mutable.pendingPush = pendingPush
mutable.hashFragment = hash
const hardNavigate = shouldHardNavigate(
// TODO-APP: remove ''
['', ...flightSegmentPath],
currentTree
)
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
......@@ -206,44 +218,49 @@ export function navigateReducer(
// Remove cache.data as it has been resolved at this point.
cache.data = null
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths.
const flightDataPath = flightData[0]
let currentTree = state.tree
let currentCache = state.cache
// The one before last item is the router state tree patch
const [treePatch] = flightDataPath.slice(-3, -2)
for (const flightDataPath of flightData) {
// 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
const flightSegmentPath = flightDataPath.slice(0, -4)
// Path without the last segment, router state, and the subTreeData
const flightSegmentPath = flightDataPath.slice(0, -4)
// Create new tree based on the flightSegmentPath and router state patch
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
state.tree,
treePatch
)
// Create new tree based on the flightSegmentPath and router state patch
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
['', ...flightSegmentPath],
currentTree,
treePatch
)
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (isNavigatingToNewRootLayout(state.tree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush)
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(state, mutable, href, pendingPush)
}
mutable.canonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: href
mutable.canonicalUrl = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: href
mutable.previousTree = state.tree
mutable.patchedTree = newTree
mutable.applyFocusAndScroll = true
mutable.pendingPush = pendingPush
mutable.hashFragment = hash
mutable.previousTree = currentTree
mutable.patchedTree = newTree
mutable.applyFocusAndScroll = true
mutable.pendingPush = pendingPush
mutable.hashFragment = hash
const applied = applyFlightData(currentCache, cache, flightDataPath)
if (applied) {
mutable.cache = cache
currentCache = cache
}
const applied = applyFlightData(state, cache, flightDataPath)
if (applied) {
mutable.cache = cache
currentTree = newTree
}
return handleMutable(state, mutable)
......
......@@ -54,62 +54,65 @@ export function refreshReducer(
// Remove cache.data as it has been resolved at this point.
cache.data = null
// TODO-APP: Currently the Flight data can only have one item but in the future it can have multiple paths.
const flightDataPath = flightData[0]
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
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 ''
[''],
state.tree,
treePatch
)
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (isNavigatingToNewRootLayout(state.tree, newTree)) {
return handleExternalUrl(state, mutable, href, state.pushRef.pendingPush)
}
let currentTree = state.tree
for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) {
// TODO-APP: handle this case better
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
)
const canonicalUrlOverrideHref = canonicalUrlOverride
? createHrefFromUrl(canonicalUrlOverride)
: undefined
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
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.canonicalUrl = canonicalUrlOverrideHref
}
mutable.previousTree = currentTree
mutable.patchedTree = newTree
mutable.canonicalUrl = href
// 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()
currentTree = newTree
}
mutable.previousTree = state.tree
mutable.patchedTree = newTree
mutable.canonicalUrl = href
return handleMutable(state, mutable)
}
......@@ -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.
const flightDataPath = flightData[0]
// 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')
}
let currentTree = state.tree
let currentCache = state.cache
if (isNavigatingToNewRootLayout(state.tree, newTree)) {
return handleExternalUrl(
state,
mutable,
state.canonicalUrl,
state.pushRef.pendingPush
for (const flightDataPath of flightData) {
// 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],
currentTree,
treePatch
)
}
const canonicalUrlOverrideHref = overrideCanonicalUrl
? createHrefFromUrl(overrideCanonicalUrl)
: undefined
if (newTree === null) {
throw new Error('SEGMENT MISMATCH')
}
if (canonicalUrlOverrideHref) {
mutable.canonicalUrl = canonicalUrlOverrideHref
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
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
mutable.patchedTree = newTree
mutable.cache = cache
applyFlightData(currentCache, cache, flightDataPath)
mutable.previousTree = currentTree
mutable.patchedTree = newTree
mutable.cache = cache
currentCache = cache
currentTree = newTree
}
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(
const html = await res.text()
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', () => {
......
{
"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