Unverified Commit 500bc1fe authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

make CacheNode properties non-optional (#63219)

No behavior changes in this PR -- this is a refactor to remove the
optional types on `CacheNode` to be more explicit when we aren't copying
over a value and to help with monomorphization. There's still work to be
done to ensure consistent property order, which will come in a separate
PR.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2794
parent 2c9af97a
......@@ -182,6 +182,8 @@ export function createEmptyCacheNode(): CacheNode {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
......
......@@ -354,6 +354,7 @@ function InnerLayoutRouter({
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
......
......@@ -58,6 +58,9 @@ describe('createInitialRouterState', () => {
lazyData: null,
rsc: children,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -77,6 +80,8 @@ describe('createInitialRouterState', () => {
prefetchRsc: null,
parallelRoutes: new Map(),
head: <title>Test</title>,
prefetchHead: null,
lazyDataResolved: false,
},
],
]),
......@@ -85,6 +90,9 @@ describe('createInitialRouterState', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
},
],
]),
......
......@@ -40,8 +40,11 @@ export function createInitialRouterState({
lazyData: null,
rsc: rsc,
prefetchRsc: 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,
lazyDataResolved: false,
}
const prefetchCache = new Map<string, PrefetchCacheEntry>()
......
......@@ -25,12 +25,18 @@ describe('fillCacheWithDataProperty', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
const existingCache: CacheNode = {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -41,6 +47,9 @@ describe('fillCacheWithDataProperty', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
lazyDataResolved: false,
head: null,
prefetchHead: null,
parallelRoutes: new Map([
[
'children',
......@@ -51,7 +60,10 @@ describe('fillCacheWithDataProperty', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
},
],
]),
......@@ -70,16 +82,23 @@ describe('fillCacheWithDataProperty', () => {
expect(cache).toMatchInlineSnapshot(`
{
"head": null,
"lazyData": null,
"lazyDataResolved": false,
"parallelRoutes": Map {
"children" => Map {
"linking" => {
"head": null,
"lazyData": null,
"lazyDataResolved": false,
"parallelRoutes": Map {
"children" => Map {
"" => {
"head": null,
"lazyData": null,
"lazyDataResolved": false,
"parallelRoutes": Map {},
"prefetchHead": null,
"prefetchRsc": null,
"rsc": <React.Fragment>
Page
......@@ -87,19 +106,24 @@ describe('fillCacheWithDataProperty', () => {
},
},
},
"prefetchHead": null,
"prefetchRsc": null,
"rsc": <React.Fragment>
Linking
</React.Fragment>,
},
"dashboard" => {
"head": null,
"lazyData": Promise {},
"lazyDataResolved": false,
"parallelRoutes": Map {},
"prefetchHead": null,
"prefetchRsc": null,
"rsc": null,
},
},
},
"prefetchHead": null,
"prefetchRsc": null,
"rsc": null,
}
......
......@@ -41,7 +41,10 @@ export function fillCacheWithDataProperty(
lazyData: fetchResponse(),
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
})
}
return
......@@ -54,7 +57,10 @@ export function fillCacheWithDataProperty(
lazyData: fetchResponse(),
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
})
}
return
......@@ -65,7 +71,10 @@ export function fillCacheWithDataProperty(
lazyData: childCacheNode.lazyData,
rsc: childCacheNode.rsc,
prefetchRsc: childCacheNode.prefetchRsc,
head: childCacheNode.head,
prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: childCacheNode.lazyDataResolved,
} as CacheNode
childSegmentMap.set(cacheKey, childCacheNode)
}
......
......@@ -30,12 +30,18 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
const existingCache: CacheNode = {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -46,6 +52,9 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -56,6 +65,9 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map(),
},
],
......@@ -84,6 +96,9 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -94,6 +109,9 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -105,6 +123,9 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map(),
},
],
......@@ -112,6 +133,9 @@ describe('fillCacheWithNewSubtreeData', () => {
'about',
{
lazyData: null,
lazyDataResolved: false,
head: null,
prefetchHead: null,
parallelRoutes: new Map([
[
'children',
......@@ -123,6 +147,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: null,
prefetchRsc: null,
parallelRoutes: new Map(),
prefetchHead: null,
head: (
<>
<title>Head Injected!</title>
......
......@@ -52,10 +52,13 @@ export function fillCacheWithNewSubTreeData(
lazyData: null,
rsc,
prefetchRsc: null,
head: null,
prefetchHead: null,
// Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes)
: new Map(),
lazyDataResolved: false,
}
if (existingChildCacheNode) {
......@@ -91,7 +94,10 @@ export function fillCacheWithNewSubTreeData(
lazyData: childCacheNode.lazyData,
rsc: childCacheNode.rsc,
prefetchRsc: childCacheNode.prefetchRsc,
head: childCacheNode.head,
prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: false,
} as CacheNode
childSegmentMap.set(cacheKey, childCacheNode)
}
......
......@@ -39,12 +39,18 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
const existingCache: CacheNode = {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -55,6 +61,9 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -65,6 +74,9 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map(),
},
],
......@@ -99,6 +111,9 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -109,6 +124,9 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -117,6 +135,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
'about',
{
lazyData: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -127,7 +146,9 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
head: (
<>
<title>About page!</title>
......@@ -140,6 +161,8 @@ describe('fillLazyItemsTillLeafWithHead', () => {
]),
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
},
],
[
......@@ -148,7 +171,10 @@ describe('fillLazyItemsTillLeafWithHead', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
},
],
]),
......
......@@ -65,7 +65,10 @@ export function fillLazyItemsTillLeafWithHead(
// `prefetchRsc`. As an incremental step, we'll just de-opt to the
// old behavior — no PPR value.
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false,
}
} else if (hasReusablePrefetch && existingCacheNode) {
// No new data was sent from the server, but the existing cache node
......@@ -77,7 +80,10 @@ export function fillLazyItemsTillLeafWithHead(
// just cloning the existing cache node, we might as well keep the
// PPR value, if it exists.
prefetchRsc: existingCacheNode.prefetchRsc,
head: existingCacheNode.head,
prefetchHead: existingCacheNode.prefetchHead,
parallelRoutes: new Map(existingCacheNode.parallelRoutes),
lazyDataResolved: existingCacheNode.lazyDataResolved,
} as CacheNode
} else {
// No data available for this node. This will trigger a lazy fetch
......@@ -86,7 +92,10 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false,
}
}
......@@ -115,7 +124,10 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null,
rsc: seedNode,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
} else {
// No data available for this node. This will trigger a lazy fetch
......@@ -124,7 +136,10 @@ export function fillLazyItemsTillLeafWithHead(
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
}
......
......@@ -31,12 +31,18 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
const existingCache: CacheNode = {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -47,6 +53,9 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -57,6 +66,9 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map(),
},
],
......@@ -95,6 +107,9 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
const expectedCache: CacheNode = {
lazyData: null,
lazyDataResolved: false,
head: null,
prefetchHead: null,
parallelRoutes: new Map([
[
'children',
......@@ -103,6 +118,9 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
'linking',
{
lazyData: null,
lazyDataResolved: false,
head: null,
prefetchHead: null,
parallelRoutes: new Map([
[
'children',
......@@ -114,6 +132,9 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
parallelRoutes: new Map(),
rsc: <React.Fragment>Page</React.Fragment>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
},
],
]),
......
......@@ -50,7 +50,10 @@ export function invalidateCacheBelowFlightSegmentPath(
lazyData: childCacheNode.lazyData,
rsc: childCacheNode.rsc,
prefetchRsc: childCacheNode.prefetchRsc,
head: childCacheNode.head,
prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: childCacheNode.lazyDataResolved,
} as CacheNode
childSegmentMap.set(cacheKey, childCacheNode)
}
......
......@@ -9,12 +9,18 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
}
const existingCache: CacheNode = {
lazyData: null,
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -25,6 +31,9 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -35,6 +44,9 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: <>Page</>,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map(),
},
],
......@@ -74,6 +86,9 @@ describe('invalidateCacheByRouterState', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([['children', new Map()]]),
}
......
......@@ -236,6 +236,7 @@ export function updateCacheNodeOnNavigation(
// Everything is cloned except for the children, which we computed above.
parallelRoutes: prefetchParallelRoutes,
lazyDataResolved: false,
}
return {
......@@ -523,6 +524,7 @@ function createPendingCacheNode(
// response is received from the server.
rsc: createDeferredRsc(),
head: isLeafSegment ? createDeferredRsc() : null,
lazyDataResolved: false,
}
}
......@@ -763,6 +765,7 @@ export function updateCacheNodeOnPopstateRestoration(
// These are the cloned children we computed above
parallelRoutes: newParallelRoutes,
lazyDataResolved: false,
}
}
......
......@@ -29,6 +29,9 @@ describe('findHeadInCache', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -39,6 +42,9 @@ describe('findHeadInCache', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map([
[
'children',
......@@ -47,6 +53,9 @@ describe('findHeadInCache', () => {
'about',
{
lazyData: null,
lazyDataResolved: false,
head: null,
prefetchHead: null,
parallelRoutes: new Map([
[
'children',
......@@ -57,6 +66,8 @@ describe('findHeadInCache', () => {
lazyData: null,
rsc: null,
prefetchRsc: null,
prefetchHead: null,
lazyDataResolved: false,
parallelRoutes: new Map(),
head: (
<>
......
......@@ -21,7 +21,7 @@ export type LazyCacheNode = {
* Whether the lazy cache node data promise has been resolved.
* This value is only true after we've called `use` on the promise (and applied the data to the tree).
*/
lazyDataResolved?: boolean
lazyDataResolved: boolean
/**
* When rsc is null, this is a lazily-initialized cache node.
*
......@@ -51,12 +51,8 @@ export type LazyCacheNode = {
*/
lazyData: Promise<FetchServerResponseResult> | null
// TODO: We should make both of these non-optional. Most of the places that
// clone the Cache Nodes do not preserve this field. In practice this ends up
// working out because we only clone nodes when we're receiving a new head,
// anyway. But it's fragile. It also breaks monomorphization.
prefetchHead?: React.ReactNode
head?: React.ReactNode
prefetchHead: React.ReactNode
head: React.ReactNode
/**
* Child parallel routes.
*/
......@@ -68,7 +64,7 @@ export type ReadyCacheNode = {
* Whether the lazy cache node data promise has been resolved.
* This value is only true after we've called `use` on the promise (and applied the data to the tree).
*/
lazyDataResolved?: boolean
lazyDataResolved: boolean
/**
* When rsc is not null, it represents the RSC data for the
* corresponding segment.
......@@ -99,8 +95,8 @@ export type ReadyCacheNode = {
* There should never be a lazy data request in this case.
*/
lazyData: null
prefetchHead?: React.ReactNode
head?: React.ReactNode
prefetchHead: React.ReactNode
head: React.ReactNode
parallelRoutes: Map<string, ChildSegmentMap>
}
......
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