Skip to content

Commit 7d3f4c6

Browse files
committed
Update findHeadInCache to return entire Cache Node
For PPR, we will have two versions of the head: a prefetched version, and a full dynamic version. In preparation for this, I've updated findHeadInCache to return the entire Cache Node instead of just the `head` field, so in the future we can render either `head` or `prefetchHead`.
1 parent 63825de commit 7d3f4c6

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

packages/next/src/client/components/app-router.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,10 @@ function Router({
542542
const { cache, tree, nextUrl, focusAndScrollRef } =
543543
useUnwrapState(reducerState)
544544

545-
const head = useMemo(() => {
545+
const headCacheNode = useMemo(() => {
546546
return findHeadInCache(cache, tree[1])
547547
}, [cache, tree])
548+
const head = headCacheNode !== null ? headCacheNode.head : null
548549

549550
let content = (
550551
<RedirectBoundary>

packages/next/src/client/components/router-reducer/reducers/find-head-in-cache.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('findHeadInCache', () => {
9494

9595
const result = findHeadInCache(cache, routerTree[1])
9696

97-
expect(result).toMatchObject(
97+
expect(result?.head).toMatchObject(
9898
<>
9999
<title>About page!</title>
100100
</>

packages/next/src/client/components/router-reducer/reducers/find-head-in-cache.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { createRouterCacheKey } from '../create-router-cache-key'
55
export function findHeadInCache(
66
cache: CacheNode,
77
parallelRoutes: FlightRouterState[1]
8-
): React.ReactNode {
8+
): CacheNode | null {
99
const isLastItem = Object.keys(parallelRoutes).length === 0
1010
if (isLastItem) {
11-
return cache.head
11+
// Returns the entire Cache Node of the segment whose head we will render.
12+
return cache
1213
}
1314
for (const key in parallelRoutes) {
1415
const [segment, childParallelRoutes] = parallelRoutes[key]
@@ -30,5 +31,5 @@ export function findHeadInCache(
3031
}
3132
}
3233

33-
return undefined
34+
return null
3435
}

0 commit comments

Comments
 (0)