Unverified Commit f93ae7c8 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix TypeError edge-case for parallel slots rendered multiple times (#64271)

### What
When rendering a parallel slot multiple times in a single layout, in
conjunction with using an error boundary, the following TypeError is
thrown:

> Cannot destructure property 'parallelRouterKey' of 'param' as it is
null

### Why
I'm not 100% sure of the reason, but I believe this is because of how
React attempts to dededupe (more specifically, "detriplficate") objects
that it sees getting passed across the RSC -> client component boundary
(and an error boundary is necessarily a client component). When React
sees the same object twice, it'll create a reference to that object and
then use that reference in future places where it sees the object. My
assumption is that there's a bug somewhere here, as the `LayoutRouter`
component for the subsequent duplicated parallel slots (after the first
one) have no props, hence the TypeError.

### How
Rather than passing the error component as a prop to `LayoutRouter`,
this puts it as part of the `CacheNodeSeedData` data structure. This is
more aligned with other properties anyway (such as `loading` and `rsc`
for each segment), and seems to work around this bug as the
`initialSeedData` prop is only passed from RSC->client once.

EDIT: Confirmed this is also fixed after syncing the latest React, due
to https://github.com/facebook/react/pull/28669

Fixes #58485
Closes NEXT-2095
parent ddf2af59
...@@ -187,6 +187,7 @@ export function createEmptyCacheNode(): CacheNode { ...@@ -187,6 +187,7 @@ export function createEmptyCacheNode(): CacheNode {
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
} }
} }
...@@ -626,8 +627,9 @@ function Router({ ...@@ -626,8 +627,9 @@ function Router({
// Provided in AppTreeContext to ensure it can be overwritten in layout-router // Provided in AppTreeContext to ensure it can be overwritten in layout-router
url: canonicalUrl, url: canonicalUrl,
loading: cache.loading, loading: cache.loading,
error: cache.error,
} }
}, [cache.parallelRoutes, tree, canonicalUrl, cache.loading]) }, [cache.parallelRoutes, tree, canonicalUrl, cache.loading, cache.error])
const globalLayoutRouterContext = useMemo(() => { const globalLayoutRouterContext = useMemo(() => {
return { return {
......
...@@ -9,7 +9,6 @@ import type { ...@@ -9,7 +9,6 @@ import type {
FlightSegmentPath, FlightSegmentPath,
Segment, Segment,
} from '../../server/app-render/types' } from '../../server/app-render/types'
import type { ErrorComponent } from './error-boundary'
import type { FocusAndScrollRef } from './router-reducer/router-reducer-types' import type { FocusAndScrollRef } from './router-reducer/router-reducer-types'
import React, { import React, {
...@@ -358,6 +357,7 @@ function InnerLayoutRouter({ ...@@ -358,6 +357,7 @@ function InnerLayoutRouter({
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
} }
/** /**
...@@ -453,6 +453,7 @@ function InnerLayoutRouter({ ...@@ -453,6 +453,7 @@ function InnerLayoutRouter({
// TODO-APP: overriding of url for parallel routes // TODO-APP: overriding of url for parallel routes
url: url, url: url,
loading: childNode.loading, loading: childNode.loading,
error: childNode.error,
}} }}
> >
{resolvedRsc} {resolvedRsc}
...@@ -507,9 +508,6 @@ function LoadingBoundary({ ...@@ -507,9 +508,6 @@ function LoadingBoundary({
export default function OuterLayoutRouter({ export default function OuterLayoutRouter({
parallelRouterKey, parallelRouterKey,
segmentPath, segmentPath,
error,
errorStyles,
errorScripts,
templateStyles, templateStyles,
templateScripts, templateScripts,
template, template,
...@@ -519,9 +517,6 @@ export default function OuterLayoutRouter({ ...@@ -519,9 +517,6 @@ export default function OuterLayoutRouter({
}: { }: {
parallelRouterKey: string parallelRouterKey: string
segmentPath: FlightSegmentPath segmentPath: FlightSegmentPath
error: ErrorComponent | undefined
errorStyles: React.ReactNode | undefined
errorScripts: React.ReactNode | undefined
templateStyles: React.ReactNode | undefined templateStyles: React.ReactNode | undefined
templateScripts: React.ReactNode | undefined templateScripts: React.ReactNode | undefined
template: React.ReactNode template: React.ReactNode
...@@ -534,7 +529,7 @@ export default function OuterLayoutRouter({ ...@@ -534,7 +529,7 @@ export default function OuterLayoutRouter({
throw new Error('invariant expected layout router to be mounted') throw new Error('invariant expected layout router to be mounted')
} }
const { childNodes, tree, url, loading } = context const { childNodes, tree, url, loading, error } = context
// Get the current parallelRouter cache node // Get the current parallelRouter cache node
let childNodesForParallelRouter = childNodes.get(parallelRouterKey) let childNodesForParallelRouter = childNodes.get(parallelRouterKey)
...@@ -580,9 +575,9 @@ export default function OuterLayoutRouter({ ...@@ -580,9 +575,9 @@ export default function OuterLayoutRouter({
value={ value={
<ScrollAndFocusHandler segmentPath={segmentPath}> <ScrollAndFocusHandler segmentPath={segmentPath}>
<ErrorBoundary <ErrorBoundary
errorComponent={error} errorComponent={error?.[0]}
errorStyles={errorStyles} errorStyles={error?.[1]}
errorScripts={errorScripts} errorScripts={error?.[2]}
> >
<LoadingBoundary <LoadingBoundary
hasLoading={Boolean(loading)} hasLoading={Boolean(loading)}
......
...@@ -21,6 +21,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -21,6 +21,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
} }
const existingCache: CacheNode = { const existingCache: CacheNode = {
lazyData: null, lazyData: null,
...@@ -30,6 +31,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -30,6 +31,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -44,6 +46,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -44,6 +46,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -59,6 +62,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -59,6 +62,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
}, },
], ],
]), ]),
...@@ -75,6 +79,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -75,6 +79,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
expect(cache).toMatchInlineSnapshot(` expect(cache).toMatchInlineSnapshot(`
{ {
"error": null,
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
...@@ -82,6 +87,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -82,6 +87,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
"parallelRoutes": Map { "parallelRoutes": Map {
"children" => Map { "children" => Map {
"linking" => { "linking" => {
"error": null,
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
...@@ -89,6 +95,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -89,6 +95,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
"parallelRoutes": Map { "parallelRoutes": Map {
"children" => Map { "children" => Map {
"" => { "" => {
"error": null,
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
...@@ -109,6 +116,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -109,6 +116,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
</React.Fragment>, </React.Fragment>,
}, },
"dashboard" => { "dashboard" => {
"error": null,
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
......
...@@ -44,6 +44,7 @@ export function clearCacheNodeDataForSegmentPath( ...@@ -44,6 +44,7 @@ export function clearCacheNodeDataForSegmentPath(
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
}) })
} }
return return
...@@ -61,6 +62,7 @@ export function clearCacheNodeDataForSegmentPath( ...@@ -61,6 +62,7 @@ export function clearCacheNodeDataForSegmentPath(
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
}) })
} }
return return
...@@ -76,6 +78,7 @@ export function clearCacheNodeDataForSegmentPath( ...@@ -76,6 +78,7 @@ export function clearCacheNodeDataForSegmentPath(
parallelRoutes: new Map(childCacheNode.parallelRoutes), parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: childCacheNode.lazyDataResolved, lazyDataResolved: childCacheNode.lazyDataResolved,
loading: childCacheNode.loading, loading: childCacheNode.loading,
error: childCacheNode.error,
} as CacheNode } as CacheNode
childSegmentMap.set(cacheKey, childCacheNode) childSegmentMap.set(cacheKey, childCacheNode)
} }
......
...@@ -37,7 +37,7 @@ describe('createInitialRouterState', () => { ...@@ -37,7 +37,7 @@ describe('createInitialRouterState', () => {
buildId, buildId,
initialTree, initialTree,
initialCanonicalUrl, initialCanonicalUrl,
initialSeedData: ['', {}, children, null], initialSeedData: ['', {}, children, null, null],
initialParallelRoutes, initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any, location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>, initialHead: <title>Test</title>,
...@@ -48,7 +48,7 @@ describe('createInitialRouterState', () => { ...@@ -48,7 +48,7 @@ describe('createInitialRouterState', () => {
buildId, buildId,
initialTree, initialTree,
initialCanonicalUrl, initialCanonicalUrl,
initialSeedData: ['', {}, children, null], initialSeedData: ['', {}, children, null, null],
initialParallelRoutes, initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any, location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>, initialHead: <title>Test</title>,
...@@ -62,6 +62,7 @@ describe('createInitialRouterState', () => { ...@@ -62,6 +62,7 @@ describe('createInitialRouterState', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -81,6 +82,7 @@ describe('createInitialRouterState', () => { ...@@ -81,6 +82,7 @@ describe('createInitialRouterState', () => {
prefetchRsc: null, prefetchRsc: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
error: null,
head: <title>Test</title>, head: <title>Test</title>,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
...@@ -96,6 +98,7 @@ describe('createInitialRouterState', () => { ...@@ -96,6 +98,7 @@ describe('createInitialRouterState', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
}, },
], ],
]), ]),
......
...@@ -47,6 +47,7 @@ export function createInitialRouterState({ ...@@ -47,6 +47,7 @@ export function createInitialRouterState({
parallelRoutes: isServer ? new Map() : initialParallelRoutes, parallelRoutes: isServer ? new Map() : initialParallelRoutes,
lazyDataResolved: false, lazyDataResolved: false,
loading: initialSeedData[3], loading: initialSeedData[3],
error: initialSeedData[4],
} }
const canonicalUrl = const canonicalUrl =
......
...@@ -33,6 +33,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -33,6 +33,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -44,6 +45,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -44,6 +45,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -58,6 +60,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -58,6 +60,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -72,6 +75,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -72,6 +75,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -104,6 +108,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -104,6 +108,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -118,6 +123,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -118,6 +123,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -133,6 +139,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -133,6 +139,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -144,6 +151,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -144,6 +151,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
......
...@@ -49,6 +49,7 @@ export function fillCacheWithNewSubTreeData( ...@@ -49,6 +49,7 @@ export function fillCacheWithNewSubTreeData(
const seedData: CacheNodeSeedData = flightDataPath[3] const seedData: CacheNodeSeedData = flightDataPath[3]
const rsc = seedData[2] const rsc = seedData[2]
const loading = seedData[3] const loading = seedData[3]
const error = seedData[4]
childCacheNode = { childCacheNode = {
lazyData: null, lazyData: null,
rsc, rsc,
...@@ -56,6 +57,7 @@ export function fillCacheWithNewSubTreeData( ...@@ -56,6 +57,7 @@ export function fillCacheWithNewSubTreeData(
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading, loading,
error,
// Ensure segments other than the one we got data for are preserved. // Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode parallelRoutes: existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes) ? new Map(existingChildCacheNode.parallelRoutes)
...@@ -101,6 +103,7 @@ export function fillCacheWithNewSubTreeData( ...@@ -101,6 +103,7 @@ export function fillCacheWithNewSubTreeData(
parallelRoutes: new Map(childCacheNode.parallelRoutes), parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: false, lazyDataResolved: false,
loading: childCacheNode.loading, loading: childCacheNode.loading,
error: childCacheNode.error,
} as CacheNode } as CacheNode
childSegmentMap.set(cacheKey, childCacheNode) childSegmentMap.set(cacheKey, childCacheNode)
} }
......
...@@ -44,6 +44,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -44,6 +44,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
} }
const existingCache: CacheNode = { const existingCache: CacheNode = {
lazyData: null, lazyData: null,
...@@ -53,6 +54,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -53,6 +54,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -67,6 +69,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -67,6 +69,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -81,6 +84,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -81,6 +84,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -119,6 +123,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -119,6 +123,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -133,6 +138,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -133,6 +138,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -143,6 +149,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -143,6 +149,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -155,6 +162,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -155,6 +162,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchRsc: null, prefetchRsc: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
head: ( head: (
...@@ -182,6 +190,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -182,6 +190,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
}, },
......
...@@ -57,6 +57,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -57,6 +57,7 @@ export function fillLazyItemsTillLeafWithHead(
// New data was sent from the server. // New data was sent from the server.
const seedNode = parallelSeedData[2] const seedNode = parallelSeedData[2]
const loading = parallelSeedData[3] const loading = parallelSeedData[3]
const error = parallelSeedData[4]
newCacheNode = { newCacheNode = {
lazyData: null, lazyData: null,
rsc: seedNode, rsc: seedNode,
...@@ -69,6 +70,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -69,6 +70,7 @@ export function fillLazyItemsTillLeafWithHead(
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading, loading,
error,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes), parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -100,6 +102,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -100,6 +102,7 @@ export function fillLazyItemsTillLeafWithHead(
parallelRoutes: new Map(existingCacheNode?.parallelRoutes), parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
} }
} }
...@@ -125,6 +128,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -125,6 +128,7 @@ export function fillLazyItemsTillLeafWithHead(
// New data was sent from the server. // New data was sent from the server.
const seedNode = parallelSeedData[2] const seedNode = parallelSeedData[2]
const loading = parallelSeedData[3] const loading = parallelSeedData[3]
const error = parallelSeedData[4]
newCacheNode = { newCacheNode = {
lazyData: null, lazyData: null,
rsc: seedNode, rsc: seedNode,
...@@ -134,6 +138,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -134,6 +138,7 @@ export function fillLazyItemsTillLeafWithHead(
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading, loading,
error,
} }
} else { } else {
// No data available for this node. This will trigger a lazy fetch // No data available for this node. This will trigger a lazy fetch
...@@ -147,6 +152,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -147,6 +152,7 @@ export function fillLazyItemsTillLeafWithHead(
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
} }
} }
......
...@@ -34,6 +34,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -34,6 +34,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -45,6 +46,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -45,6 +46,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -59,6 +61,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -59,6 +61,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -73,6 +76,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -73,6 +76,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -115,6 +119,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -115,6 +119,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -127,6 +132,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -127,6 +132,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -136,6 +142,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -136,6 +142,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
{ {
lazyData: null, lazyData: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
rsc: <React.Fragment>Page</React.Fragment>, rsc: <React.Fragment>Page</React.Fragment>,
prefetchRsc: null, prefetchRsc: null,
......
...@@ -12,6 +12,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -12,6 +12,7 @@ describe('invalidateCacheByRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -23,6 +24,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -23,6 +24,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -37,6 +39,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -37,6 +39,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -51,6 +54,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -51,6 +54,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -94,6 +98,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -94,6 +98,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([['children', new Map()]]), parallelRoutes: new Map([['children', new Map()]]),
} }
......
...@@ -234,6 +234,7 @@ export function updateCacheNodeOnNavigation( ...@@ -234,6 +234,7 @@ export function updateCacheNodeOnNavigation(
head: oldCacheNode.head, head: oldCacheNode.head,
prefetchHead: oldCacheNode.prefetchHead, prefetchHead: oldCacheNode.prefetchHead,
loading: oldCacheNode.loading, loading: oldCacheNode.loading,
error: oldCacheNode.error,
// Everything is cloned except for the children, which we computed above. // Everything is cloned except for the children, which we computed above.
parallelRoutes: prefetchParallelRoutes, parallelRoutes: prefetchParallelRoutes,
...@@ -514,6 +515,8 @@ function createPendingCacheNode( ...@@ -514,6 +515,8 @@ function createPendingCacheNode(
const maybePrefetchRsc = prefetchData !== null ? prefetchData[2] : null const maybePrefetchRsc = prefetchData !== null ? prefetchData[2] : null
const maybePrefetchLoading = prefetchData !== null ? prefetchData[3] : null const maybePrefetchLoading = prefetchData !== null ? prefetchData[3] : null
const maybePrefetchError = prefetchData !== null ? prefetchData[4] : null
return { return {
lazyData: null, lazyData: null,
parallelRoutes: parallelRoutes, parallelRoutes: parallelRoutes,
...@@ -521,6 +524,7 @@ function createPendingCacheNode( ...@@ -521,6 +524,7 @@ function createPendingCacheNode(
prefetchRsc: maybePrefetchRsc !== undefined ? maybePrefetchRsc : null, prefetchRsc: maybePrefetchRsc !== undefined ? maybePrefetchRsc : null,
prefetchHead: isLeafSegment ? prefetchHead : null, prefetchHead: isLeafSegment ? prefetchHead : null,
loading: maybePrefetchLoading !== undefined ? maybePrefetchLoading : null, loading: maybePrefetchLoading !== undefined ? maybePrefetchLoading : null,
error: maybePrefetchError !== undefined ? maybePrefetchError : null,
// Create a deferred promise. This will be fulfilled once the dynamic // Create a deferred promise. This will be fulfilled once the dynamic
// response is received from the server. // response is received from the server.
...@@ -765,6 +769,7 @@ export function updateCacheNodeOnPopstateRestoration( ...@@ -765,6 +769,7 @@ export function updateCacheNodeOnPopstateRestoration(
prefetchHead: shouldUsePrefetch ? oldCacheNode.prefetchHead : null, prefetchHead: shouldUsePrefetch ? oldCacheNode.prefetchHead : null,
prefetchRsc: shouldUsePrefetch ? oldCacheNode.prefetchRsc : null, prefetchRsc: shouldUsePrefetch ? oldCacheNode.prefetchRsc : null,
loading: shouldUsePrefetch ? oldCacheNode.loading : null, loading: shouldUsePrefetch ? oldCacheNode.loading : null,
error: shouldUsePrefetch ? oldCacheNode.error : null,
// These are the cloned children we computed above // These are the cloned children we computed above
parallelRoutes: newParallelRoutes, parallelRoutes: newParallelRoutes,
......
...@@ -33,6 +33,7 @@ describe('findHeadInCache', () => { ...@@ -33,6 +33,7 @@ describe('findHeadInCache', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -47,6 +48,7 @@ describe('findHeadInCache', () => { ...@@ -47,6 +48,7 @@ describe('findHeadInCache', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -59,6 +61,7 @@ describe('findHeadInCache', () => { ...@@ -59,6 +61,7 @@ describe('findHeadInCache', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
error: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -72,6 +75,7 @@ describe('findHeadInCache', () => { ...@@ -72,6 +75,7 @@ describe('findHeadInCache', () => {
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null, loading: null,
error: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
head: ( head: (
<> <>
......
...@@ -541,6 +541,7 @@ async function ReactServerError({ ...@@ -541,6 +541,7 @@ async function ReactServerError({
<body></body> <body></body>
</html>, </html>,
null, null,
null,
] ]
return ( return (
<AppRouter <AppRouter
......
...@@ -5,7 +5,9 @@ import type { AppRenderContext } from './app-render' ...@@ -5,7 +5,9 @@ import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string' import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path' import { encodeURIPath } from '../../shared/lib/encode-uri-path'
export async function createComponentStylesAndScripts({ export async function createComponentStylesAndScripts<
T = React.ComponentType<unknown>
>({
filePath, filePath,
getComponent, getComponent,
injectedCSS, injectedCSS,
...@@ -17,7 +19,7 @@ export async function createComponentStylesAndScripts({ ...@@ -17,7 +19,7 @@ export async function createComponentStylesAndScripts({
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string> injectedJS: Set<string>
ctx: AppRenderContext ctx: AppRenderContext
}): Promise<[React.ComponentType<any>, React.ReactNode, React.ReactNode]> { }): Promise<[T, React.ReactNode, React.ReactNode]> {
const { styles: cssHrefs, scripts: jsHrefs } = getLinkAndScriptTags( const { styles: cssHrefs, scripts: jsHrefs } = getLinkAndScriptTags(
ctx.clientReferenceManifest, ctx.clientReferenceManifest,
filePath, filePath,
......
...@@ -14,7 +14,11 @@ import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/parallel-ro ...@@ -14,7 +14,11 @@ import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/parallel-ro
import { getTracer } from '../lib/trace/tracer' import { getTracer } from '../lib/trace/tracer'
import { NextNodeServerSpan } from '../lib/trace/constants' import { NextNodeServerSpan } from '../lib/trace/constants'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime' import type {
ErrorModuleData,
LoadingModuleData,
} from '../../shared/lib/app-router-context.shared-runtime'
import type { ErrorComponent } from '../../client/components/error-boundary'
type ComponentTree = { type ComponentTree = {
seedData: CacheNodeSeedData seedData: CacheNodeSeedData
...@@ -127,7 +131,7 @@ async function createComponentTreeInternal({ ...@@ -127,7 +131,7 @@ async function createComponentTreeInternal({
: [React.Fragment] : [React.Fragment]
const [ErrorComponent, errorStyles, errorScripts] = error const [ErrorComponent, errorStyles, errorScripts] = error
? await createComponentStylesAndScripts({ ? await createComponentStylesAndScripts<ErrorComponent>({
ctx, ctx,
filePath: error[1], filePath: error[1],
getComponent: error[0], getComponent: error[0],
...@@ -459,9 +463,6 @@ async function createComponentTreeInternal({ ...@@ -459,9 +463,6 @@ async function createComponentTreeInternal({
parallelRouterKey={parallelRouteKey} parallelRouterKey={parallelRouteKey}
segmentPath={createSegmentPath(currentSegmentPath)} segmentPath={createSegmentPath(currentSegmentPath)}
// TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning. // TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning.
error={ErrorComponent}
errorStyles={errorStyles}
errorScripts={errorScripts}
template={ template={
<Template> <Template>
<RenderFromTemplateContext /> <RenderFromTemplateContext />
...@@ -494,6 +495,10 @@ async function createComponentTreeInternal({ ...@@ -494,6 +495,10 @@ async function createComponentTreeInternal({
? [<Loading />, loadingStyles, loadingScripts] ? [<Loading />, loadingStyles, loadingScripts]
: null : null
const errorData: ErrorModuleData = ErrorComponent
? [ErrorComponent, errorStyles, errorScripts]
: null
// When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component // When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component
if (!Component) { if (!Component) {
return { return {
...@@ -507,6 +512,7 @@ async function createComponentTreeInternal({ ...@@ -507,6 +512,7 @@ async function createComponentTreeInternal({
// caution, for now. // caution, for now.
<>{parallelRouteProps.children}</>, <>{parallelRouteProps.children}</>,
loadingData, loadingData,
errorData,
], ],
styles: layerAssets, styles: layerAssets,
} }
...@@ -537,6 +543,7 @@ async function createComponentTreeInternal({ ...@@ -537,6 +543,7 @@ async function createComponentTreeInternal({
pathname={staticGenerationStore.urlPathname} pathname={staticGenerationStore.urlPathname}
/>, />,
loadingData, loadingData,
errorData,
], ],
styles: layerAssets, styles: layerAssets,
} }
...@@ -629,6 +636,7 @@ async function createComponentTreeInternal({ ...@@ -629,6 +636,7 @@ async function createComponentTreeInternal({
{null} {null}
</>, </>,
loadingData, loadingData,
errorData,
], ],
styles: layerAssets, styles: layerAssets,
} }
......
...@@ -6,8 +6,11 @@ import type { NextFontManifest } from '../../build/webpack/plugins/next-font-man ...@@ -6,8 +6,11 @@ import type { NextFontManifest } from '../../build/webpack/plugins/next-font-man
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { AppPageModule } from '../future/route-modules/app-page/module' import type { AppPageModule } from '../future/route-modules/app-page/module'
import type { SwrDelta } from '../lib/revalidate' import type { SwrDelta } from '../lib/revalidate'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime'
import type { DeepReadonly } from '../../shared/lib/deep-readonly' import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import type {
ErrorModuleData,
LoadingModuleData,
} from '../../shared/lib/app-router-context.shared-runtime'
import s from 'next/dist/compiled/superstruct' import s from 'next/dist/compiled/superstruct'
...@@ -89,7 +92,8 @@ export type CacheNodeSeedData = [ ...@@ -89,7 +92,8 @@ export type CacheNodeSeedData = [
[parallelRouterKey: string]: CacheNodeSeedData | null [parallelRouterKey: string]: CacheNodeSeedData | null
}, },
node: React.ReactNode | null, node: React.ReactNode | null,
loading: LoadingModuleData loading: LoadingModuleData,
error: ErrorModuleData
] ]
export type FlightDataPath = export type FlightDataPath =
......
...@@ -8,6 +8,7 @@ import type { ...@@ -8,6 +8,7 @@ import type {
import type { FetchServerResponseResult } from '../../client/components/router-reducer/fetch-server-response' import type { FetchServerResponseResult } from '../../client/components/router-reducer/fetch-server-response'
import type { FlightRouterState } from '../../server/app-render/types' import type { FlightRouterState } from '../../server/app-render/types'
import React from 'react' import React from 'react'
import type { ErrorComponent } from '../../client/components/error-boundary'
export type ChildSegmentMap = Map<string, CacheNode> export type ChildSegmentMap = Map<string, CacheNode>
...@@ -20,6 +21,10 @@ export type LoadingModuleData = ...@@ -20,6 +21,10 @@ export type LoadingModuleData =
| [React.JSX.Element, React.ReactNode, React.ReactNode] | [React.JSX.Element, React.ReactNode, React.ReactNode]
| null | null
export type ErrorModuleData =
| [ErrorComponent, React.ReactNode, React.ReactNode]
| null
export type LazyCacheNode = { export type LazyCacheNode = {
/** /**
* Whether the lazy cache node data promise has been resolved. * Whether the lazy cache node data promise has been resolved.
...@@ -59,6 +64,7 @@ export type LazyCacheNode = { ...@@ -59,6 +64,7 @@ export type LazyCacheNode = {
head: React.ReactNode head: React.ReactNode
loading: LoadingModuleData loading: LoadingModuleData
error: ErrorModuleData
/** /**
* Child parallel routes. * Child parallel routes.
...@@ -106,6 +112,7 @@ export type ReadyCacheNode = { ...@@ -106,6 +112,7 @@ export type ReadyCacheNode = {
head: React.ReactNode head: React.ReactNode
loading: LoadingModuleData loading: LoadingModuleData
error: ErrorModuleData
parallelRoutes: Map<string, ChildSegmentMap> parallelRoutes: Map<string, ChildSegmentMap>
} }
...@@ -160,6 +167,7 @@ export const LayoutRouterContext = React.createContext<{ ...@@ -160,6 +167,7 @@ export const LayoutRouterContext = React.createContext<{
tree: FlightRouterState tree: FlightRouterState
url: string url: string
loading: LoadingModuleData loading: LoadingModuleData
error: ErrorModuleData
} | null>(null) } | null>(null)
export const GlobalLayoutRouterContext = React.createContext<{ export const GlobalLayoutRouterContext = React.createContext<{
......
import { notFound } from 'next/navigation'
export default function CatchAllPage() {
notFound()
}
// we don't want to try and prerender this page or it'll fail the build
export const dynamic = 'force-dynamic'
export default function Page() {
throw new Error('Trigger error boundary')
}
'use client' // Error components must be Client Components
import { useEffect } from 'react'
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
// Log the error to an error reporting service
console.error(error)
}, [error])
return (
<div>
<h2>Something went wrong!</h2>
<button
onClick={
// Attempt to recover by trying to re-render the segment
() => reset()
}
>
Try again
</button>
</div>
)
}
export default function RootLayout({
children,
nav,
}: {
children: React.ReactNode
nav: React.ReactNode
}) {
return (
<html>
<head />
<body>
<div>{children}</div>
<div className="nav">{nav}</div>
<div className="nav">{nav}</div>
</body>
</html>
)
}
import Link from 'next/link'
export default function NotFound() {
return (
<div>
<h2>Not Found</h2>
<p>Could not find requested resource</p>
<Link href="/">Return Home</Link>
</div>
)
}
export default function Home() {
return <h1>Hello World</h1>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('parallel-routes-duplicate-rendering', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should render parallel routes that repeat slot content', async () => {
const browser = await next.browser('/')
expect((await browser.elementsByCss('.nav')).length).toBe(2)
})
it('should still be able to handle not-found pages', async () => {
const browser = await next.browser('/non-existent')
expect((await browser.elementsByCss('.nav')).length).toBe(2)
expect(await browser.elementByCss('body').text()).toContain(
'Could not find requested resource'
)
})
it('should still be able to handle error pages', async () => {
const browser = await next.browser('/error-page')
expect((await browser.elementsByCss('.nav')).length).toBe(2)
expect(await browser.elementByCss('body').text()).toContain(
'Something went wrong!'
)
})
})
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