Unverified Commit 39cd2f6e authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Allow CacheNode.loading to be a promise (#72872)

In the Segment Cache implementation of prefetching, during a navigation,
it's possible that the prefetch for a segment may already be
in-progress, but we haven't received its data yet. In that case, since
the prefetch is very likely to load faster than the dynamic request, we
might as well use the result, if we can.

This updates type CacheNode.loading to accept a promise for the loading
module data, and updates the LayoutRouter to conditionally unwrap it if
needed. We already use this same strategy in other places, like for
streaming in the main segment data.
parent f803a950
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import type { import type {
ChildSegmentMap, ChildSegmentMap,
LazyCacheNode, LazyCacheNode,
LoadingModuleData,
} from '../../shared/lib/app-router-context.shared-runtime' } from '../../shared/lib/app-router-context.shared-runtime'
import type { import type {
FlightRouterState, FlightRouterState,
...@@ -450,28 +451,43 @@ function InnerLayoutRouter({ ...@@ -450,28 +451,43 @@ function InnerLayoutRouter({
* If no loading property is provided it renders the children without a suspense boundary. * If no loading property is provided it renders the children without a suspense boundary.
*/ */
function LoadingBoundary({ function LoadingBoundary({
children,
hasLoading,
loading, loading,
loadingStyles, children,
loadingScripts,
}: { }: {
loading: LoadingModuleData | Promise<LoadingModuleData>
children: React.ReactNode children: React.ReactNode
hasLoading: boolean
loading?: React.ReactNode
loadingStyles?: React.ReactNode
loadingScripts?: React.ReactNode
}): JSX.Element { }): JSX.Element {
// We have an explicit prop for checking if `loading` is provided, to disambiguate between a loading // If loading is a promise, unwrap it. This happens in cases where we haven't
// component that returns `null` / `undefined`, vs not having a loading component at all. // yet received the loading data from the server — which includes whether or
if (hasLoading) { // not this layout has a loading component at all.
//
// It's OK to suspend here instead of inside the fallback because this
// promise will resolve simultaneously with the data for the segment itself.
// So it will never suspend for longer than it would have if we didn't use
// a Suspense fallback at all.
let loadingModuleData
if (
typeof loading === 'object' &&
loading !== null &&
typeof (loading as any).then === 'function'
) {
const promiseForLoading = loading as Promise<LoadingModuleData>
loadingModuleData = use(promiseForLoading)
} else {
loadingModuleData = loading as LoadingModuleData
}
if (loadingModuleData) {
const loadingRsc = loadingModuleData[0]
const loadingStyles = loadingModuleData[1]
const loadingScripts = loadingModuleData[2]
return ( return (
<Suspense <Suspense
fallback={ fallback={
<> <>
{loadingStyles} {loadingStyles}
{loadingScripts} {loadingScripts}
{loading} {loadingRsc}
</> </>
} }
> >
...@@ -562,12 +578,7 @@ export default function OuterLayoutRouter({ ...@@ -562,12 +578,7 @@ export default function OuterLayoutRouter({
errorStyles={errorStyles} errorStyles={errorStyles}
errorScripts={errorScripts} errorScripts={errorScripts}
> >
<LoadingBoundary <LoadingBoundary loading={loading}>
hasLoading={Boolean(loading)}
loading={loading?.[0]}
loadingStyles={loading?.[1]}
loadingScripts={loading?.[2]}
>
<HTTPAccessFallbackBoundary notFound={notFound}> <HTTPAccessFallbackBoundary notFound={notFound}>
<RedirectBoundary> <RedirectBoundary>
<InnerLayoutRouter <InnerLayoutRouter
......
...@@ -53,7 +53,7 @@ export type LazyCacheNode = { ...@@ -53,7 +53,7 @@ export type LazyCacheNode = {
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
head: React.ReactNode head: React.ReactNode
loading: LoadingModuleData loading: LoadingModuleData | Promise<LoadingModuleData>
/** /**
* Child parallel routes. * Child parallel routes.
...@@ -95,7 +95,7 @@ export type ReadyCacheNode = { ...@@ -95,7 +95,7 @@ export type ReadyCacheNode = {
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
head: React.ReactNode head: React.ReactNode
loading: LoadingModuleData loading: LoadingModuleData | Promise<LoadingModuleData>
parallelRoutes: Map<string, ChildSegmentMap> parallelRoutes: Map<string, ChildSegmentMap>
} }
...@@ -149,7 +149,7 @@ export const LayoutRouterContext = React.createContext<{ ...@@ -149,7 +149,7 @@ export const LayoutRouterContext = React.createContext<{
childNodes: CacheNode['parallelRoutes'] childNodes: CacheNode['parallelRoutes']
tree: FlightRouterState tree: FlightRouterState
url: string url: string
loading: LoadingModuleData loading: LoadingModuleData | Promise<LoadingModuleData>
} | null>(null) } | null>(null)
export const GlobalLayoutRouterContext = React.createContext<{ export const GlobalLayoutRouterContext = React.createContext<{
......
...@@ -443,7 +443,7 @@ describe('Error overlay for hydration errors in App router', () => { ...@@ -443,7 +443,7 @@ describe('Error overlay for hydration errors in App router', () => {
<ScrollAndFocusHandler segmentPath={[...]}> <ScrollAndFocusHandler segmentPath={[...]}>
<InnerScrollAndFocusHandler segmentPath={[...]} focusAndScrollRef={{apply:false, ...}}> <InnerScrollAndFocusHandler segmentPath={[...]} focusAndScrollRef={{apply:false, ...}}>
<ErrorBoundary errorComponent={undefined} errorStyles={undefined} errorScripts={undefined}> <ErrorBoundary errorComponent={undefined} errorStyles={undefined} errorScripts={undefined}>
<LoadingBoundary hasLoading={false} loading={undefined} loadingStyles={undefined} loadingScripts={undefined}> <LoadingBoundary loading={null}>
<HTTPAccessFallbackBoundary notFound={[...]}> <HTTPAccessFallbackBoundary notFound={[...]}>
<HTTPAccessFallbackErrorBoundary pathname="/" notFound={[...]} missingSlots={Set}> <HTTPAccessFallbackErrorBoundary pathname="/" notFound={[...]} missingSlots={Set}>
<RedirectBoundary> <RedirectBoundary>
......
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