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 {
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
}
}
......@@ -626,8 +627,9 @@ function Router({
// Provided in AppTreeContext to ensure it can be overwritten in layout-router
url: canonicalUrl,
loading: cache.loading,
error: cache.error,
}
}, [cache.parallelRoutes, tree, canonicalUrl, cache.loading])
}, [cache.parallelRoutes, tree, canonicalUrl, cache.loading, cache.error])
const globalLayoutRouterContext = useMemo(() => {
return {
......
......@@ -9,7 +9,6 @@ import type {
FlightSegmentPath,
Segment,
} from '../../server/app-render/types'
import type { ErrorComponent } from './error-boundary'
import type { FocusAndScrollRef } from './router-reducer/router-reducer-types'
import React, {
......@@ -358,6 +357,7 @@ function InnerLayoutRouter({
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
}
/**
......@@ -453,6 +453,7 @@ function InnerLayoutRouter({
// TODO-APP: overriding of url for parallel routes
url: url,
loading: childNode.loading,
error: childNode.error,
}}
>
{resolvedRsc}
......@@ -507,9 +508,6 @@ function LoadingBoundary({
export default function OuterLayoutRouter({
parallelRouterKey,
segmentPath,
error,
errorStyles,
errorScripts,
templateStyles,
templateScripts,
template,
......@@ -519,9 +517,6 @@ export default function OuterLayoutRouter({
}: {
parallelRouterKey: string
segmentPath: FlightSegmentPath
error: ErrorComponent | undefined
errorStyles: React.ReactNode | undefined
errorScripts: React.ReactNode | undefined
templateStyles: React.ReactNode | undefined
templateScripts: React.ReactNode | undefined
template: React.ReactNode
......@@ -534,7 +529,7 @@ export default function OuterLayoutRouter({
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
let childNodesForParallelRouter = childNodes.get(parallelRouterKey)
......@@ -580,9 +575,9 @@ export default function OuterLayoutRouter({
value={
<ScrollAndFocusHandler segmentPath={segmentPath}>
<ErrorBoundary
errorComponent={error}
errorStyles={errorStyles}
errorScripts={errorScripts}
errorComponent={error?.[0]}
errorStyles={error?.[1]}
errorScripts={error?.[2]}
>
<LoadingBoundary
hasLoading={Boolean(loading)}
......
......@@ -21,6 +21,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
}
const existingCache: CacheNode = {
lazyData: null,
......@@ -30,6 +31,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -44,6 +46,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -59,6 +62,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
},
],
]),
......@@ -75,6 +79,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
expect(cache).toMatchInlineSnapshot(`
{
"error": null,
"head": null,
"lazyData": null,
"lazyDataResolved": false,
......@@ -82,6 +87,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
"parallelRoutes": Map {
"children" => Map {
"linking" => {
"error": null,
"head": null,
"lazyData": null,
"lazyDataResolved": false,
......@@ -89,6 +95,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
"parallelRoutes": Map {
"children" => Map {
"" => {
"error": null,
"head": null,
"lazyData": null,
"lazyDataResolved": false,
......@@ -109,6 +116,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
</React.Fragment>,
},
"dashboard" => {
"error": null,
"head": null,
"lazyData": null,
"lazyDataResolved": false,
......
......@@ -44,6 +44,7 @@ export function clearCacheNodeDataForSegmentPath(
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
})
}
return
......@@ -61,6 +62,7 @@ export function clearCacheNodeDataForSegmentPath(
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
})
}
return
......@@ -76,6 +78,7 @@ export function clearCacheNodeDataForSegmentPath(
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: childCacheNode.lazyDataResolved,
loading: childCacheNode.loading,
error: childCacheNode.error,
} as CacheNode
childSegmentMap.set(cacheKey, childCacheNode)
}
......
......@@ -37,7 +37,7 @@ describe('createInitialRouterState', () => {
buildId,
initialTree,
initialCanonicalUrl,
initialSeedData: ['', {}, children, null],
initialSeedData: ['', {}, children, null, null],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>,
......@@ -48,7 +48,7 @@ describe('createInitialRouterState', () => {
buildId,
initialTree,
initialCanonicalUrl,
initialSeedData: ['', {}, children, null],
initialSeedData: ['', {}, children, null, null],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>,
......@@ -62,6 +62,7 @@ describe('createInitialRouterState', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -81,6 +82,7 @@ describe('createInitialRouterState', () => {
prefetchRsc: null,
parallelRoutes: new Map(),
loading: null,
error: null,
head: <title>Test</title>,
prefetchHead: null,
lazyDataResolved: false,
......@@ -96,6 +98,7 @@ describe('createInitialRouterState', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
},
],
]),
......
......@@ -47,6 +47,7 @@ export function createInitialRouterState({
parallelRoutes: isServer ? new Map() : initialParallelRoutes,
lazyDataResolved: false,
loading: initialSeedData[3],
error: initialSeedData[4],
}
const canonicalUrl =
......
......@@ -33,6 +33,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
......@@ -44,6 +45,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -58,6 +60,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -72,6 +75,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map(),
},
],
......@@ -104,6 +108,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -118,6 +123,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -133,6 +139,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map(),
},
],
......@@ -144,6 +151,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......
......@@ -49,6 +49,7 @@ export function fillCacheWithNewSubTreeData(
const seedData: CacheNodeSeedData = flightDataPath[3]
const rsc = seedData[2]
const loading = seedData[3]
const error = seedData[4]
childCacheNode = {
lazyData: null,
rsc,
......@@ -56,6 +57,7 @@ export function fillCacheWithNewSubTreeData(
head: null,
prefetchHead: null,
loading,
error,
// Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes)
......@@ -101,6 +103,7 @@ export function fillCacheWithNewSubTreeData(
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: false,
loading: childCacheNode.loading,
error: childCacheNode.error,
} as CacheNode
childSegmentMap.set(cacheKey, childCacheNode)
}
......
......@@ -44,6 +44,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
}
const existingCache: CacheNode = {
lazyData: null,
......@@ -53,6 +54,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -67,6 +69,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -81,6 +84,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map(),
},
],
......@@ -119,6 +123,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -133,6 +138,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -143,6 +149,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -155,6 +162,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchRsc: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
head: (
......@@ -182,6 +190,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
},
......
......@@ -57,6 +57,7 @@ export function fillLazyItemsTillLeafWithHead(
// New data was sent from the server.
const seedNode = parallelSeedData[2]
const loading = parallelSeedData[3]
const error = parallelSeedData[4]
newCacheNode = {
lazyData: null,
rsc: seedNode,
......@@ -69,6 +70,7 @@ export function fillLazyItemsTillLeafWithHead(
head: null,
prefetchHead: null,
loading,
error,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false,
}
......@@ -100,6 +102,7 @@ export function fillLazyItemsTillLeafWithHead(
parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false,
loading: null,
error: null,
}
}
......@@ -125,6 +128,7 @@ export function fillLazyItemsTillLeafWithHead(
// New data was sent from the server.
const seedNode = parallelSeedData[2]
const loading = parallelSeedData[3]
const error = parallelSeedData[4]
newCacheNode = {
lazyData: null,
rsc: seedNode,
......@@ -134,6 +138,7 @@ export function fillLazyItemsTillLeafWithHead(
parallelRoutes: new Map(),
lazyDataResolved: false,
loading,
error,
}
} else {
// No data available for this node. This will trigger a lazy fetch
......@@ -147,6 +152,7 @@ export function fillLazyItemsTillLeafWithHead(
parallelRoutes: new Map(),
lazyDataResolved: false,
loading: null,
error: null,
}
}
......
......@@ -34,6 +34,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
......@@ -45,6 +46,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -59,6 +61,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -73,6 +76,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map(),
},
],
......@@ -115,6 +119,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -127,6 +132,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -136,6 +142,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
{
lazyData: null,
loading: null,
error: null,
parallelRoutes: new Map(),
rsc: <React.Fragment>Page</React.Fragment>,
prefetchRsc: null,
......
......@@ -12,6 +12,7 @@ describe('invalidateCacheByRouterState', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
......@@ -23,6 +24,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -37,6 +39,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -51,6 +54,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map(),
},
],
......@@ -94,6 +98,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([['children', new Map()]]),
}
......
......@@ -234,6 +234,7 @@ export function updateCacheNodeOnNavigation(
head: oldCacheNode.head,
prefetchHead: oldCacheNode.prefetchHead,
loading: oldCacheNode.loading,
error: oldCacheNode.error,
// Everything is cloned except for the children, which we computed above.
parallelRoutes: prefetchParallelRoutes,
......@@ -514,6 +515,8 @@ function createPendingCacheNode(
const maybePrefetchRsc = prefetchData !== null ? prefetchData[2] : null
const maybePrefetchLoading = prefetchData !== null ? prefetchData[3] : null
const maybePrefetchError = prefetchData !== null ? prefetchData[4] : null
return {
lazyData: null,
parallelRoutes: parallelRoutes,
......@@ -521,6 +524,7 @@ function createPendingCacheNode(
prefetchRsc: maybePrefetchRsc !== undefined ? maybePrefetchRsc : null,
prefetchHead: isLeafSegment ? prefetchHead : null,
loading: maybePrefetchLoading !== undefined ? maybePrefetchLoading : null,
error: maybePrefetchError !== undefined ? maybePrefetchError : null,
// Create a deferred promise. This will be fulfilled once the dynamic
// response is received from the server.
......@@ -765,6 +769,7 @@ export function updateCacheNodeOnPopstateRestoration(
prefetchHead: shouldUsePrefetch ? oldCacheNode.prefetchHead : null,
prefetchRsc: shouldUsePrefetch ? oldCacheNode.prefetchRsc : null,
loading: shouldUsePrefetch ? oldCacheNode.loading : null,
error: shouldUsePrefetch ? oldCacheNode.error : null,
// These are the cloned children we computed above
parallelRoutes: newParallelRoutes,
......
......@@ -33,6 +33,7 @@ describe('findHeadInCache', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -47,6 +48,7 @@ describe('findHeadInCache', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -59,6 +61,7 @@ describe('findHeadInCache', () => {
head: null,
prefetchHead: null,
loading: null,
error: null,
parallelRoutes: new Map([
[
'children',
......@@ -72,6 +75,7 @@ describe('findHeadInCache', () => {
prefetchHead: null,
lazyDataResolved: false,
loading: null,
error: null,
parallelRoutes: new Map(),
head: (
<>
......
......@@ -541,6 +541,7 @@ async function ReactServerError({
<body></body>
</html>,
null,
null,
]
return (
<AppRouter
......
......@@ -5,7 +5,9 @@ import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path'
export async function createComponentStylesAndScripts({
export async function createComponentStylesAndScripts<
T = React.ComponentType<unknown>
>({
filePath,
getComponent,
injectedCSS,
......@@ -17,7 +19,7 @@ export async function createComponentStylesAndScripts({
injectedCSS: Set<string>
injectedJS: Set<string>
ctx: AppRenderContext
}): Promise<[React.ComponentType<any>, React.ReactNode, React.ReactNode]> {
}): Promise<[T, React.ReactNode, React.ReactNode]> {
const { styles: cssHrefs, scripts: jsHrefs } = getLinkAndScriptTags(
ctx.clientReferenceManifest,
filePath,
......
......@@ -14,7 +14,11 @@ import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/parallel-ro
import { getTracer } from '../lib/trace/tracer'
import { NextNodeServerSpan } from '../lib/trace/constants'
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 = {
seedData: CacheNodeSeedData
......@@ -127,7 +131,7 @@ async function createComponentTreeInternal({
: [React.Fragment]
const [ErrorComponent, errorStyles, errorScripts] = error
? await createComponentStylesAndScripts({
? await createComponentStylesAndScripts<ErrorComponent>({
ctx,
filePath: error[1],
getComponent: error[0],
......@@ -459,9 +463,6 @@ async function createComponentTreeInternal({
parallelRouterKey={parallelRouteKey}
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.
error={ErrorComponent}
errorStyles={errorStyles}
errorScripts={errorScripts}
template={
<Template>
<RenderFromTemplateContext />
......@@ -494,6 +495,10 @@ async function createComponentTreeInternal({
? [<Loading />, loadingStyles, loadingScripts]
: 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
if (!Component) {
return {
......@@ -507,6 +512,7 @@ async function createComponentTreeInternal({
// caution, for now.
<>{parallelRouteProps.children}</>,
loadingData,
errorData,
],
styles: layerAssets,
}
......@@ -537,6 +543,7 @@ async function createComponentTreeInternal({
pathname={staticGenerationStore.urlPathname}
/>,
loadingData,
errorData,
],
styles: layerAssets,
}
......@@ -629,6 +636,7 @@ async function createComponentTreeInternal({
{null}
</>,
loadingData,
errorData,
],
styles: layerAssets,
}
......
......@@ -6,8 +6,11 @@ import type { NextFontManifest } from '../../build/webpack/plugins/next-font-man
import type { ParsedUrlQuery } from 'querystring'
import type { AppPageModule } from '../future/route-modules/app-page/module'
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 {
ErrorModuleData,
LoadingModuleData,
} from '../../shared/lib/app-router-context.shared-runtime'
import s from 'next/dist/compiled/superstruct'
......@@ -89,7 +92,8 @@ export type CacheNodeSeedData = [
[parallelRouterKey: string]: CacheNodeSeedData | null
},
node: React.ReactNode | null,
loading: LoadingModuleData
loading: LoadingModuleData,
error: ErrorModuleData
]
export type FlightDataPath =
......
......@@ -8,6 +8,7 @@ import type {
import type { FetchServerResponseResult } from '../../client/components/router-reducer/fetch-server-response'
import type { FlightRouterState } from '../../server/app-render/types'
import React from 'react'
import type { ErrorComponent } from '../../client/components/error-boundary'
export type ChildSegmentMap = Map<string, CacheNode>
......@@ -20,6 +21,10 @@ export type LoadingModuleData =
| [React.JSX.Element, React.ReactNode, React.ReactNode]
| null
export type ErrorModuleData =
| [ErrorComponent, React.ReactNode, React.ReactNode]
| null
export type LazyCacheNode = {
/**
* Whether the lazy cache node data promise has been resolved.
......@@ -59,6 +64,7 @@ export type LazyCacheNode = {
head: React.ReactNode
loading: LoadingModuleData
error: ErrorModuleData
/**
* Child parallel routes.
......@@ -106,6 +112,7 @@ export type ReadyCacheNode = {
head: React.ReactNode
loading: LoadingModuleData
error: ErrorModuleData
parallelRoutes: Map<string, ChildSegmentMap>
}
......@@ -160,6 +167,7 @@ export const LayoutRouterContext = React.createContext<{
tree: FlightRouterState
url: string
loading: LoadingModuleData
error: ErrorModuleData
} | null>(null)
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