Unverified Commit 5e3bac5c authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Store loading data in CacheNode (#62346)

This changes how loading components are provided to `LayoutRouter` to instead be part of the `CacheNode`. This makes it so that we can copy it over/expire it in a similar way to the `data` property on a `CacheNode`. Consequently, this fixes a bug in PPR navigations, where 2 different loading states (the prefetched one, followed by the updated one) might be displayed upon navigating. 

Note: This disables a PPR test that asserts the loading data gets expired after 5 minutes. As implemented, the loading data will remain cached for a particular segment. 

<!-- 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-2557
parent 39a8429a
...@@ -186,6 +186,7 @@ export function createEmptyCacheNode(): CacheNode { ...@@ -186,6 +186,7 @@ export function createEmptyCacheNode(): CacheNode {
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
} }
} }
...@@ -683,6 +684,7 @@ function Router({ ...@@ -683,6 +684,7 @@ function Router({
// Root node always has `url` // Root node always has `url`
// Provided in AppTreeContext to ensure it can be overwritten in layout-router // Provided in AppTreeContext to ensure it can be overwritten in layout-router
url: canonicalUrl, url: canonicalUrl,
loading: cache.loading,
}} }}
> >
{content} {content}
......
...@@ -357,6 +357,7 @@ function InnerLayoutRouter({ ...@@ -357,6 +357,7 @@ function InnerLayoutRouter({
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
} }
/** /**
...@@ -451,6 +452,7 @@ function InnerLayoutRouter({ ...@@ -451,6 +452,7 @@ function InnerLayoutRouter({
childNodes: childNode.parallelRoutes, childNodes: childNode.parallelRoutes,
// TODO-APP: overriding of url for parallel routes // TODO-APP: overriding of url for parallel routes
url: url, url: url,
loading: childNode.loading,
}} }}
> >
{resolvedRsc} {resolvedRsc}
...@@ -469,15 +471,13 @@ function LoadingBoundary({ ...@@ -469,15 +471,13 @@ function LoadingBoundary({
loading, loading,
loadingStyles, loadingStyles,
loadingScripts, loadingScripts,
hasLoading,
}: { }: {
children: React.ReactNode children: React.ReactNode
loading?: React.ReactNode loading?: React.ReactNode
loadingStyles?: React.ReactNode loadingStyles?: React.ReactNode
loadingScripts?: React.ReactNode loadingScripts?: React.ReactNode
hasLoading: boolean
}): JSX.Element { }): JSX.Element {
if (hasLoading) { if (loading) {
return ( return (
<Suspense <Suspense
fallback={ fallback={
...@@ -508,10 +508,6 @@ export default function OuterLayoutRouter({ ...@@ -508,10 +508,6 @@ export default function OuterLayoutRouter({
errorScripts, errorScripts,
templateStyles, templateStyles,
templateScripts, templateScripts,
loading,
loadingStyles,
loadingScripts,
hasLoading,
template, template,
notFound, notFound,
notFoundStyles, notFoundStyles,
...@@ -525,10 +521,6 @@ export default function OuterLayoutRouter({ ...@@ -525,10 +521,6 @@ export default function OuterLayoutRouter({
templateStyles: React.ReactNode | undefined templateStyles: React.ReactNode | undefined
templateScripts: React.ReactNode | undefined templateScripts: React.ReactNode | undefined
template: React.ReactNode template: React.ReactNode
loading: React.ReactNode | undefined
loadingStyles: React.ReactNode | undefined
loadingScripts: React.ReactNode | undefined
hasLoading: boolean
notFound: React.ReactNode | undefined notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined notFoundStyles: React.ReactNode | undefined
styles?: React.ReactNode styles?: React.ReactNode
...@@ -538,7 +530,7 @@ export default function OuterLayoutRouter({ ...@@ -538,7 +530,7 @@ export default function OuterLayoutRouter({
throw new Error('invariant expected layout router to be mounted') throw new Error('invariant expected layout router to be mounted')
} }
const { childNodes, tree, url } = context const { childNodes, tree, url, loading } = context
// Get the current parallelRouter cache node // Get the current parallelRouter cache node
let childNodesForParallelRouter = childNodes.get(parallelRouterKey) let childNodesForParallelRouter = childNodes.get(parallelRouterKey)
...@@ -589,10 +581,9 @@ export default function OuterLayoutRouter({ ...@@ -589,10 +581,9 @@ export default function OuterLayoutRouter({
errorScripts={errorScripts} errorScripts={errorScripts}
> >
<LoadingBoundary <LoadingBoundary
hasLoading={hasLoading} loading={loading?.[0]}
loading={loading} loadingStyles={loading?.[1]}
loadingStyles={loadingStyles} loadingScripts={loading?.[2]}
loadingScripts={loadingScripts}
> >
<NotFoundBoundary <NotFoundBoundary
notFound={notFound} notFound={notFound}
......
...@@ -20,6 +20,8 @@ export function applyFlightData( ...@@ -20,6 +20,8 @@ export function applyFlightData(
if (flightDataPath.length === 3) { if (flightDataPath.length === 3) {
const rsc = cacheNodeSeedData[2] const rsc = cacheNodeSeedData[2]
const loading = cacheNodeSeedData[3]
cache.loading = loading
cache.rsc = rsc cache.rsc = rsc
// This is a PPR-only field. When PPR is enabled, we shouldn't hit // This is a PPR-only field. When PPR is enabled, we shouldn't hit
// this path during a navigation, but until PPR is fully implemented // this path during a navigation, but until PPR is fully implemented
...@@ -43,6 +45,7 @@ export function applyFlightData( ...@@ -43,6 +45,7 @@ export function applyFlightData(
// PPR value, if it exists. // PPR value, if it exists.
cache.prefetchRsc = existingCache.prefetchRsc cache.prefetchRsc = existingCache.prefetchRsc
cache.parallelRoutes = new Map(existingCache.parallelRoutes) cache.parallelRoutes = new Map(existingCache.parallelRoutes)
cache.loading = existingCache.loading
// Create a copy of the existing cache with the rsc applied. // Create a copy of the existing cache with the rsc applied.
fillCacheWithNewSubTreeData( fillCacheWithNewSubTreeData(
cache, cache,
......
...@@ -37,7 +37,7 @@ describe('createInitialRouterState', () => { ...@@ -37,7 +37,7 @@ describe('createInitialRouterState', () => {
buildId, buildId,
initialTree, initialTree,
initialCanonicalUrl, initialCanonicalUrl,
initialSeedData: ['', {}, children], initialSeedData: ['', {}, children, null],
initialParallelRoutes, initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any, location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>, initialHead: <title>Test</title>,
...@@ -48,7 +48,7 @@ describe('createInitialRouterState', () => { ...@@ -48,7 +48,7 @@ describe('createInitialRouterState', () => {
buildId, buildId,
initialTree, initialTree,
initialCanonicalUrl, initialCanonicalUrl,
initialSeedData: ['', {}, children], initialSeedData: ['', {}, children, null],
initialParallelRoutes, initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any, location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>, initialHead: <title>Test</title>,
...@@ -61,6 +61,7 @@ describe('createInitialRouterState', () => { ...@@ -61,6 +61,7 @@ describe('createInitialRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -79,6 +80,7 @@ describe('createInitialRouterState', () => { ...@@ -79,6 +80,7 @@ describe('createInitialRouterState', () => {
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
loading: null,
head: <title>Test</title>, head: <title>Test</title>,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
...@@ -93,6 +95,7 @@ describe('createInitialRouterState', () => { ...@@ -93,6 +95,7 @@ describe('createInitialRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
}, },
], ],
]), ]),
......
...@@ -45,6 +45,7 @@ export function createInitialRouterState({ ...@@ -45,6 +45,7 @@ export function createInitialRouterState({
// The cache gets seeded during the first render. `initialParallelRoutes` ensures the cache from the first render is there during the second render. // 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, parallelRoutes: isServer ? new Map() : initialParallelRoutes,
lazyDataResolved: false, lazyDataResolved: false,
loading: initialSeedData[3],
} }
const prefetchCache = new Map<string, PrefetchCacheEntry>() const prefetchCache = new Map<string, PrefetchCacheEntry>()
......
...@@ -29,6 +29,7 @@ describe('fillCacheWithDataProperty', () => { ...@@ -29,6 +29,7 @@ describe('fillCacheWithDataProperty', () => {
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
} }
const existingCache: CacheNode = { const existingCache: CacheNode = {
lazyData: null, lazyData: null,
...@@ -37,6 +38,7 @@ describe('fillCacheWithDataProperty', () => { ...@@ -37,6 +38,7 @@ describe('fillCacheWithDataProperty', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -50,6 +52,7 @@ describe('fillCacheWithDataProperty', () => { ...@@ -50,6 +52,7 @@ describe('fillCacheWithDataProperty', () => {
lazyDataResolved: false, lazyDataResolved: false,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -64,6 +67,7 @@ describe('fillCacheWithDataProperty', () => { ...@@ -64,6 +67,7 @@ describe('fillCacheWithDataProperty', () => {
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
}, },
], ],
]), ]),
...@@ -85,18 +89,21 @@ describe('fillCacheWithDataProperty', () => { ...@@ -85,18 +89,21 @@ describe('fillCacheWithDataProperty', () => {
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
"loading": null,
"parallelRoutes": Map { "parallelRoutes": Map {
"children" => Map { "children" => Map {
"linking" => { "linking" => {
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
"loading": null,
"parallelRoutes": Map { "parallelRoutes": Map {
"children" => Map { "children" => Map {
"" => { "" => {
"head": null, "head": null,
"lazyData": null, "lazyData": null,
"lazyDataResolved": false, "lazyDataResolved": false,
"loading": null,
"parallelRoutes": Map {}, "parallelRoutes": Map {},
"prefetchHead": null, "prefetchHead": null,
"prefetchRsc": null, "prefetchRsc": null,
...@@ -116,6 +123,7 @@ describe('fillCacheWithDataProperty', () => { ...@@ -116,6 +123,7 @@ describe('fillCacheWithDataProperty', () => {
"head": null, "head": null,
"lazyData": Promise {}, "lazyData": Promise {},
"lazyDataResolved": false, "lazyDataResolved": false,
"loading": null,
"parallelRoutes": Map {}, "parallelRoutes": Map {},
"prefetchHead": null, "prefetchHead": null,
"prefetchRsc": null, "prefetchRsc": null,
......
...@@ -45,6 +45,7 @@ export function fillCacheWithDataProperty( ...@@ -45,6 +45,7 @@ export function fillCacheWithDataProperty(
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
}) })
} }
return return
...@@ -61,6 +62,7 @@ export function fillCacheWithDataProperty( ...@@ -61,6 +62,7 @@ export function fillCacheWithDataProperty(
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
}) })
} }
return return
...@@ -75,6 +77,7 @@ export function fillCacheWithDataProperty( ...@@ -75,6 +77,7 @@ export function fillCacheWithDataProperty(
prefetchHead: childCacheNode.prefetchHead, prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes), parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: childCacheNode.lazyDataResolved, lazyDataResolved: childCacheNode.lazyDataResolved,
loading: childCacheNode.loading,
} as CacheNode } as CacheNode
childSegmentMap.set(cacheKey, childCacheNode) childSegmentMap.set(cacheKey, childCacheNode)
} }
......
...@@ -32,6 +32,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -32,6 +32,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchRsc: null, prefetchRsc: null,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -42,6 +43,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -42,6 +43,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -55,6 +57,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -55,6 +57,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -68,6 +71,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -68,6 +71,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -99,6 +103,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -99,6 +103,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -112,6 +117,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -112,6 +117,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -126,6 +132,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -126,6 +132,7 @@ describe('fillCacheWithNewSubtreeData', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -136,6 +143,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -136,6 +143,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyDataResolved: false, lazyDataResolved: false,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -148,6 +156,7 @@ describe('fillCacheWithNewSubtreeData', () => { ...@@ -148,6 +156,7 @@ describe('fillCacheWithNewSubtreeData', () => {
prefetchRsc: null, prefetchRsc: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
prefetchHead: null, prefetchHead: null,
loading: null,
head: ( head: (
<> <>
<title>Head Injected!</title> <title>Head Injected!</title>
......
...@@ -48,12 +48,14 @@ export function fillCacheWithNewSubTreeData( ...@@ -48,12 +48,14 @@ export function fillCacheWithNewSubTreeData(
) { ) {
const seedData: CacheNodeSeedData = flightDataPath[3] const seedData: CacheNodeSeedData = flightDataPath[3]
const rsc = seedData[2] const rsc = seedData[2]
const loading = seedData[3]
childCacheNode = { childCacheNode = {
lazyData: null, lazyData: null,
rsc, rsc,
prefetchRsc: null, prefetchRsc: null,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading,
// Ensure segments other than the one we got data for are preserved. // Ensure segments other than the one we got data for are preserved.
parallelRoutes: existingChildCacheNode parallelRoutes: existingChildCacheNode
? new Map(existingChildCacheNode.parallelRoutes) ? new Map(existingChildCacheNode.parallelRoutes)
...@@ -98,6 +100,7 @@ export function fillCacheWithNewSubTreeData( ...@@ -98,6 +100,7 @@ export function fillCacheWithNewSubTreeData(
prefetchHead: childCacheNode.prefetchHead, prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes), parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: false, lazyDataResolved: false,
loading: childCacheNode.loading,
} as CacheNode } as CacheNode
childSegmentMap.set(cacheKey, childCacheNode) childSegmentMap.set(cacheKey, childCacheNode)
} }
......
...@@ -43,6 +43,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -43,6 +43,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
} }
const existingCache: CacheNode = { const existingCache: CacheNode = {
lazyData: null, lazyData: null,
...@@ -51,6 +52,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -51,6 +52,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -64,6 +66,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -64,6 +66,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -77,6 +80,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -77,6 +80,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -114,6 +118,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -114,6 +118,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -127,6 +132,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -127,6 +132,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -136,6 +142,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -136,6 +142,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
{ {
lazyData: null, lazyData: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -147,6 +154,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -147,6 +154,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
rsc: null, rsc: null,
prefetchRsc: null, prefetchRsc: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
head: ( head: (
...@@ -173,6 +181,7 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -173,6 +181,7 @@ describe('fillLazyItemsTillLeafWithHead', () => {
prefetchRsc: null, prefetchRsc: null,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
}, },
......
...@@ -56,6 +56,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -56,6 +56,7 @@ export function fillLazyItemsTillLeafWithHead(
if (parallelSeedData !== null) { if (parallelSeedData !== null) {
// New data was sent from the server. // New data was sent from the server.
const seedNode = parallelSeedData[2] const seedNode = parallelSeedData[2]
const loading = parallelSeedData[3]
newCacheNode = { newCacheNode = {
lazyData: null, lazyData: null,
rsc: seedNode, rsc: seedNode,
...@@ -67,6 +68,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -67,6 +68,7 @@ export function fillLazyItemsTillLeafWithHead(
prefetchRsc: null, prefetchRsc: null,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes), parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -84,6 +86,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -84,6 +86,7 @@ export function fillLazyItemsTillLeafWithHead(
prefetchHead: existingCacheNode.prefetchHead, prefetchHead: existingCacheNode.prefetchHead,
parallelRoutes: new Map(existingCacheNode.parallelRoutes), parallelRoutes: new Map(existingCacheNode.parallelRoutes),
lazyDataResolved: existingCacheNode.lazyDataResolved, lazyDataResolved: existingCacheNode.lazyDataResolved,
loading: existingCacheNode.loading,
} as CacheNode } as CacheNode
} else { } else {
// No data available for this node. This will trigger a lazy fetch // No data available for this node. This will trigger a lazy fetch
...@@ -96,6 +99,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -96,6 +99,7 @@ export function fillLazyItemsTillLeafWithHead(
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(existingCacheNode?.parallelRoutes), parallelRoutes: new Map(existingCacheNode?.parallelRoutes),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
} }
} }
...@@ -120,6 +124,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -120,6 +124,7 @@ export function fillLazyItemsTillLeafWithHead(
if (parallelSeedData !== null) { if (parallelSeedData !== null) {
// New data was sent from the server. // New data was sent from the server.
const seedNode = parallelSeedData[2] const seedNode = parallelSeedData[2]
const loading = parallelSeedData[3]
newCacheNode = { newCacheNode = {
lazyData: null, lazyData: null,
rsc: seedNode, rsc: seedNode,
...@@ -128,6 +133,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -128,6 +133,7 @@ export function fillLazyItemsTillLeafWithHead(
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading,
} }
} else { } else {
// No data available for this node. This will trigger a lazy fetch // No data available for this node. This will trigger a lazy fetch
...@@ -140,6 +146,7 @@ export function fillLazyItemsTillLeafWithHead( ...@@ -140,6 +146,7 @@ export function fillLazyItemsTillLeafWithHead(
prefetchHead: null, prefetchHead: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
} }
} }
......
...@@ -33,6 +33,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -33,6 +33,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
prefetchRsc: null, prefetchRsc: null,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -43,6 +44,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -43,6 +44,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -56,6 +58,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -56,6 +58,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -69,6 +72,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -69,6 +72,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -110,6 +114,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -110,6 +114,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyDataResolved: false, lazyDataResolved: false,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -121,6 +126,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -121,6 +126,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
lazyDataResolved: false, lazyDataResolved: false,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -129,6 +135,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => { ...@@ -129,6 +135,7 @@ describe('invalidateCacheBelowFlightSegmentPath', () => {
'', '',
{ {
lazyData: null, lazyData: null,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
rsc: <React.Fragment>Page</React.Fragment>, rsc: <React.Fragment>Page</React.Fragment>,
prefetchRsc: null, prefetchRsc: null,
......
...@@ -11,6 +11,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -11,6 +11,7 @@ describe('invalidateCacheByRouterState', () => {
prefetchRsc: null, prefetchRsc: null,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
lazyDataResolved: false, lazyDataResolved: false,
} }
...@@ -21,6 +22,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -21,6 +22,7 @@ describe('invalidateCacheByRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -34,6 +36,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -34,6 +36,7 @@ describe('invalidateCacheByRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -47,6 +50,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -47,6 +50,7 @@ describe('invalidateCacheByRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
}, },
], ],
...@@ -89,6 +93,7 @@ describe('invalidateCacheByRouterState', () => { ...@@ -89,6 +93,7 @@ describe('invalidateCacheByRouterState', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([['children', new Map()]]), parallelRoutes: new Map([['children', new Map()]]),
} }
......
...@@ -233,6 +233,7 @@ export function updateCacheNodeOnNavigation( ...@@ -233,6 +233,7 @@ export function updateCacheNodeOnNavigation(
prefetchRsc: oldCacheNode.prefetchRsc, prefetchRsc: oldCacheNode.prefetchRsc,
head: oldCacheNode.head, head: oldCacheNode.head,
prefetchHead: oldCacheNode.prefetchHead, prefetchHead: oldCacheNode.prefetchHead,
loading: oldCacheNode.loading,
// Everything is cloned except for the children, which we computed above. // Everything is cloned except for the children, which we computed above.
parallelRoutes: prefetchParallelRoutes, parallelRoutes: prefetchParallelRoutes,
...@@ -512,13 +513,14 @@ function createPendingCacheNode( ...@@ -512,13 +513,14 @@ function createPendingCacheNode(
const isLeafSegment = parallelRoutes.size === 0 const isLeafSegment = parallelRoutes.size === 0
const maybePrefetchRsc = prefetchData !== null ? prefetchData[2] : null const maybePrefetchRsc = prefetchData !== null ? prefetchData[2] : null
const maybePrefetchLoading = prefetchData !== null ? prefetchData[3] : null
return { return {
lazyData: null, lazyData: null,
parallelRoutes: parallelRoutes, parallelRoutes: parallelRoutes,
prefetchRsc: maybePrefetchRsc !== undefined ? maybePrefetchRsc : null, prefetchRsc: maybePrefetchRsc !== undefined ? maybePrefetchRsc : null,
prefetchHead: isLeafSegment ? prefetchHead : null, prefetchHead: isLeafSegment ? prefetchHead : null,
loading: maybePrefetchLoading !== undefined ? maybePrefetchLoading : null,
// Create a deferred promise. This will be fulfilled once the dynamic // Create a deferred promise. This will be fulfilled once the dynamic
// response is received from the server. // response is received from the server.
...@@ -762,6 +764,7 @@ export function updateCacheNodeOnPopstateRestoration( ...@@ -762,6 +764,7 @@ export function updateCacheNodeOnPopstateRestoration(
prefetchHead: shouldUsePrefetch ? oldCacheNode.prefetchHead : null, prefetchHead: shouldUsePrefetch ? oldCacheNode.prefetchHead : null,
prefetchRsc: shouldUsePrefetch ? oldCacheNode.prefetchRsc : null, prefetchRsc: shouldUsePrefetch ? oldCacheNode.prefetchRsc : null,
loading: shouldUsePrefetch ? oldCacheNode.loading : null,
// These are the cloned children we computed above // These are the cloned children we computed above
parallelRoutes: newParallelRoutes, parallelRoutes: newParallelRoutes,
......
...@@ -32,6 +32,7 @@ describe('findHeadInCache', () => { ...@@ -32,6 +32,7 @@ describe('findHeadInCache', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -45,6 +46,7 @@ describe('findHeadInCache', () => { ...@@ -45,6 +46,7 @@ describe('findHeadInCache', () => {
head: null, head: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -56,6 +58,7 @@ describe('findHeadInCache', () => { ...@@ -56,6 +58,7 @@ describe('findHeadInCache', () => {
lazyDataResolved: false, lazyDataResolved: false,
head: null, head: null,
prefetchHead: null, prefetchHead: null,
loading: null,
parallelRoutes: new Map([ parallelRoutes: new Map([
[ [
'children', 'children',
...@@ -68,6 +71,7 @@ describe('findHeadInCache', () => { ...@@ -68,6 +71,7 @@ describe('findHeadInCache', () => {
prefetchRsc: null, prefetchRsc: null,
prefetchHead: null, prefetchHead: null,
lazyDataResolved: false, lazyDataResolved: false,
loading: null,
parallelRoutes: new Map(), parallelRoutes: new Map(),
head: ( head: (
<> <>
......
...@@ -536,6 +536,7 @@ async function ReactServerError({ ...@@ -536,6 +536,7 @@ async function ReactServerError({
<head></head> <head></head>
<body></body> <body></body>
</html>, </html>,
null,
] ]
return ( return (
<AppRouter <AppRouter
......
...@@ -14,6 +14,7 @@ import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/parallel-ro ...@@ -14,6 +14,7 @@ import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/parallel-ro
import { getTracer } from '../lib/trace/tracer' import { getTracer } from '../lib/trace/tracer'
import { NextNodeServerSpan } from '../lib/trace/constants' import { NextNodeServerSpan } from '../lib/trace/constants'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime'
type ComponentTree = { type ComponentTree = {
seedData: CacheNodeSeedData seedData: CacheNodeSeedData
...@@ -457,11 +458,7 @@ async function createComponentTreeInternal({ ...@@ -457,11 +458,7 @@ async function createComponentTreeInternal({
<LayoutRouter <LayoutRouter
parallelRouterKey={parallelRouteKey} parallelRouterKey={parallelRouteKey}
segmentPath={createSegmentPath(currentSegmentPath)} segmentPath={createSegmentPath(currentSegmentPath)}
loading={Loading ? <Loading /> : undefined}
loadingStyles={loadingStyles}
loadingScripts={loadingScripts}
// TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning. // TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning.
hasLoading={Boolean(Loading)}
error={ErrorComponent} error={ErrorComponent}
errorStyles={errorStyles} errorStyles={errorStyles}
errorScripts={errorScripts} errorScripts={errorScripts}
...@@ -493,6 +490,10 @@ async function createComponentTreeInternal({ ...@@ -493,6 +490,10 @@ async function createComponentTreeInternal({
parallelRouteCacheNodeSeedData[parallelRouteKey] = flightData parallelRouteCacheNodeSeedData[parallelRouteKey] = flightData
} }
const loadingData: LoadingModuleData = Loading
? [<Loading />, loadingStyles, loadingScripts]
: null
// When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component // When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component
if (!Component) { if (!Component) {
return { return {
...@@ -505,6 +506,7 @@ async function createComponentTreeInternal({ ...@@ -505,6 +506,7 @@ async function createComponentTreeInternal({
// reason that I'm not aware of, so I'm leaving it as-is out of extreme // reason that I'm not aware of, so I'm leaving it as-is out of extreme
// caution, for now. // caution, for now.
<>{parallelRouteProps.children}</>, <>{parallelRouteProps.children}</>,
loadingData,
], ],
styles: layerAssets, styles: layerAssets,
} }
...@@ -534,6 +536,7 @@ async function createComponentTreeInternal({ ...@@ -534,6 +536,7 @@ async function createComponentTreeInternal({
reason='dynamic = "force-dynamic" was used' reason='dynamic = "force-dynamic" was used'
pathname={staticGenerationStore.urlPathname} pathname={staticGenerationStore.urlPathname}
/>, />,
loadingData,
], ],
styles: layerAssets, styles: layerAssets,
} }
...@@ -625,6 +628,7 @@ async function createComponentTreeInternal({ ...@@ -625,6 +628,7 @@ async function createComponentTreeInternal({
TODO-APP update router to use a Symbol for partial tree detection */} TODO-APP update router to use a Symbol for partial tree detection */}
{null} {null}
</>, </>,
loadingData,
], ],
styles: layerAssets, styles: layerAssets,
} }
......
...@@ -6,6 +6,7 @@ import type { NextFontManifest } from '../../build/webpack/plugins/next-font-man ...@@ -6,6 +6,7 @@ import type { NextFontManifest } from '../../build/webpack/plugins/next-font-man
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { AppPageModule } from '../future/route-modules/app-page/module' import type { AppPageModule } from '../future/route-modules/app-page/module'
import type { SwrDelta } from '../lib/revalidate' import type { SwrDelta } from '../lib/revalidate'
import type { LoadingModuleData } from '../../shared/lib/app-router-context.shared-runtime'
import s from 'next/dist/compiled/superstruct' import s from 'next/dist/compiled/superstruct'
...@@ -75,7 +76,8 @@ export type CacheNodeSeedData = [ ...@@ -75,7 +76,8 @@ export type CacheNodeSeedData = [
parallelRoutes: { parallelRoutes: {
[parallelRouterKey: string]: CacheNodeSeedData | null [parallelRouterKey: string]: CacheNodeSeedData | null
}, },
node: React.ReactNode | null node: React.ReactNode | null,
loading: LoadingModuleData
] ]
export type FlightDataPath = export type FlightDataPath =
......
...@@ -16,6 +16,10 @@ export type ChildSegmentMap = Map<string, CacheNode> ...@@ -16,6 +16,10 @@ export type ChildSegmentMap = Map<string, CacheNode>
*/ */
export type CacheNode = ReadyCacheNode | LazyCacheNode export type CacheNode = ReadyCacheNode | LazyCacheNode
export type LoadingModuleData =
| [React.JSX.Element, React.ReactNode, React.ReactNode]
| null
export type LazyCacheNode = { export type LazyCacheNode = {
/** /**
* Whether the lazy cache node data promise has been resolved. * Whether the lazy cache node data promise has been resolved.
...@@ -53,6 +57,9 @@ export type LazyCacheNode = { ...@@ -53,6 +57,9 @@ export type LazyCacheNode = {
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
head: React.ReactNode head: React.ReactNode
loading: LoadingModuleData
/** /**
* Child parallel routes. * Child parallel routes.
*/ */
...@@ -97,6 +104,9 @@ export type ReadyCacheNode = { ...@@ -97,6 +104,9 @@ export type ReadyCacheNode = {
lazyData: null lazyData: null
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
head: React.ReactNode head: React.ReactNode
loading: LoadingModuleData
parallelRoutes: Map<string, ChildSegmentMap> parallelRoutes: Map<string, ChildSegmentMap>
} }
...@@ -149,6 +159,7 @@ export const LayoutRouterContext = React.createContext<{ ...@@ -149,6 +159,7 @@ export const LayoutRouterContext = React.createContext<{
childNodes: CacheNode['parallelRoutes'] childNodes: CacheNode['parallelRoutes']
tree: FlightRouterState tree: FlightRouterState
url: string url: string
loading: LoadingModuleData
} | null>(null) } | null>(null)
export const GlobalLayoutRouterContext = React.createContext<{ export const GlobalLayoutRouterContext = React.createContext<{
......
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
"failed": [ "failed": [
"app dir client cache semantics prefetch={undefined} - default should re-use the full cache for only 30 seconds", "app dir client cache semantics prefetch={undefined} - default should re-use the full cache for only 30 seconds",
"app dir client cache semantics prefetch={undefined} - default should refetch below the fold after 30 seconds", "app dir client cache semantics prefetch={undefined} - default should refetch below the fold after 30 seconds",
"app dir client cache semantics prefetch={undefined} - default should renew the 30s cache once the data is revalidated" "app dir client cache semantics prefetch={undefined} - default should renew the 30s cache once the data is revalidated",
"app dir client cache semantics prefetch={undefined} - default should refetch the full page after 5 mins"
] ]
}, },
"test/e2e/app-dir/headers-static-bailout/headers-static-bailout.test.ts": { "test/e2e/app-dir/headers-static-bailout/headers-static-bailout.test.ts": {
......
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