Unverified Commit d5aea2c5 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Add cacheNodeSeedData to RSC payload (#58566)

This adds an additional item to the FlightData type. The type of the
field is currently `null`, but eventually this slot will represent a
tree of data that is used to seed the cache nodes.

This is a fragile change because the FlightData type is not covered by
TypeScript. This was an intentional decision to optimize the size of the
Flight payload. It's made more tricky because there are many places in
the codebase that access the fields of FlightData using direct indexing,
e.g. `flightData[0]`.

To minimze the number of places I needed to update, I added the new
field to the end of the array. However, many places access the fields
using negative indexing (via the `slice` method), so I needed to update
all of those. I also had to change any place that checked the length of
the array.

In the future, when we introduce clever types like this that are
intentionally unsound, we should contain the unsoundness to a single
module by only accessing the type with getter functions. Something like:

```js
const treePath = getTreePatch(flightData);
const seedData = getCacheNodeSeedData(flightData);
```

and so on.

That way when we add a new field like this, we don't have to carefully
update every single place that accesses the type. (TypeScript lacks the
ability to mark a type as opaque, unfortunately, but I believe you can
simulate it in other ways. This is one feature I miss from Flow.)

I considered adding these getter methods as part of this PR, but since
we're in the middle of a larger refactor of the Flight response type,
I'd prefer to change as little as possible for now until we can land an
MVP of PPR for client navigations. (That being said, if we struggle to
land this, I'll reconsider.)

My strategy for finding the places that needed to update was to change
the type of FlightDataPath to a nonsense type (e.g. number) and track
down all the TypeScript errors. I also searched for all references to
variables named `flightDataPath` or similar. I also referred to a prior
PR, #42791, which added the `head` field to the FlightDataPath type.

Because of the nature of the change, there's a moderately elevated risk
that this will break something. However, if something does slip through
the cracks, it's likely to fail fast, given how common most of these
paths are; I expect any egregious oversights would be caught by our e2e
test suite.

<!-- 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 #

-->
parent 41d890aa
...@@ -11,14 +11,15 @@ export function applyFlightData( ...@@ -11,14 +11,15 @@ export function applyFlightData(
wasPrefetched: boolean = false wasPrefetched: boolean = false
): boolean { ): boolean {
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const [treePatch, subTreeData, head] = flightDataPath.slice(-3) const [treePatch, subTreeData, head /* , cacheNodeSeedData */] =
flightDataPath.slice(-4)
// Handles case where prefetch only returns the router tree patch without rendered components. // Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData === null) { if (subTreeData === null) {
return false return false
} }
if (flightDataPath.length === 3) { if (flightDataPath.length === 4) {
cache.status = CacheStates.READY cache.status = CacheStates.READY
cache.subTreeData = subTreeData cache.subTreeData = subTreeData
fillLazyItemsTillLeafWithHead( fillLazyItemsTillLeafWithHead(
......
...@@ -37,6 +37,7 @@ const getFlightData = (): FlightData => { ...@@ -37,6 +37,7 @@ const getFlightData = (): FlightData => {
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
} }
...@@ -52,8 +53,8 @@ describe('applyRouterStatePatchToTree', () => { ...@@ -52,8 +53,8 @@ describe('applyRouterStatePatchToTree', () => {
// Mirrors the way router-reducer values are passed in. // Mirrors the way router-reducer values are passed in.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
const [treePatch /*, subTreeData, head*/] = flightDataPath.slice(-3) const [treePatch /*, subTreeData, head*/] = flightDataPath.slice(-4)
const flightSegmentPath = flightDataPath.slice(0, -4) const flightSegmentPath = flightDataPath.slice(0, -5)
const newRouterStateTree = applyRouterStatePatchToTree( const newRouterStateTree = applyRouterStatePatchToTree(
['', ...flightSegmentPath], ['', ...flightSegmentPath],
......
...@@ -14,7 +14,7 @@ export function fillCacheWithNewSubTreeData( ...@@ -14,7 +14,7 @@ export function fillCacheWithNewSubTreeData(
flightDataPath: FlightDataPath, flightDataPath: FlightDataPath,
wasPrefetched?: boolean wasPrefetched?: boolean
): void { ): void {
const isLastEntry = flightDataPath.length <= 5 const isLastEntry = flightDataPath.length <= 6
const [parallelRouteKey, segment] = flightDataPath const [parallelRouteKey, segment] = flightDataPath
const cacheKey = createRouterCacheKey(segment) const cacheKey = createRouterCacheKey(segment)
......
...@@ -30,6 +30,7 @@ const getFlightData = (): FlightData => { ...@@ -30,6 +30,7 @@ const getFlightData = (): FlightData => {
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
} }
...@@ -88,7 +89,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -88,7 +89,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
// Mirrors the way router-reducer values are passed in. // Mirrors the way router-reducer values are passed in.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const [treePatch, _subTreeData, head] = flightDataPath.slice(-3) const [treePatch, _subTreeData, head] = flightDataPath.slice(-4)
fillLazyItemsTillLeafWithHead(cache, existingCache, treePatch, head) fillLazyItemsTillLeafWithHead(cache, existingCache, treePatch, head)
const expectedCache: CacheNode = { const expectedCache: CacheNode = {
......
...@@ -22,6 +22,7 @@ const getFlightData = (): FlightData => { ...@@ -22,6 +22,7 @@ const getFlightData = (): FlightData => {
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
} }
...@@ -79,7 +80,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -79,7 +80,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
// Mirrors the way router-reducer values are passed in. // Mirrors the way router-reducer values are passed in.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
const flightSegmentPath = flightDataPath.slice(0, -3) const flightSegmentPath = flightDataPath.slice(0, -4)
// @ts-expect-error TODO-APP: investigate why this is not a TS error in router-reducer. // @ts-expect-error TODO-APP: investigate why this is not a TS error in router-reducer.
cache.status = CacheStates.READY cache.status = CacheStates.READY
......
...@@ -59,7 +59,7 @@ function fastRefreshReducerImpl( ...@@ -59,7 +59,7 @@ function fastRefreshReducerImpl(
for (const flightDataPath of flightData) { for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned // FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) { if (flightDataPath.length !== 4) {
// TODO-APP: handle this case better // TODO-APP: handle this case better
console.log('REFRESH FAILED') console.log('REFRESH FAILED')
return state return state
......
...@@ -32,6 +32,7 @@ const flightData: FlightData = [ ...@@ -32,6 +32,7 @@ const flightData: FlightData = [
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
...@@ -63,6 +64,7 @@ const demographicsFlightData: FlightData = [ ...@@ -63,6 +64,7 @@ const demographicsFlightData: FlightData = [
<> <>
<title>Demographics Head</title> <title>Demographics Head</title>
</>, </>,
null,
], ],
] ]
......
...@@ -175,10 +175,10 @@ export function navigateReducer( ...@@ -175,10 +175,10 @@ export function navigateReducer(
for (const flightDataPath of flightData) { for (const flightDataPath of flightData) {
const flightSegmentPath = flightDataPath.slice( const flightSegmentPath = flightDataPath.slice(
0, 0,
-4 -5
) as unknown as FlightSegmentPath ) as unknown as FlightSegmentPath
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const treePatch = flightDataPath.slice(-3)[0] as FlightRouterState const treePatch = flightDataPath.slice(-4)[0] as FlightRouterState
// TODO-APP: remove '' // TODO-APP: remove ''
const flightSegmentPathWithLeadingEmpty = ['', ...flightSegmentPath] const flightSegmentPathWithLeadingEmpty = ['', ...flightSegmentPath]
......
...@@ -27,6 +27,7 @@ jest.mock('../fetch-server-response', () => { ...@@ -27,6 +27,7 @@ jest.mock('../fetch-server-response', () => {
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
return { return {
......
...@@ -36,6 +36,7 @@ jest.mock('../fetch-server-response', () => { ...@@ -36,6 +36,7 @@ jest.mock('../fetch-server-response', () => {
<> <>
<title>Linking page!</title> <title>Linking page!</title>
</>, </>,
null,
], ],
] ]
return { return {
......
...@@ -57,8 +57,8 @@ export function refreshReducer( ...@@ -57,8 +57,8 @@ export function refreshReducer(
cache.data = null cache.data = null
for (const flightDataPath of flightData) { for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned // FlightDataPath with more than four items means unexpected Flight data was returned
if (flightDataPath.length !== 3) { if (flightDataPath.length !== 4) {
// TODO-APP: handle this case better // TODO-APP: handle this case better
console.log('REFRESH FAILED') console.log('REFRESH FAILED')
return state return state
...@@ -95,7 +95,7 @@ export function refreshReducer( ...@@ -95,7 +95,7 @@ export function refreshReducer(
} }
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const [subTreeData, head] = flightDataPath.slice(-2) const [subTreeData, head] = flightDataPath.slice(-3)
// Handles case where prefetch only returns the router tree patch without rendered components. // Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) { if (subTreeData !== null) {
......
...@@ -206,7 +206,7 @@ export function serverActionReducer( ...@@ -206,7 +206,7 @@ export function serverActionReducer(
for (const flightDataPath of flightData) { for (const flightDataPath of flightData) {
// FlightDataPath with more than two items means unexpected Flight data was returned // FlightDataPath with more than two items means unexpected Flight data was returned
if (flightDataPath.length !== 3) { if (flightDataPath.length !== 4) {
// TODO-APP: handle this case better // TODO-APP: handle this case better
console.log('SERVER ACTION APPLY FAILED') console.log('SERVER ACTION APPLY FAILED')
return state return state
...@@ -235,7 +235,7 @@ export function serverActionReducer( ...@@ -235,7 +235,7 @@ export function serverActionReducer(
} }
// The one before last item is the router state tree patch // The one before last item is the router state tree patch
const [subTreeData, head] = flightDataPath.slice(-2) const [subTreeData, head] = flightDataPath.slice(-3)
// Handles case where prefetch only returns the router tree patch without rendered components. // Handles case where prefetch only returns the router tree patch without rendered components.
if (subTreeData !== null) { if (subTreeData !== null) {
......
...@@ -30,6 +30,7 @@ jest.mock('../fetch-server-response', () => { ...@@ -30,6 +30,7 @@ jest.mock('../fetch-server-response', () => {
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
return { return {
...@@ -61,6 +62,7 @@ const flightDataForPatch: FlightData = [ ...@@ -61,6 +62,7 @@ const flightDataForPatch: FlightData = [
<> <>
<title>Somewhere page!</title> <title>Somewhere page!</title>
</>, </>,
null,
], ],
] ]
......
...@@ -49,10 +49,10 @@ export function serverPatchReducer( ...@@ -49,10 +49,10 @@ export function serverPatchReducer(
let currentCache = state.cache let currentCache = state.cache
for (const flightDataPath of flightData) { for (const flightDataPath of flightData) {
// Slices off the last segment (which is at -4) as it doesn't exist in the tree yet // Slices off the last segment (which is at -5) as it doesn't exist in the tree yet
const flightSegmentPath = flightDataPath.slice(0, -4) const flightSegmentPath = flightDataPath.slice(0, -5)
const [treePatch] = flightDataPath.slice(-3, -2) const [treePatch] = flightDataPath.slice(-4, -3)
const newTree = applyRouterStatePatchToTree( const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove '' // TODO-APP: remove ''
['', ...flightSegmentPath], ['', ...flightSegmentPath],
......
...@@ -39,6 +39,7 @@ describe('shouldHardNavigate', () => { ...@@ -39,6 +39,7 @@ describe('shouldHardNavigate', () => {
<> <>
<title>About page!</title> <title>About page!</title>
</>, </>,
null,
], ],
] ]
} }
...@@ -50,7 +51,7 @@ describe('shouldHardNavigate', () => { ...@@ -50,7 +51,7 @@ describe('shouldHardNavigate', () => {
// Mirrors the way router-reducer values are passed in. // Mirrors the way router-reducer values are passed in.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
const flightSegmentPath = flightDataPath.slice(0, -3) const flightSegmentPath = flightDataPath.slice(0, -4)
const result = shouldHardNavigate( const result = shouldHardNavigate(
['', ...flightSegmentPath], ['', ...flightSegmentPath],
...@@ -96,6 +97,7 @@ describe('shouldHardNavigate', () => { ...@@ -96,6 +97,7 @@ describe('shouldHardNavigate', () => {
], ],
null, null,
null, null,
null,
], ],
] ]
} }
...@@ -107,7 +109,7 @@ describe('shouldHardNavigate', () => { ...@@ -107,7 +109,7 @@ describe('shouldHardNavigate', () => {
// Mirrors the way router-reducer values are passed in. // Mirrors the way router-reducer values are passed in.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
const flightSegmentPath = flightDataPath.slice(0, -3) const flightSegmentPath = flightDataPath.slice(0, -4)
const result = shouldHardNavigate( const result = shouldHardNavigate(
['', ...flightSegmentPath], ['', ...flightSegmentPath],
...@@ -153,6 +155,7 @@ describe('shouldHardNavigate', () => { ...@@ -153,6 +155,7 @@ describe('shouldHardNavigate', () => {
], ],
null, null,
null, null,
null,
], ],
] ]
} }
...@@ -164,7 +167,7 @@ describe('shouldHardNavigate', () => { ...@@ -164,7 +167,7 @@ describe('shouldHardNavigate', () => {
// Mirrors the way router-reducer values are passed in. // Mirrors the way router-reducer values are passed in.
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
const flightSegmentPath = flightDataPath.slice(0, -3) const flightSegmentPath = flightDataPath.slice(0, -4)
const result = shouldHardNavigate( const result = shouldHardNavigate(
['', ...flightSegmentPath], ['', ...flightSegmentPath],
......
...@@ -72,7 +72,8 @@ export type FlightDataPath = ...@@ -72,7 +72,8 @@ export type FlightDataPath =
/* segment of the rendered slice: */ Segment, /* segment of the rendered slice: */ Segment,
/* treePatch */ FlightRouterState, /* treePatch */ FlightRouterState,
/* subTreeData: */ React.ReactNode | null, // Can be null during prefetch if there's no loading component /* subTreeData: */ React.ReactNode | null, // Can be null during prefetch if there's no loading component
/* head */ React.ReactNode | null /* head */ React.ReactNode | null,
/* cacheNodeSeedData */ null
] ]
/** /**
......
...@@ -178,6 +178,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -178,6 +178,7 @@ export async function walkTreeWithFlightRouterState({
</> </>
) )
})(), })(),
null,
], ],
] ]
} }
......
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