Unverified Commit eb6ea067 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] Change the array head to single node in flight data (#74299)

parent 15bb8ce5
......@@ -145,7 +145,7 @@ export function createEmptyCacheNode(): CacheNode {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......
......@@ -549,7 +549,7 @@ export default function OuterLayoutRouter({
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......
......@@ -172,7 +172,7 @@ function fillNewTreeWithOnlyLoadingSegments(
// copy the layout but null the page segment as that's not meant to be used
rsc: segmentForParallelRoute.includes(PAGE_SEGMENT_KEY) ? null : rsc,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading,
......@@ -184,7 +184,7 @@ function fillNewTreeWithOnlyLoadingSegments(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......
......@@ -16,7 +16,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......@@ -25,7 +25,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -38,7 +38,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -51,7 +51,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......@@ -71,28 +71,19 @@ describe('clearCacheNodeDataForSegmentPath', () => {
expect(cache).toMatchInlineSnapshot(`
{
"head": [
null,
null,
],
"head": null,
"lazyData": null,
"loading": null,
"parallelRoutes": Map {
"children" => Map {
"linking" => {
"head": [
null,
null,
],
"head": null,
"lazyData": null,
"loading": null,
"parallelRoutes": Map {
"children" => Map {
"" => {
"head": [
null,
null,
],
"head": null,
"lazyData": null,
"loading": null,
"parallelRoutes": Map {},
......@@ -111,10 +102,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
</React.Fragment>,
},
"dashboard" => {
"head": [
null,
null,
],
"head": null,
"lazyData": null,
"loading": null,
"parallelRoutes": Map {},
......
......@@ -40,7 +40,7 @@ export function clearCacheNodeDataForSegmentPath(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......@@ -56,7 +56,7 @@ export function clearCacheNodeDataForSegmentPath(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......
......@@ -32,9 +32,7 @@ describe('createInitialRouterState', () => {
const initialParallelRoutes: CacheNode['parallelRoutes'] = new Map()
const state = createInitialRouterState({
initialFlightData: [
[initialTree, ['', children, {}, null], <title>Test</title>],
],
initialFlightData: [[initialTree, ['', children, {}, null]]],
initialCanonicalUrlParts: initialCanonicalUrl.split('/'),
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
......@@ -44,9 +42,7 @@ describe('createInitialRouterState', () => {
})
const state2 = createInitialRouterState({
initialFlightData: [
[initialTree, ['', children, {}, null], <title>Test</title>],
],
initialFlightData: [[initialTree, ['', children, {}, null]]],
initialCanonicalUrlParts: initialCanonicalUrl.split('/'),
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
......@@ -59,7 +55,7 @@ describe('createInitialRouterState', () => {
lazyData: null,
rsc: children,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -81,7 +77,7 @@ describe('createInitialRouterState', () => {
prefetchRsc: null,
parallelRoutes: new Map(),
loading: null,
head: [null, <title>Test</title>],
head: null,
prefetchHead: null,
},
],
......@@ -91,7 +87,7 @@ describe('createInitialRouterState', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
},
......
......@@ -48,7 +48,7 @@ export function createInitialRouterState({
lazyData: null,
rsc,
prefetchRsc: null,
head: [null, null],
head: 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.
parallelRoutes: isServer ? new Map() : initialParallelRoutes,
......
......@@ -10,7 +10,7 @@ const getFlightData = (): NormalizedFlightData[] => {
segment: 'about',
tree: ['about', { children: ['', {}] }],
seedData: ['about', <h1>SubTreeData Injected!</h1>, {}, null, false],
head: [null, '<title>Head Injected!</title>'],
head: null,
isHeadPartial: false,
isRootRender: false,
},
......@@ -23,7 +23,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -32,7 +32,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -45,7 +45,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -58,7 +58,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -89,7 +89,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -102,7 +102,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -116,7 +116,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -126,7 +126,7 @@ describe('fillCacheWithNewSubtreeData', () => {
'about',
{
lazyData: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -142,12 +142,7 @@ describe('fillCacheWithNewSubtreeData', () => {
parallelRoutes: new Map(),
prefetchHead: null,
loading: null,
head: [
null,
<>
<title>Head Injected!</title>
</>,
],
head: null,
},
],
]),
......
......@@ -71,7 +71,7 @@ function fillCacheHelper(
rsc:
fillLazyItems || incomingSegment !== PAGE_SEGMENT_KEY ? rsc : null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading,
parallelRoutes:
......
......@@ -39,7 +39,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......@@ -48,7 +48,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -61,7 +61,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -74,7 +74,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -114,7 +114,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -127,7 +127,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -152,12 +152,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
head: [
null,
<>
<title>About page!</title>
</>,
],
head: null,
},
],
]),
......@@ -165,7 +160,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
]),
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
},
],
......@@ -175,7 +170,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......
import type {
CacheNode,
HeadData,
} from '../../../shared/lib/app-router-context.shared-runtime'
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type {
FlightRouterState,
CacheNodeSeedData,
......@@ -17,7 +14,7 @@ export function fillLazyItemsTillLeafWithHead(
existingCache: CacheNode | undefined,
routerState: FlightRouterState,
cacheNodeSeedData: CacheNodeSeedData | null,
head: HeadData,
head: React.ReactNode,
prefetchEntry: PrefetchCacheEntry | undefined
): void {
const isLastSegment = Object.keys(routerState[1]).length === 0
......@@ -69,7 +66,7 @@ export function fillLazyItemsTillLeafWithHead(
// `prefetchRsc`. As an incremental step, we'll just de-opt to the
// old behavior — no PPR value.
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
......@@ -96,7 +93,7 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
loading: null,
......@@ -129,7 +126,7 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null,
rsc: seedNode,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading,
......@@ -141,7 +138,7 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
loading: null,
......
......@@ -12,7 +12,7 @@ const getFlightData = (): NormalizedFlightData[] => {
segment: 'about',
tree: ['about', { children: ['', {}] }],
seedData: ['about', <h1>About Page!</h1>, {}, null, false],
head: [null, '<title>About page!</title>'],
head: null,
isHeadPartial: false,
isRootRender: false,
},
......@@ -25,7 +25,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -34,7 +34,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -47,7 +47,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -60,7 +60,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -100,7 +100,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
const expectedCache: CacheNode = {
lazyData: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -111,7 +111,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
'linking',
{
lazyData: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -126,7 +126,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
parallelRoutes: new Map(),
rsc: <React.Fragment>Page</React.Fragment>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
},
],
......
......@@ -8,7 +8,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -17,7 +17,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -30,7 +30,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -43,7 +43,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
......@@ -85,7 +85,7 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([['children', new Map()]]),
......
......@@ -387,10 +387,7 @@ function createCacheNodeOnNavigation(
// `prefetchRsc` field.
rsc,
prefetchRsc: null,
head: (isLeafSegment ? possiblyPartialPrefetchHead : null) ?? [
null,
null,
],
head: isLeafSegment ? possiblyPartialPrefetchHead : null,
prefetchHead: null,
loading,
parallelRoutes: cacheNodeChildren,
......@@ -698,14 +695,7 @@ function createPendingCacheNode(
// Create a deferred promise. This will be fulfilled once the dynamic
// response is received from the server.
rsc: createDeferredRsc() as React.ReactNode,
head: isLeafSegment
? [
// TODO: change head back to ReactNode when metadata
// is stably rendered in body
createDeferredRsc() as React.ReactNode,
null,
]
: [null, null],
head: isLeafSegment ? (createDeferredRsc() as React.ReactNode) : null,
}
}
......@@ -810,8 +800,8 @@ function finishPendingCacheNode(
// TODO: change head back to ReactNode when metadata
// is stably rendered in body
// Handle head[0] - viewport
if (isDeferredRsc(head[0])) {
head[0].resolve(dynamicHead[0])
if (isDeferredRsc(head)) {
head.resolve(dynamicHead)
}
}
......
import React from 'react'
import type { FlightRouterState } from '../../../../server/app-render/types'
import type { CacheNode } from '../../../../shared/lib/app-router-context.shared-runtime'
import { findHeadInCache } from './find-head-in-cache'
......@@ -29,7 +28,7 @@ describe('findHeadInCache', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -42,7 +41,7 @@ describe('findHeadInCache', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -53,7 +52,7 @@ describe('findHeadInCache', () => {
'about',
{
lazyData: null,
head: [null, null],
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
......@@ -69,12 +68,7 @@ describe('findHeadInCache', () => {
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
head: [
null,
<>
<title>About page!</title>
</>,
],
head: null,
},
],
]),
......@@ -108,12 +102,7 @@ describe('findHeadInCache', () => {
expect(result).not.toBeNull()
const [cacheNode, key] = result!
expect(cacheNode.head[0]).toBe(null)
expect(cacheNode.head[1]).toMatchObject(
<>
<title>About page!</title>
</>
)
expect(cacheNode.head).toBe(null)
expect(key).toBe('/linking/about/')
})
})
......@@ -313,7 +313,7 @@ export function readOrCreateRouteCacheEntry(
status: EntryStatus.Empty,
blockedTasks: null,
tree: null,
head: [null, null],
head: null,
isHeadPartial: true,
// Since this is an empty entry, there's no reason to ever evict it. It will
// be updated when the data is populated.
......
......@@ -32,7 +32,7 @@ export type NormalizedFlightData = {
export function getFlightDataPartsFromPath(
flightDataPath: FlightDataPath
): NormalizedFlightData {
// Pick the last 4 items from the `FlightDataPath` to get the [tree, seedData, [viewport, metadata], isHeadPartial].
// Pick the last 4 items from the `FlightDataPath` to get the [tree, seedData, viewport, isHeadPartial].
const flightDataPathLength = 4
// tree, seedData, and head are *always* the last three items in the `FlightDataPath`.
const [tree, seedData, head, isHeadPartial] =
......
......@@ -6,7 +6,10 @@ import type { NextFontManifest } from '../../build/webpack/plugins/next-font-man
import type { ParsedUrlQuery } from 'querystring'
import type { AppPageModule } from '../route-modules/app-page/module'
import type { ExpireTime } from '../lib/revalidate'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime'
import type {
HeadData,
LoadingModuleData,
} from '../../shared/lib/app-router-context.shared-runtime'
import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import type { __ApiPreviewProps } from '../api-utils'
......@@ -131,7 +134,7 @@ export type FlightDataSegment = [
/* segment of the rendered slice: */ Segment,
/* treePatch */ FlightRouterState,
/* cacheNodeSeedData */ CacheNodeSeedData | null, // Can be null during prefetch if there's no loading component
/* head: [viewport, metadata] */ [React.ReactNode, React.ReactNode],
/* head: viewport */ HeadData,
/* isHeadPartial */ boolean,
]
......
......@@ -23,8 +23,8 @@ export type LoadingModuleData =
| [React.JSX.Element, React.ReactNode, React.ReactNode]
| null
/** metadata cache nodes: [viewport, metadata] */
export type HeadData = [React.ReactNode, React.ReactNode]
/** viewport metadata node */
export type HeadData = React.ReactNode
export type LazyCacheNode = {
/**
......
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