Unverified Commit 73cfb966 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Separate viewport and metadata in rsc and cache (#73867)

parent 2a791d1b
...@@ -145,7 +145,7 @@ export function createEmptyCacheNode(): CacheNode { ...@@ -145,7 +145,7 @@ export function createEmptyCacheNode(): CacheNode {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
......
...@@ -549,7 +549,7 @@ export default function OuterLayoutRouter({ ...@@ -549,7 +549,7 @@ export default function OuterLayoutRouter({
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
......
...@@ -172,7 +172,7 @@ function fillNewTreeWithOnlyLoadingSegments( ...@@ -172,7 +172,7 @@ function fillNewTreeWithOnlyLoadingSegments(
// copy the layout but null the page segment as that's not meant to be used // copy the layout but null the page segment as that's not meant to be used
rsc: segmentForParallelRoute.includes(PAGE_SEGMENT_KEY) ? null : rsc, rsc: segmentForParallelRoute.includes(PAGE_SEGMENT_KEY) ? null : rsc,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading, loading,
...@@ -184,7 +184,7 @@ function fillNewTreeWithOnlyLoadingSegments( ...@@ -184,7 +184,7 @@ function fillNewTreeWithOnlyLoadingSegments(
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
......
...@@ -16,7 +16,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -16,7 +16,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
...@@ -25,7 +25,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -25,7 +25,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: <>Root layout</>, rsc: <>Root layout</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -38,7 +38,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -38,7 +38,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: <>Linking</>, rsc: <>Linking</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -51,7 +51,7 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -51,7 +51,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
...@@ -70,52 +70,64 @@ describe('clearCacheNodeDataForSegmentPath', () => { ...@@ -70,52 +70,64 @@ describe('clearCacheNodeDataForSegmentPath', () => {
clearCacheNodeDataForSegmentPath(cache, existingCache, flightSegmentPath) clearCacheNodeDataForSegmentPath(cache, existingCache, flightSegmentPath)
expect(cache).toMatchInlineSnapshot(` expect(cache).toMatchInlineSnapshot(`
{ {
"head": null, "head": [
"lazyData": null, null,
"loading": null, null,
"parallelRoutes": Map { ],
"children" => Map { "lazyData": null,
"linking" => { "loading": null,
"head": null, "parallelRoutes": Map {
"lazyData": null, "children" => Map {
"loading": null, "linking" => {
"parallelRoutes": Map { "head": [
"children" => Map { null,
"" => { null,
"head": null, ],
"lazyData": null, "lazyData": null,
"loading": null, "loading": null,
"parallelRoutes": Map {}, "parallelRoutes": Map {
"prefetchHead": null, "children" => Map {
"prefetchRsc": null, "" => {
"rsc": <React.Fragment> "head": [
Page null,
</React.Fragment>, null,
}, ],
}, "lazyData": null,
}, "loading": null,
"prefetchHead": null, "parallelRoutes": Map {},
"prefetchRsc": null, "prefetchHead": null,
"rsc": <React.Fragment> "prefetchRsc": null,
Linking "rsc": <React.Fragment>
</React.Fragment>, Page
}, </React.Fragment>,
"dashboard" => { },
"head": null, },
"lazyData": null, },
"loading": null, "prefetchHead": null,
"parallelRoutes": Map {}, "prefetchRsc": null,
"prefetchHead": null, "rsc": <React.Fragment>
"prefetchRsc": null, Linking
"rsc": null, </React.Fragment>,
}, },
}, "dashboard" => {
}, "head": [
"prefetchHead": null, null,
"prefetchRsc": null, null,
"rsc": null, ],
} "lazyData": null,
"loading": null,
"parallelRoutes": Map {},
"prefetchHead": null,
"prefetchRsc": null,
"rsc": null,
},
},
},
"prefetchHead": null,
"prefetchRsc": null,
"rsc": null,
}
`) `)
}) })
}) })
...@@ -40,7 +40,7 @@ export function clearCacheNodeDataForSegmentPath( ...@@ -40,7 +40,7 @@ export function clearCacheNodeDataForSegmentPath(
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
...@@ -56,7 +56,7 @@ export function clearCacheNodeDataForSegmentPath( ...@@ -56,7 +56,7 @@ export function clearCacheNodeDataForSegmentPath(
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
......
...@@ -59,7 +59,7 @@ describe('createInitialRouterState', () => { ...@@ -59,7 +59,7 @@ describe('createInitialRouterState', () => {
lazyData: null, lazyData: null,
rsc: children, rsc: children,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -81,7 +81,7 @@ describe('createInitialRouterState', () => { ...@@ -81,7 +81,7 @@ describe('createInitialRouterState', () => {
prefetchRsc: null, prefetchRsc: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
head: <title>Test</title>, head: [null, <title>Test</title>],
prefetchHead: null, prefetchHead: null,
}, },
], ],
...@@ -91,7 +91,7 @@ describe('createInitialRouterState', () => { ...@@ -91,7 +91,7 @@ describe('createInitialRouterState', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
}, },
......
...@@ -48,7 +48,7 @@ export function createInitialRouterState({ ...@@ -48,7 +48,7 @@ export function createInitialRouterState({
lazyData: null, lazyData: null,
rsc, rsc,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
// The cache gets seeded during the first render. `initialParallelRoutes` ensures the cache from the first render is there during the second render. // The cache gets seeded during the first render. `initialParallelRoutes` ensures the cache from the first render is there during the second render.
parallelRoutes: isServer ? new Map() : initialParallelRoutes, parallelRoutes: isServer ? new Map() : initialParallelRoutes,
...@@ -74,7 +74,8 @@ export function createInitialRouterState({ ...@@ -74,7 +74,8 @@ export function createInitialRouterState({
undefined, undefined,
initialTree, initialTree,
initialSeedData, initialSeedData,
initialHead initialHead,
undefined
) )
} }
......
import React from 'react'
import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data' import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data'
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime' import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type { NormalizedFlightData } from '../../flight-data-helpers' import type { NormalizedFlightData } from '../../flight-data-helpers'
...@@ -11,7 +10,7 @@ const getFlightData = (): NormalizedFlightData[] => { ...@@ -11,7 +10,7 @@ const getFlightData = (): NormalizedFlightData[] => {
segment: 'about', segment: 'about',
tree: ['about', { children: ['', {}] }], tree: ['about', { children: ['', {}] }],
seedData: ['about', <h1>SubTreeData Injected!</h1>, {}, null, false], seedData: ['about', <h1>SubTreeData Injected!</h1>, {}, null, false],
head: '<title>Head Injected!</title>', head: [null, '<title>Head Injected!</title>'],
isHeadPartial: false, isHeadPartial: false,
isRootRender: false, isRootRender: false,
}, },
...@@ -24,7 +23,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -24,7 +23,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -33,7 +32,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -33,7 +32,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: <>Root layout</>, rsc: <>Root layout</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -46,7 +45,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -46,7 +45,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: <>Linking</>, rsc: <>Linking</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -59,7 +58,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -59,7 +58,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -90,7 +89,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -90,7 +89,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -103,7 +102,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -103,7 +102,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: <>Linking</>, rsc: <>Linking</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -117,7 +116,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -117,7 +116,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -127,7 +126,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -127,7 +126,7 @@ describe('fillCacheWithNewSubtreeData', () => {
'about', 'about',
{ {
lazyData: null, lazyData: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -143,11 +142,12 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -143,11 +142,12 @@ describe('fillCacheWithNewSubtreeData', () => {
parallelRoutes: new Map(), parallelRoutes: new Map(),
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
head: ( head: [
null,
<> <>
<title>Head Injected!</title> <title>Head Injected!</title>
</> </>,
), ],
}, },
], ],
]), ]),
......
...@@ -71,7 +71,7 @@ function fillCacheHelper( ...@@ -71,7 +71,7 @@ function fillCacheHelper(
rsc: rsc:
fillLazyItems || incomingSegment !== PAGE_SEGMENT_KEY ? rsc : null, fillLazyItems || incomingSegment !== PAGE_SEGMENT_KEY ? rsc : null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading, loading,
parallelRoutes: parallelRoutes:
......
...@@ -39,7 +39,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -39,7 +39,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
...@@ -48,7 +48,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -48,7 +48,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
rsc: <>Root layout</>, rsc: <>Root layout</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -61,7 +61,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -61,7 +61,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
rsc: <>Linking</>, rsc: <>Linking</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -74,7 +74,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -74,7 +74,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -100,19 +100,21 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -100,19 +100,21 @@ describe('fillLazyItemsTillLeafWithHead', () => {
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
const [treePatch, cacheNodeSeedData, head /*, isHeadPartial */] = const [treePatch, cacheNodeSeedData, head /*, isHeadPartial */] =
flightDataPath.slice(-4) flightDataPath.slice(-4)
fillLazyItemsTillLeafWithHead( fillLazyItemsTillLeafWithHead(
cache, cache,
existingCache, existingCache,
treePatch, treePatch,
cacheNodeSeedData, cacheNodeSeedData,
head head,
undefined
) )
const expectedCache: CacheNode = { const expectedCache: CacheNode = {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -125,7 +127,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -125,7 +127,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -150,11 +152,12 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -150,11 +152,12 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
head: ( head: [
null,
<> <>
<title>About page!</title> <title>About page!</title>
</> </>,
), ],
}, },
], ],
]), ]),
...@@ -162,7 +165,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -162,7 +165,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
]), ]),
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
}, },
], ],
...@@ -172,7 +175,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -172,7 +175,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
......
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime' import type {
CacheNode,
HeadData,
} from '../../../shared/lib/app-router-context.shared-runtime'
import type { import type {
FlightRouterState, FlightRouterState,
CacheNodeSeedData, CacheNodeSeedData,
...@@ -14,8 +17,8 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -14,8 +17,8 @@ export function fillLazyItemsTillLeafWithHead(
existingCache: CacheNode | undefined, existingCache: CacheNode | undefined,
routerState: FlightRouterState, routerState: FlightRouterState,
cacheNodeSeedData: CacheNodeSeedData | null, cacheNodeSeedData: CacheNodeSeedData | null,
head: React.ReactNode, head: HeadData,
prefetchEntry?: PrefetchCacheEntry prefetchEntry: PrefetchCacheEntry | undefined
): void { ): void {
const isLastSegment = Object.keys(routerState[1]).length === 0 const isLastSegment = Object.keys(routerState[1]).length === 0
if (isLastSegment) { if (isLastSegment) {
...@@ -66,7 +69,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -66,7 +69,7 @@ export function fillLazyItemsTillLeafWithHead(
// `prefetchRsc`. As an incremental step, we'll just de-opt to the // `prefetchRsc`. As an incremental step, we'll just de-opt to the
// old behavior — no PPR value. // old behavior — no PPR value.
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading, loading,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes), parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
...@@ -93,7 +96,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -93,7 +96,7 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes), parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
loading: null, loading: null,
...@@ -126,7 +129,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -126,7 +129,7 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null, lazyData: null,
rsc: seedNode, rsc: seedNode,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading, loading,
...@@ -138,7 +141,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -138,7 +141,7 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null, loading: null,
......
...@@ -12,7 +12,7 @@ const getFlightData = (): NormalizedFlightData[] => { ...@@ -12,7 +12,7 @@ const getFlightData = (): NormalizedFlightData[] => {
segment: 'about', segment: 'about',
tree: ['about', { children: ['', {}] }], tree: ['about', { children: ['', {}] }],
seedData: ['about', <h1>About Page!</h1>, {}, null, false], seedData: ['about', <h1>About Page!</h1>, {}, null, false],
head: '<title>About page!</title>', head: [null, '<title>About page!</title>'],
isHeadPartial: false, isHeadPartial: false,
isRootRender: false, isRootRender: false,
}, },
...@@ -25,7 +25,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -25,7 +25,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -34,7 +34,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -34,7 +34,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: <>Root layout</>, rsc: <>Root layout</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -47,7 +47,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -47,7 +47,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: <>Linking</>, rsc: <>Linking</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -60,7 +60,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -60,7 +60,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -100,7 +100,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -100,7 +100,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
const expectedCache: CacheNode = { const expectedCache: CacheNode = {
lazyData: null, lazyData: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -111,7 +111,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -111,7 +111,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
'linking', 'linking',
{ {
lazyData: null, lazyData: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -126,7 +126,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -126,7 +126,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
parallelRoutes: new Map(), parallelRoutes: new Map(),
rsc: <React.Fragment>Page</React.Fragment>, rsc: <React.Fragment>Page</React.Fragment>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
}, },
], ],
......
import React from 'react'
import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state' import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state'
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime' import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type { FlightRouterState } from '../../../server/app-render/types' import type { FlightRouterState } from '../../../server/app-render/types'
...@@ -9,7 +8,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -9,7 +8,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -18,7 +17,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -18,7 +17,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null, lazyData: null,
rsc: <>Root layout</>, rsc: <>Root layout</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -31,7 +30,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -31,7 +30,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null, lazyData: null,
rsc: <>Linking</>, rsc: <>Linking</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -44,7 +43,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -44,7 +43,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null, lazyData: null,
rsc: <>Page</>, rsc: <>Page</>,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
...@@ -86,7 +85,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -86,7 +85,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([['children', new Map()]]), parallelRoutes: new Map([['children', new Map()]]),
......
...@@ -7,6 +7,7 @@ import type { ...@@ -7,6 +7,7 @@ import type {
import type { import type {
CacheNode, CacheNode,
ChildSegmentMap, ChildSegmentMap,
HeadData,
ReadyCacheNode, ReadyCacheNode,
} from '../../../shared/lib/app-router-context.shared-runtime' } from '../../../shared/lib/app-router-context.shared-runtime'
import { DEFAULT_SEGMENT_KEY } from '../../../shared/lib/segment' import { DEFAULT_SEGMENT_KEY } from '../../../shared/lib/segment'
...@@ -70,7 +71,7 @@ export function updateCacheNodeOnNavigation( ...@@ -70,7 +71,7 @@ export function updateCacheNodeOnNavigation(
oldRouterState: FlightRouterState, oldRouterState: FlightRouterState,
newRouterState: FlightRouterState, newRouterState: FlightRouterState,
prefetchData: CacheNodeSeedData | null, prefetchData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode | null, prefetchHead: HeadData,
isPrefetchHeadPartial: boolean isPrefetchHeadPartial: boolean
): Task | null { ): Task | null {
// Diff the old and new trees to reuse the shared layouts. // Diff the old and new trees to reuse the shared layouts.
...@@ -285,7 +286,7 @@ export function updateCacheNodeOnNavigation( ...@@ -285,7 +286,7 @@ export function updateCacheNodeOnNavigation(
function createCacheNodeOnNavigation( function createCacheNodeOnNavigation(
routerState: FlightRouterState, routerState: FlightRouterState,
prefetchData: CacheNodeSeedData | null, prefetchData: CacheNodeSeedData | null,
possiblyPartialPrefetchHead: React.ReactNode | null, possiblyPartialPrefetchHead: HeadData,
isPrefetchHeadPartial: boolean isPrefetchHeadPartial: boolean
): Task { ): Task {
// Same traversal as updateCacheNodeNavigation, but we switch to this path // Same traversal as updateCacheNodeNavigation, but we switch to this path
...@@ -386,7 +387,7 @@ function createCacheNodeOnNavigation( ...@@ -386,7 +387,7 @@ function createCacheNodeOnNavigation(
// `prefetchRsc` field. // `prefetchRsc` field.
rsc, rsc,
prefetchRsc: null, prefetchRsc: null,
head: isLeafSegment ? possiblyPartialPrefetchHead : null, head: isLeafSegment ? possiblyPartialPrefetchHead : [null, null],
prefetchHead: null, prefetchHead: null,
loading, loading,
parallelRoutes: cacheNodeChildren, parallelRoutes: cacheNodeChildren,
...@@ -531,7 +532,7 @@ function writeDynamicDataIntoPendingTask( ...@@ -531,7 +532,7 @@ function writeDynamicDataIntoPendingTask(
segmentPath: FlightSegmentPath, segmentPath: FlightSegmentPath,
serverRouterState: FlightRouterState, serverRouterState: FlightRouterState,
dynamicData: CacheNodeSeedData, dynamicData: CacheNodeSeedData,
dynamicHead: React.ReactNode dynamicHead: HeadData
) { ) {
// The data sent by the server represents only a subtree of the app. We need // The data sent by the server represents only a subtree of the app. We need
// to find the part of the task tree that matches the server response, and // to find the part of the task tree that matches the server response, and
...@@ -578,7 +579,7 @@ function finishTaskUsingDynamicDataPayload( ...@@ -578,7 +579,7 @@ function finishTaskUsingDynamicDataPayload(
task: Task, task: Task,
serverRouterState: FlightRouterState, serverRouterState: FlightRouterState,
dynamicData: CacheNodeSeedData, dynamicData: CacheNodeSeedData,
dynamicHead: React.ReactNode dynamicHead: HeadData
) { ) {
if (task.dynamicRequestTree === null) { if (task.dynamicRequestTree === null) {
// Everything in this subtree is already complete. Bail out. // Everything in this subtree is already complete. Bail out.
...@@ -694,7 +695,12 @@ function createPendingCacheNode( ...@@ -694,7 +695,12 @@ function createPendingCacheNode(
// 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.
rsc: createDeferredRsc() as React.ReactNode, rsc: createDeferredRsc() as React.ReactNode,
head: isLeafSegment ? (createDeferredRsc() as React.ReactNode) : null, head: isLeafSegment
? [
createDeferredRsc() as React.ReactNode,
createDeferredRsc() as React.ReactNode,
]
: [null, null],
} }
} }
...@@ -703,7 +709,7 @@ function finishPendingCacheNode( ...@@ -703,7 +709,7 @@ function finishPendingCacheNode(
taskState: FlightRouterState, taskState: FlightRouterState,
serverState: FlightRouterState, serverState: FlightRouterState,
dynamicData: CacheNodeSeedData, dynamicData: CacheNodeSeedData,
dynamicHead: React.ReactNode dynamicHead: HeadData
): void { ): void {
// Writes a dynamic response into an existing Cache Node tree. This does _not_ // Writes a dynamic response into an existing Cache Node tree. This does _not_
// create a new tree, it updates the existing tree in-place. So it must follow // create a new tree, it updates the existing tree in-place. So it must follow
...@@ -796,8 +802,12 @@ function finishPendingCacheNode( ...@@ -796,8 +802,12 @@ function finishPendingCacheNode(
// a pending promise that needs to be resolved with the dynamic head from // a pending promise that needs to be resolved with the dynamic head from
// the server. // the server.
const head = cacheNode.head const head = cacheNode.head
if (isDeferredRsc(head)) { // Handle head[0] - viewport and head[1] - metadata
head.resolve(dynamicHead) if (isDeferredRsc(head[0])) {
head[0].resolve(dynamicHead[0])
}
if (isDeferredRsc(head[1])) {
head[1].resolve(dynamicHead[1])
} }
} }
...@@ -880,7 +890,7 @@ function abortPendingCacheNode( ...@@ -880,7 +890,7 @@ function abortPendingCacheNode(
export function updateCacheNodeOnPopstateRestoration( export function updateCacheNodeOnPopstateRestoration(
oldCacheNode: CacheNode, oldCacheNode: CacheNode,
routerState: FlightRouterState routerState: FlightRouterState
) { ): ReadyCacheNode {
// A popstate navigation reads data from the local cache. It does not issue // A popstate navigation reads data from the local cache. It does not issue
// new network requests (unless the cache entries have been evicted). So, we // new network requests (unless the cache entries have been evicted). So, we
// update the cache to drop the prefetch data for any segment whose dynamic // update the cache to drop the prefetch data for any segment whose dynamic
......
...@@ -29,7 +29,7 @@ describe('findHeadInCache', () => { ...@@ -29,7 +29,7 @@ describe('findHeadInCache', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -42,7 +42,7 @@ describe('findHeadInCache', () => { ...@@ -42,7 +42,7 @@ describe('findHeadInCache', () => {
lazyData: null, lazyData: null,
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -53,7 +53,7 @@ describe('findHeadInCache', () => { ...@@ -53,7 +53,7 @@ describe('findHeadInCache', () => {
'about', 'about',
{ {
lazyData: null, lazyData: null,
head: null, head: [null, null],
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
...@@ -69,11 +69,12 @@ describe('findHeadInCache', () => { ...@@ -69,11 +69,12 @@ describe('findHeadInCache', () => {
prefetchHead: null, prefetchHead: null,
loading: null, loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
head: ( head: [
null,
<> <>
<title>About page!</title> <title>About page!</title>
</> </>,
), ],
}, },
], ],
]), ]),
...@@ -107,7 +108,8 @@ describe('findHeadInCache', () => { ...@@ -107,7 +108,8 @@ describe('findHeadInCache', () => {
expect(result).not.toBeNull() expect(result).not.toBeNull()
const [cacheNode, key] = result! const [cacheNode, key] = result!
expect(cacheNode.head).toMatchObject( expect(cacheNode.head[0]).toBe(null)
expect(cacheNode.head[1]).toMatchObject(
<> <>
<title>About page!</title> <title>About page!</title>
</> </>
......
...@@ -118,7 +118,8 @@ export function refreshReducer( ...@@ -118,7 +118,8 @@ export function refreshReducer(
undefined, undefined,
treePatch, treePatch,
cacheNodeSeedData, cacheNodeSeedData,
head head,
undefined
) )
mutable.prefetchCache = new Map() mutable.prefetchCache = new Map()
} }
......
...@@ -333,7 +333,8 @@ export function serverActionReducer( ...@@ -333,7 +333,8 @@ export function serverActionReducer(
undefined, undefined,
treePatch, treePatch,
cacheNodeSeedData, cacheNodeSeedData,
head head,
undefined
) )
mutable.cache = cache mutable.cache = cache
......
...@@ -3,7 +3,10 @@ import type { ...@@ -3,7 +3,10 @@ import type {
RootTreePrefetch, RootTreePrefetch,
SegmentPrefetch, SegmentPrefetch,
} from '../../../server/app-render/collect-segment-data' } from '../../../server/app-render/collect-segment-data'
import type { LoadingModuleData } from '../../../shared/lib/app-router-context.shared-runtime' import type {
HeadData,
LoadingModuleData,
} from '../../../shared/lib/app-router-context.shared-runtime'
import { import {
NEXT_DID_POSTPONE_HEADER, NEXT_DID_POSTPONE_HEADER,
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
...@@ -80,7 +83,7 @@ type PendingRouteCacheEntry = RouteCacheEntryShared & { ...@@ -80,7 +83,7 @@ type PendingRouteCacheEntry = RouteCacheEntryShared & {
blockedTasks: Set<PrefetchTask> | null blockedTasks: Set<PrefetchTask> | null
canonicalUrl: null canonicalUrl: null
tree: null tree: null
head: null head: [null, null]
isHeadPartial: true isHeadPartial: true
} }
...@@ -89,7 +92,7 @@ type RejectedRouteCacheEntry = RouteCacheEntryShared & { ...@@ -89,7 +92,7 @@ type RejectedRouteCacheEntry = RouteCacheEntryShared & {
blockedTasks: Set<PrefetchTask> | null blockedTasks: Set<PrefetchTask> | null
canonicalUrl: null canonicalUrl: null
tree: null tree: null
head: null head: [null, null]
isHeadPartial: true isHeadPartial: true
} }
...@@ -98,7 +101,7 @@ export type FulfilledRouteCacheEntry = RouteCacheEntryShared & { ...@@ -98,7 +101,7 @@ export type FulfilledRouteCacheEntry = RouteCacheEntryShared & {
blockedTasks: null blockedTasks: null
canonicalUrl: string canonicalUrl: string
tree: TreePrefetch tree: TreePrefetch
head: React.ReactNode | null head: HeadData
isHeadPartial: boolean isHeadPartial: boolean
} }
...@@ -284,7 +287,7 @@ export function requestRouteCacheEntryFromCache( ...@@ -284,7 +287,7 @@ export function requestRouteCacheEntryFromCache(
status: EntryStatus.Pending, status: EntryStatus.Pending,
blockedTasks: null, blockedTasks: null,
tree: null, tree: null,
head: null, head: [null, null],
isHeadPartial: true, isHeadPartial: true,
// If the request takes longer than a minute, a subsequent request should // If the request takes longer than a minute, a subsequent request should
// retry instead of waiting for this one. // retry instead of waiting for this one.
...@@ -424,7 +427,7 @@ function pingBlockedTasks(entry: { ...@@ -424,7 +427,7 @@ function pingBlockedTasks(entry: {
function fulfillRouteCacheEntry( function fulfillRouteCacheEntry(
entry: PendingRouteCacheEntry, entry: PendingRouteCacheEntry,
tree: TreePrefetch, tree: TreePrefetch,
head: React.ReactNode, head: HeadData,
isHeadPartial: boolean, isHeadPartial: boolean,
staleAt: number, staleAt: number,
couldBeIntercepted: boolean, couldBeIntercepted: boolean,
......
...@@ -5,6 +5,7 @@ import type { ...@@ -5,6 +5,7 @@ import type {
} from '../../../server/app-render/types' } from '../../../server/app-render/types'
import type { import type {
CacheNode, CacheNode,
HeadData,
LoadingModuleData, LoadingModuleData,
} from '../../../shared/lib/app-router-context.shared-runtime' } from '../../../shared/lib/app-router-context.shared-runtime'
import type { NormalizedFlightData } from '../../flight-data-helpers' import type { NormalizedFlightData } from '../../flight-data-helpers'
...@@ -125,7 +126,7 @@ function navigateUsingPrefetchedRouteTree( ...@@ -125,7 +126,7 @@ function navigateUsingPrefetchedRouteTree(
currentFlightRouterState: FlightRouterState, currentFlightRouterState: FlightRouterState,
prefetchFlightRouterState: FlightRouterState, prefetchFlightRouterState: FlightRouterState,
prefetchSeedData: CacheNodeSeedData | null, prefetchSeedData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode | null, prefetchHead: HeadData,
isPrefetchHeadPartial: boolean, isPrefetchHeadPartial: boolean,
canonicalUrl: string canonicalUrl: string
): SuccessfulNavigationResult | NoOpNavigationResult { ): SuccessfulNavigationResult | NoOpNavigationResult {
...@@ -307,7 +308,7 @@ async function navigateDynamicallyWithNoPrefetch( ...@@ -307,7 +308,7 @@ async function navigateDynamicallyWithNoPrefetch(
// In our simulated prefetch payload, we pretend that there's no seed data // In our simulated prefetch payload, we pretend that there's no seed data
// nor a prefetch head. // nor a prefetch head.
const prefetchSeedData = null const prefetchSeedData = null
const prefetchHead = null const prefetchHead: [null, null] = [null, null]
const isPrefetchHeadPartial = true const isPrefetchHeadPartial = true
const canonicalUrl = createCanonicalUrl( const canonicalUrl = createCanonicalUrl(
......
...@@ -6,6 +6,7 @@ import type { ...@@ -6,6 +6,7 @@ import type {
FlightSegmentPath, FlightSegmentPath,
Segment, Segment,
} from '../server/app-render/types' } from '../server/app-render/types'
import type { HeadData } from '../shared/lib/app-router-context.shared-runtime'
export type NormalizedFlightData = { export type NormalizedFlightData = {
/** /**
...@@ -19,7 +20,7 @@ export type NormalizedFlightData = { ...@@ -19,7 +20,7 @@ export type NormalizedFlightData = {
segment: Segment segment: Segment
tree: FlightRouterState tree: FlightRouterState
seedData: CacheNodeSeedData | null seedData: CacheNodeSeedData | null
head: React.ReactNode | null head: HeadData
isHeadPartial: boolean isHeadPartial: boolean
isRootRender: boolean isRootRender: boolean
} }
...@@ -31,10 +32,13 @@ export type NormalizedFlightData = { ...@@ -31,10 +32,13 @@ export type NormalizedFlightData = {
export function getFlightDataPartsFromPath( export function getFlightDataPartsFromPath(
flightDataPath: FlightDataPath flightDataPath: FlightDataPath
): NormalizedFlightData { ): NormalizedFlightData {
// Pick the last 4 items from the `FlightDataPath` to get the [tree, seedData, [viewport, metadata], isHeadPartial].
const flightDataPathLength = 4
// tree, seedData, and head are *always* the last three items in the `FlightDataPath`. // tree, seedData, and head are *always* the last three items in the `FlightDataPath`.
const [tree, seedData, head, isHeadPartial] = flightDataPath.slice(-4) const [tree, seedData, head, isHeadPartial] =
flightDataPath.slice(-flightDataPathLength)
// The `FlightSegmentPath` is everything except the last three items. For a root render, it won't be present. // The `FlightSegmentPath` is everything except the last three items. For a root render, it won't be present.
const segmentPath = flightDataPath.slice(0, -4) const segmentPath = flightDataPath.slice(0, -flightDataPathLength)
return { return {
// TODO: Unify these two segment path helpers. We are inconsistently pushing an empty segment ("") // TODO: Unify these two segment path helpers. We are inconsistently pushing an empty segment ("")
...@@ -49,7 +53,7 @@ export function getFlightDataPartsFromPath( ...@@ -49,7 +53,7 @@ export function getFlightDataPartsFromPath(
seedData, seedData,
head, head,
isHeadPartial, isHeadPartial,
isRootRender: flightDataPath.length === 4, isRootRender: flightDataPath.length === flightDataPathLength,
} }
} }
......
...@@ -67,16 +67,19 @@ export function createMetadataComponents({ ...@@ -67,16 +67,19 @@ export function createMetadataComponents({
workStore: WorkStore workStore: WorkStore
MetadataBoundary: (props: { children: React.ReactNode }) => React.ReactNode MetadataBoundary: (props: { children: React.ReactNode }) => React.ReactNode
ViewportBoundary: (props: { children: React.ReactNode }) => React.ReactNode ViewportBoundary: (props: { children: React.ReactNode }) => React.ReactNode
}): [React.ComponentType, () => Promise<void>] { }): {
function MetadataRoot() { MetadataTree: React.ComponentType
ViewportTree: React.ComponentType
getMetadataReady: () => Promise<void>
getViewportReady: () => Promise<void>
} {
function ViewportTree() {
return ( return (
<> <>
<MetadataBoundary>
<Metadata />
</MetadataBoundary>
<ViewportBoundary> <ViewportBoundary>
<Viewport /> <Viewport />
</ViewportBoundary> </ViewportBoundary>
{/* This meta tag is for next/font which is still required to be blocking. */}
{appUsingSizeAdjustment ? ( {appUsingSizeAdjustment ? (
<meta name="next-size-adjust" content="" /> <meta name="next-size-adjust" content="" />
) : null} ) : null}
...@@ -84,6 +87,14 @@ export function createMetadataComponents({ ...@@ -84,6 +87,14 @@ export function createMetadataComponents({
) )
} }
function MetadataTree() {
return (
<MetadataBoundary>
<Metadata />
</MetadataBoundary>
)
}
async function viewport() { async function viewport() {
return getResolvedViewport( return getResolvedViewport(
tree, tree,
...@@ -156,13 +167,22 @@ export function createMetadataComponents({ ...@@ -156,13 +167,22 @@ export function createMetadataComponents({
} }
Metadata.displayName = METADATA_BOUNDARY_NAME Metadata.displayName = METADATA_BOUNDARY_NAME
async function getMetadataAndViewportReady(): Promise<void> { async function getMetadataReady(): Promise<void> {
await viewport()
await metadata() await metadata()
return undefined return undefined
} }
return [MetadataRoot, getMetadataAndViewportReady] async function getViewportReady(): Promise<void> {
await viewport()
return undefined
}
return {
ViewportTree,
MetadataTree,
getViewportReady,
getMetadataReady,
}
} }
const getResolvedMetadata = cache(getResolvedMetadataImpl) const getResolvedMetadata = cache(getResolvedMetadataImpl)
......
...@@ -223,7 +223,8 @@ interface ParseRequestHeadersOptions { ...@@ -223,7 +223,8 @@ interface ParseRequestHeadersOptions {
readonly isRoutePPREnabled: boolean readonly isRoutePPREnabled: boolean
} }
const flightDataPathHeadKey = 'h' const flightDataPathMetadataKey = 'h'
const flightDataPathViewportKey = 'v'
interface ParsedRequestHeaders { interface ParsedRequestHeaders {
/** /**
...@@ -445,21 +446,22 @@ async function generateDynamicRSCPayload( ...@@ -445,21 +446,22 @@ async function generateDynamicRSCPayload(
const preloadCallbacks: PreloadCallbacks = [] const preloadCallbacks: PreloadCallbacks = []
const searchParams = createServerSearchParamsForMetadata(query, workStore) const searchParams = createServerSearchParamsForMetadata(query, workStore)
const [MetadataTree, getMetadataReady] = createMetadataComponents({ const { ViewportTree, MetadataTree, getViewportReady, getMetadataReady } =
tree: loaderTree, createMetadataComponents({
searchParams, tree: loaderTree,
metadataContext: createTrackedMetadataContext( searchParams,
url.pathname, metadataContext: createTrackedMetadataContext(
ctx.renderOpts, url.pathname,
workStore ctx.renderOpts,
), workStore
getDynamicParamFromSegment, ),
appUsingSizeAdjustment, getDynamicParamFromSegment,
createServerParamsForMetadata, appUsingSizeAdjustment,
workStore, createServerParamsForMetadata,
MetadataBoundary, workStore,
ViewportBoundary, MetadataBoundary,
}) ViewportBoundary,
})
flightData = ( flightData = (
await walkTreeWithFlightRouterState({ await walkTreeWithFlightRouterState({
ctx, ctx,
...@@ -467,17 +469,23 @@ async function generateDynamicRSCPayload( ...@@ -467,17 +469,23 @@ async function generateDynamicRSCPayload(
parentParams: {}, parentParams: {},
flightRouterState, flightRouterState,
// For flight, render metadata inside leaf page // For flight, render metadata inside leaf page
rscPayloadHead: ( rscHead: [
<React.Fragment key={flightDataPathHeadKey}> <React.Fragment key={flightDataPathViewportKey}>
{/* noindex needs to be blocking */}
<NonIndex ctx={ctx} /> <NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */}
<ViewportTree key={requestId} />
</React.Fragment>,
<React.Fragment key={flightDataPathMetadataKey}>
{/* Adding requestId as react key to make metadata remount for each render */} {/* Adding requestId as react key to make metadata remount for each render */}
<MetadataTree key={requestId} /> <MetadataTree key={requestId} />
</React.Fragment> </React.Fragment>,
), ],
injectedCSS: new Set(), injectedCSS: new Set(),
injectedJS: new Set(), injectedJS: new Set(),
injectedFontPreloadTags: new Set(), injectedFontPreloadTags: new Set(),
rootLayoutIncluded: false, rootLayoutIncluded: false,
getViewportReady,
getMetadataReady, getMetadataReady,
preloadCallbacks, preloadCallbacks,
}) })
...@@ -733,22 +741,23 @@ async function getRSCPayload( ...@@ -733,22 +741,23 @@ async function getRSCPayload(
) )
const searchParams = createServerSearchParamsForMetadata(query, workStore) const searchParams = createServerSearchParamsForMetadata(query, workStore)
const [MetadataTree, getMetadataReady] = createMetadataComponents({ const { ViewportTree, MetadataTree, getViewportReady, getMetadataReady } =
tree, createMetadataComponents({
errorType: is404 ? 'not-found' : undefined, tree,
searchParams, errorType: is404 ? 'not-found' : undefined,
metadataContext: createTrackedMetadataContext( searchParams,
url.pathname, metadataContext: createTrackedMetadataContext(
ctx.renderOpts, url.pathname,
workStore ctx.renderOpts,
), workStore
getDynamicParamFromSegment, ),
appUsingSizeAdjustment, getDynamicParamFromSegment,
createServerParamsForMetadata, appUsingSizeAdjustment,
workStore, createServerParamsForMetadata,
MetadataBoundary, workStore,
ViewportBoundary, MetadataBoundary,
}) ViewportBoundary,
})
const preloadCallbacks: PreloadCallbacks = [] const preloadCallbacks: PreloadCallbacks = []
...@@ -760,6 +769,7 @@ async function getRSCPayload( ...@@ -760,6 +769,7 @@ async function getRSCPayload(
injectedJS, injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
rootLayoutIncluded: false, rootLayoutIncluded: false,
getViewportReady,
getMetadataReady, getMetadataReady,
missingSlots, missingSlots,
preloadCallbacks, preloadCallbacks,
...@@ -773,14 +783,20 @@ async function getRSCPayload( ...@@ -773,14 +783,20 @@ async function getRSCPayload(
const couldBeIntercepted = const couldBeIntercepted =
typeof varyHeader === 'string' && varyHeader.includes(NEXT_URL) typeof varyHeader === 'string' && varyHeader.includes(NEXT_URL)
const initialHead = ( const initialHeadMetadata = (
<React.Fragment key={flightDataPathHeadKey}> <React.Fragment key={flightDataPathMetadataKey}>
<NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */} {/* Adding requestId as react key to make metadata remount for each render */}
<MetadataTree key={ctx.requestId} /> <MetadataTree key={ctx.requestId} />
</React.Fragment> </React.Fragment>
) )
const initialHeadViewport = (
<React.Fragment key={flightDataPathViewportKey}>
<NonIndex ctx={ctx} />
<ViewportTree key={ctx.requestId} />
</React.Fragment>
)
const globalErrorStyles = await getGlobalErrorStyles(tree, ctx) const globalErrorStyles = await getGlobalErrorStyles(tree, ctx)
// Assume the head we're rendering contains only partial data if PPR is // Assume the head we're rendering contains only partial data if PPR is
...@@ -804,7 +820,7 @@ async function getRSCPayload( ...@@ -804,7 +820,7 @@ async function getRSCPayload(
[ [
initialTree, initialTree,
seedData, seedData,
initialHead, [initialHeadViewport, initialHeadMetadata],
isPossiblyPartialHead, isPossiblyPartialHead,
] as FlightDataPath, ] as FlightDataPath,
], ],
...@@ -850,7 +866,7 @@ async function getErrorRSCPayload( ...@@ -850,7 +866,7 @@ async function getErrorRSCPayload(
} = ctx } = ctx
const searchParams = createServerSearchParamsForMetadata(query, workStore) const searchParams = createServerSearchParamsForMetadata(query, workStore)
const [MetadataTree] = createMetadataComponents({ const { MetadataTree, ViewportTree } = createMetadataComponents({
tree, tree,
searchParams, searchParams,
// We create an untracked metadata context here because we can't postpone // We create an untracked metadata context here because we can't postpone
...@@ -865,11 +881,17 @@ async function getErrorRSCPayload( ...@@ -865,11 +881,17 @@ async function getErrorRSCPayload(
ViewportBoundary, ViewportBoundary,
}) })
const initialHead = ( const initialHeadMetadata = (
<React.Fragment key={flightDataPathHeadKey}> <React.Fragment key={flightDataPathMetadataKey}>
<NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */} {/* Adding requestId as react key to make metadata remount for each render */}
<MetadataTree key={requestId} /> <MetadataTree key={requestId} />
</React.Fragment>
)
const initialHeadViewport = (
<React.Fragment key={flightDataPathViewportKey}>
<NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */}
<ViewportTree key={requestId} />
{process.env.NODE_ENV === 'development' && ( {process.env.NODE_ENV === 'development' && (
<meta name="next-error" content="not-found" /> <meta name="next-error" content="not-found" />
)} )}
...@@ -911,7 +933,7 @@ async function getErrorRSCPayload( ...@@ -911,7 +933,7 @@ async function getErrorRSCPayload(
[ [
initialTree, initialTree,
initialSeedData, initialSeedData,
initialHead, [initialHeadViewport, initialHeadMetadata],
isPossiblyPartialHead, isPossiblyPartialHead,
] as FlightDataPath, ] as FlightDataPath,
], ],
......
...@@ -17,14 +17,17 @@ import { ...@@ -17,14 +17,17 @@ import {
} from '../stream-utils/node-web-streams-helper' } from '../stream-utils/node-web-streams-helper'
import { UNDERSCORE_NOT_FOUND_ROUTE } from '../../api/constants' import { UNDERSCORE_NOT_FOUND_ROUTE } from '../../api/constants'
import { waitAtLeastOneReactRenderTask } from '../../lib/scheduler' import { waitAtLeastOneReactRenderTask } from '../../lib/scheduler'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime' import type {
HeadData,
LoadingModuleData,
} from '../../shared/lib/app-router-context.shared-runtime'
// Contains metadata about the route tree. The client must fetch this before // Contains metadata about the route tree. The client must fetch this before
// it can fetch any actual segment data. // it can fetch any actual segment data.
export type RootTreePrefetch = { export type RootTreePrefetch = {
buildId: string buildId: string
tree: TreePrefetch tree: TreePrefetch
head: React.ReactNode | null head: HeadData
isHeadPartial: boolean isHeadPartial: boolean
staleTime: number staleTime: number
} }
...@@ -178,7 +181,7 @@ async function PrefetchTreeData({ ...@@ -178,7 +181,7 @@ async function PrefetchTreeData({
} }
const flightRouterState: FlightRouterState = flightDataPaths[0][0] const flightRouterState: FlightRouterState = flightDataPaths[0][0]
const seedData: CacheNodeSeedData = flightDataPaths[0][1] const seedData: CacheNodeSeedData = flightDataPaths[0][1]
const head: React.ReactNode | null = flightDataPaths[0][2] const head: HeadData = flightDataPaths[0][2]
// Compute the route metadata tree by traversing the FlightRouterState. As we // Compute the route metadata tree by traversing the FlightRouterState. As we
// walk the tree, we will also spawn a task to produce a prefetch response for // walk the tree, we will also spawn a task to produce a prefetch response for
......
...@@ -30,6 +30,7 @@ export function createComponentTree(props: { ...@@ -30,6 +30,7 @@ export function createComponentTree(props: {
injectedJS: Set<string> injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
getMetadataReady: () => Promise<void> getMetadataReady: () => Promise<void>
getViewportReady: () => Promise<void>
ctx: AppRenderContext ctx: AppRenderContext
missingSlots?: Set<string> missingSlots?: Set<string>
preloadCallbacks: PreloadCallbacks preloadCallbacks: PreloadCallbacks
...@@ -63,6 +64,7 @@ async function createComponentTreeInternal({ ...@@ -63,6 +64,7 @@ async function createComponentTreeInternal({
injectedCSS, injectedCSS,
injectedJS, injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
getViewportReady,
getMetadataReady, getMetadataReady,
ctx, ctx,
missingSlots, missingSlots,
...@@ -75,6 +77,7 @@ async function createComponentTreeInternal({ ...@@ -75,6 +77,7 @@ async function createComponentTreeInternal({
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string> injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
getViewportReady: () => Promise<void>
getMetadataReady: () => Promise<void> getMetadataReady: () => Promise<void>
ctx: AppRenderContext ctx: AppRenderContext
missingSlots?: Set<string> missingSlots?: Set<string>
...@@ -481,11 +484,14 @@ async function createComponentTreeInternal({ ...@@ -481,11 +484,14 @@ async function createComponentTreeInternal({
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout, injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
// getMetadataReady is used to conditionally throw. In the case of parallel routes we will have more than one page // `getMetadataReady` and `getViewportReady` are used to conditionally throw. In the case of parallel routes we will have more than one page
// but we only want to throw on the first one. // but we only want to throw on the first one.
getMetadataReady: isChildrenRouteKey getMetadataReady: isChildrenRouteKey
? getMetadataReady ? getMetadataReady
: () => Promise.resolve(), : () => Promise.resolve(),
getViewportReady: isChildrenRouteKey
? getViewportReady
: () => Promise.resolve(),
ctx, ctx,
missingSlots, missingSlots,
preloadCallbacks, preloadCallbacks,
...@@ -645,6 +651,7 @@ async function createComponentTreeInternal({ ...@@ -645,6 +651,7 @@ async function createComponentTreeInternal({
{pageElement} {pageElement}
{layerAssets} {layerAssets}
<OutletBoundary> <OutletBoundary>
<MetadataOutlet ready={getViewportReady} />
<MetadataOutlet ready={getMetadataReady} /> <MetadataOutlet ready={getMetadataReady} />
</OutletBoundary> </OutletBoundary>
</React.Fragment>, </React.Fragment>,
......
...@@ -104,7 +104,7 @@ export type FlightDataSegment = [ ...@@ -104,7 +104,7 @@ export type FlightDataSegment = [
/* segment of the rendered slice: */ Segment, /* segment of the rendered slice: */ Segment,
/* treePatch */ FlightRouterState, /* treePatch */ FlightRouterState,
/* cacheNodeSeedData */ CacheNodeSeedData | null, // Can be null during prefetch if there's no loading component /* cacheNodeSeedData */ CacheNodeSeedData | null, // Can be null during prefetch if there's no loading component
/* head */ React.ReactNode | null, /* head: [viewport, metadata] */ [React.ReactNode, React.ReactNode],
/* isHeadPartial */ boolean, /* isHeadPartial */ boolean,
] ]
......
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
addSearchParamsIfPageSegment, addSearchParamsIfPageSegment,
} from '../../shared/lib/segment' } from '../../shared/lib/segment'
import { createComponentTree } from './create-component-tree' import { createComponentTree } from './create-component-tree'
import type { HeadData } from '../../shared/lib/app-router-context.shared-runtime'
/** /**
* Use router state to decide at what common layout to render the page. * Use router state to decide at what common layout to render the page.
...@@ -30,11 +31,12 @@ export async function walkTreeWithFlightRouterState({ ...@@ -30,11 +31,12 @@ export async function walkTreeWithFlightRouterState({
parentParams, parentParams,
flightRouterState, flightRouterState,
parentRendered, parentRendered,
rscPayloadHead, rscHead,
injectedCSS, injectedCSS,
injectedJS, injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
rootLayoutIncluded, rootLayoutIncluded,
getViewportReady,
getMetadataReady, getMetadataReady,
ctx, ctx,
preloadCallbacks, preloadCallbacks,
...@@ -43,12 +45,13 @@ export async function walkTreeWithFlightRouterState({ ...@@ -43,12 +45,13 @@ export async function walkTreeWithFlightRouterState({
parentParams: { [key: string]: string | string[] } parentParams: { [key: string]: string | string[] }
flightRouterState?: FlightRouterState flightRouterState?: FlightRouterState
parentRendered?: boolean parentRendered?: boolean
rscPayloadHead: React.ReactNode rscHead: HeadData
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string> injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
rootLayoutIncluded: boolean rootLayoutIncluded: boolean
getMetadataReady: () => Promise<void> getMetadataReady: () => Promise<void>
getViewportReady: () => Promise<void>
ctx: AppRenderContext ctx: AppRenderContext
preloadCallbacks: PreloadCallbacks preloadCallbacks: PreloadCallbacks
}): Promise<FlightDataPath[]> { }): Promise<FlightDataPath[]> {
...@@ -144,7 +147,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -144,7 +147,7 @@ export async function walkTreeWithFlightRouterState({
overriddenSegment, overriddenSegment,
routerState, routerState,
null, null,
null, [null, null],
false, false,
] satisfies FlightDataSegment, ] satisfies FlightDataSegment,
] ]
...@@ -161,6 +164,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -161,6 +164,7 @@ export async function walkTreeWithFlightRouterState({
injectedFontPreloadTags, injectedFontPreloadTags,
// This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too. // This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too.
rootLayoutIncluded, rootLayoutIncluded,
getViewportReady,
getMetadataReady, getMetadataReady,
preloadCallbacks, preloadCallbacks,
authInterrupts: experimental.authInterrupts, authInterrupts: experimental.authInterrupts,
...@@ -172,7 +176,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -172,7 +176,7 @@ export async function walkTreeWithFlightRouterState({
overriddenSegment, overriddenSegment,
routerState, routerState,
seedData, seedData,
rscPayloadHead, rscHead,
false, false,
] satisfies FlightDataSegment, ] satisfies FlightDataSegment,
] ]
...@@ -216,11 +220,12 @@ export async function walkTreeWithFlightRouterState({ ...@@ -216,11 +220,12 @@ export async function walkTreeWithFlightRouterState({
flightRouterState: flightRouterState:
flightRouterState && flightRouterState[1][parallelRouteKey], flightRouterState && flightRouterState[1][parallelRouteKey],
parentRendered: parentRendered || renderComponentsOnThisLevel, parentRendered: parentRendered || renderComponentsOnThisLevel,
rscPayloadHead, rscHead,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout, injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
getViewportReady,
getMetadataReady, getMetadataReady,
preloadCallbacks, preloadCallbacks,
}) })
......
...@@ -23,6 +23,9 @@ export type LoadingModuleData = ...@@ -23,6 +23,9 @@ export type LoadingModuleData =
| [React.JSX.Element, React.ReactNode, React.ReactNode] | [React.JSX.Element, React.ReactNode, React.ReactNode]
| null | null
/** metadata cache nodes: [viewport, metadata] */
export type HeadData = [React.ReactNode, React.ReactNode]
export type LazyCacheNode = { export type LazyCacheNode = {
/** /**
* When rsc is null, this is a lazily-initialized cache node. * When rsc is null, this is a lazily-initialized cache node.
...@@ -54,7 +57,8 @@ export type LazyCacheNode = { ...@@ -54,7 +57,8 @@ export type LazyCacheNode = {
lazyData: Promise<FetchServerResponseResult> | null lazyData: Promise<FetchServerResponseResult> | null
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
head: React.ReactNode
head: HeadData
loading: LoadingModuleData | Promise<LoadingModuleData> loading: LoadingModuleData | Promise<LoadingModuleData>
...@@ -96,7 +100,8 @@ export type ReadyCacheNode = { ...@@ -96,7 +100,8 @@ export type ReadyCacheNode = {
*/ */
lazyData: null lazyData: null
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
head: React.ReactNode
head: HeadData
loading: LoadingModuleData | Promise<LoadingModuleData> loading: LoadingModuleData | Promise<LoadingModuleData>
......
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