Skip to content

Commit

Permalink
Rename CacheNode.data -> .lazyData
Browse files Browse the repository at this point in the history
`CacheNode.data` is used to lazily kick off a request during render, and
represents the result of the entire Flight response. It doesn't
correspond directly to the RSC data of the cache node itself — that's
`subTreeData`. To complicate things further, I'm about to add another
field to CacheNode that represents prefetched RSC data.

To make it a little less confusing, I've renamed the `data` field to
`lazyData`. Still not perfectly clear on first glance, but it's at least
more specific. With PPR, the goal is to remove the lazy data fetching
mechanism in favor of initiating the request immediately upon
navigation. So this field will eventually go away.

In the next PR, I will rename `subTreeData`, too. Perhaps something with
"rsc" in the name so it's less generic than "data".
  • Loading branch information
acdlite committed Dec 11, 2023
1 parent f4206e8 commit 7598b38
Show file tree
Hide file tree
Showing 21 changed files with 191 additions and 191 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function HistoryUpdater({
}

export const createEmptyCacheNode = () => ({
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
})
Expand Down
16 changes: 8 additions & 8 deletions packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function InnerLayoutRouter({
// data request.
//
// TODO: An eventual goal of PPR is to remove this case entirely.
(childNode.subTreeData === null && childNode.data === null)
(childNode.subTreeData === null && childNode.lazyData === null)
) {
/**
* Router state with refetch marker added
Expand All @@ -354,7 +354,7 @@ function InnerLayoutRouter({
const refetchTree = walkAddRefetch(['', ...segmentPath], fullTree)

childNode = {
data: fetchServerResponse(
lazyData: fetchServerResponse(
new URL(url, location.origin),
refetchTree,
context.nextUrl,
Expand All @@ -377,20 +377,20 @@ function InnerLayoutRouter({
}

// This case should never happen so it throws an error. It indicates there's a bug in the Next.js.
if (childNode.subTreeData && childNode.data) {
throw new Error('Child node should not have both subTreeData and data')
if (childNode.subTreeData && childNode.lazyData) {
throw new Error('Child node should not have both subTreeData and lazyData')
}

// If cache node has a data request we have to unwrap response by `use` and update the cache.
if (childNode.data) {
if (childNode.lazyData) {
/**
* Flight response data
*/
// When the data has not resolved yet `use` will suspend here.
const [flightData, overrideCanonicalUrl] = use(childNode.data)
const [flightData, overrideCanonicalUrl] = use(childNode.lazyData)

// segmentPath from the server does not match the layout's segmentPath
childNode.data = null
childNode.lazyData = null

// setTimeout is used to start a new transition during render, this is an intentional hack around React.
setTimeout(() => {
Expand All @@ -402,7 +402,7 @@ function InnerLayoutRouter({
use(createInfinitePromise())
}

// If cache node has no subTreeData and no data request we have to infinitely suspend as the data will likely flow in from another place.
// If cache node has no subTreeData and no lazy data request we have to infinitely suspend as the data will likely flow in from another place.
// TODO-APP: double check users can't return null in a component that will kick in here.
if (!childNode.subTreeData) {
use(createInfinitePromise())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('createInitialRouterState', () => {
})

const expectedCache: CacheNode = {
data: null,
lazyData: null,
subTreeData: children,
parallelRoutes: new Map([
[
Expand All @@ -71,7 +71,7 @@ describe('createInitialRouterState', () => {
[
'',
{
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
head: <title>Test</title>,
Expand All @@ -80,7 +80,7 @@ describe('createInitialRouterState', () => {
]),
],
]),
data: null,
lazyData: null,
subTreeData: null,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function createInitialRouterState({
const subTreeData = initialSeedData[2]

const cache: CacheNode = {
data: null,
lazyData: null,
subTreeData: subTreeData,
// 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ describe('fillCacheWithDataProperty', () => {
.flat()

const cache: CacheNode = {
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
}
const existingCache: CacheNode = {
data: null,
lazyData: null,
subTreeData: <>Root layout</>,
parallelRoutes: new Map([
[
Expand All @@ -36,7 +36,7 @@ describe('fillCacheWithDataProperty', () => {
[
'linking',
{
data: null,
lazyData: null,
subTreeData: <>Linking</>,
parallelRoutes: new Map([
[
Expand All @@ -45,7 +45,7 @@ describe('fillCacheWithDataProperty', () => {
[
'',
{
data: null,
lazyData: null,
subTreeData: <>Page</>,
parallelRoutes: new Map(),
},
Expand All @@ -66,15 +66,15 @@ describe('fillCacheWithDataProperty', () => {

expect(cache).toMatchInlineSnapshot(`
{
"data": null,
"lazyData": null,
"parallelRoutes": Map {
"children" => Map {
"linking" => {
"data": null,
"lazyData": null,
"parallelRoutes": Map {
"children" => Map {
"" => {
"data": null,
"lazyData": null,
"parallelRoutes": Map {},
"subTreeData": <React.Fragment>
Page
Expand All @@ -87,7 +87,7 @@ describe('fillCacheWithDataProperty', () => {
</React.Fragment>,
},
"dashboard" => {
"data": Promise {},
"lazyData": Promise {},
"parallelRoutes": Map {},
"subTreeData": null,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export function fillCacheWithDataProperty(
if (isLastEntry) {
if (
!childCacheNode ||
!childCacheNode.data ||
!childCacheNode.lazyData ||
childCacheNode === existingChildCacheNode
) {
childSegmentMap.set(cacheKey, {
data: fetchResponse(),
lazyData: fetchResponse(),
subTreeData: null,
parallelRoutes: new Map(),
})
Expand All @@ -50,7 +50,7 @@ export function fillCacheWithDataProperty(
// Start fetch in the place where the existing cache doesn't have the data yet.
if (!childCacheNode) {
childSegmentMap.set(cacheKey, {
data: fetchResponse(),
lazyData: fetchResponse(),
subTreeData: null,
parallelRoutes: new Map(),
})
Expand All @@ -60,7 +60,7 @@ export function fillCacheWithDataProperty(

if (childCacheNode === existingChildCacheNode) {
childCacheNode = {
data: childCacheNode.data,
lazyData: childCacheNode.lazyData,
subTreeData: childCacheNode.subTreeData,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
} as CacheNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const getFlightData = (): FlightData => {
describe('fillCacheWithNewSubtreeData', () => {
it('should apply subTreeData and head property', () => {
const cache: CacheNode = {
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
}
const existingCache: CacheNode = {
data: null,
lazyData: null,
subTreeData: <>Root layout</>,
parallelRoutes: new Map([
[
Expand All @@ -41,7 +41,7 @@ describe('fillCacheWithNewSubtreeData', () => {
[
'linking',
{
data: null,
lazyData: null,
subTreeData: <>Linking</>,
parallelRoutes: new Map([
[
Expand All @@ -50,7 +50,7 @@ describe('fillCacheWithNewSubtreeData', () => {
[
'',
{
data: null,
lazyData: null,
subTreeData: <>Page</>,
parallelRoutes: new Map(),
},
Expand All @@ -77,7 +77,7 @@ describe('fillCacheWithNewSubtreeData', () => {
fillCacheWithNewSubTreeData(cache, existingCache, flightDataPath, false)

const expectedCache: CacheNode = {
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map([
[
Expand All @@ -86,7 +86,7 @@ describe('fillCacheWithNewSubtreeData', () => {
[
'linking',
{
data: null,
lazyData: null,
subTreeData: <>Linking</>,
parallelRoutes: new Map([
[
Expand All @@ -96,23 +96,23 @@ describe('fillCacheWithNewSubtreeData', () => {
[
'',
{
data: null,
lazyData: null,
subTreeData: <>Page</>,
parallelRoutes: new Map(),
},
],
[
'about',
{
data: null,
lazyData: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
head: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export function fillCacheWithNewSubTreeData(
if (isLastEntry) {
if (
!childCacheNode ||
!childCacheNode.data ||
!childCacheNode.lazyData ||
childCacheNode === existingChildCacheNode
) {
const seedData: CacheNodeSeedData = flightDataPath[3]
const subTreeData = seedData[2]
childCacheNode = {
data: null,
lazyData: null,
subTreeData,
// Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode
Expand Down Expand Up @@ -86,7 +86,7 @@ export function fillCacheWithNewSubTreeData(

if (childCacheNode === existingChildCacheNode) {
childCacheNode = {
data: childCacheNode.data,
lazyData: childCacheNode.lazyData,
subTreeData: childCacheNode.subTreeData,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
} as CacheNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ const getFlightData = (): FlightData => {
describe('fillLazyItemsTillLeafWithHead', () => {
it('should fill lazy items till leaf with head', () => {
const cache: CacheNode = {
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
}
const existingCache: CacheNode = {
data: null,
lazyData: null,
subTreeData: <>Root layout</>,
parallelRoutes: new Map([
[
Expand All @@ -50,7 +50,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
[
'linking',
{
data: null,
lazyData: null,
subTreeData: <>Linking</>,
parallelRoutes: new Map([
[
Expand All @@ -59,7 +59,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
[
'',
{
data: null,
lazyData: null,
subTreeData: <>Page</>,
parallelRoutes: new Map(),
},
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
)

const expectedCache: CacheNode = {
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map([
[
Expand All @@ -101,7 +101,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
[
'linking',
{
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map([
[
Expand All @@ -110,15 +110,15 @@ describe('fillLazyItemsTillLeafWithHead', () => {
[
'about',
{
data: null,
lazyData: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
data: null,
lazyData: null,
subTreeData: null,
parallelRoutes: new Map(),
head: (
Expand All @@ -137,7 +137,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
[
'',
{
data: null,
lazyData: null,
subTreeData: <>Page</>,
parallelRoutes: new Map(),
},
Expand Down

0 comments on commit 7598b38

Please sign in to comment.