Unverified Commit f7dd09fd authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[Instant] Validation on client navs (#89777)

This PR makes instant validation work for client navigations, albeit in
a pretty inefficent way. If we're navigating and have an `instant`
config somewhere in the tree, we won't skip shared layouts like a client
navigation usually would. Instead, we render all the shared segments too
(via `createFullTreeFlightDataForNavigation`, a very simplified
variation of `walkTreeWithFlightRouterState`) and use them to perform
instant validation on the current page. The client router doesn't mind
getting extra segments -- `startPPRNavigation` (and
`updateCacheNodeOnNavigation` specifically) ignore segments that are
already present in the old tree.

This approach has some downsides:
- It doesn't match prod behavior (parent layouts don't render there), so
it could lead to user confusion
- We're validating using different segments that the browser actually
has, so we could see an error in the validation that isn't present in
the browser

but we'll iterate on this later. We only do this when a page is using
`instant`, so for now the impact is narrowed down to early adopters.

### Test changes

For each test case we have, we now run it both on initial load and on
client nav.

This got somewhat noisy because i had to reorganize the test files a
bit. We use the index pages to test client navigations, but in order to
do that, we need the source and the target to be inside the same root
layout -- otherwise, they'd be an MPA nav.
parent 4385ed36
......@@ -92,7 +92,10 @@ import { createServerInsertedHTML } from './server-inserted-html'
import { getRequiredScripts } from './required-scripts'
import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'
import { makeGetServerInsertedHTML } from './make-get-server-inserted-html'
import { walkTreeWithFlightRouterState } from './walk-tree-with-flight-router-state'
import {
walkTreeWithFlightRouterState,
createFullTreeFlightDataForNavigation,
} from './walk-tree-with-flight-router-state'
import { createComponentTree, getRootParams } from './create-component-tree'
import { getAssetQueryString } from './get-asset-query-string'
import {
......@@ -532,6 +535,14 @@ async function generateDynamicRSCPayload(
if (!options?.skipPageRendering) {
const preloadCallbacks: PreloadCallbacks = []
// If we're performing instant validation, we need to render the whole tree,
// without skipping shared layouts.
const needsFullTree =
process.env.NODE_ENV === 'development' &&
ctx.renderOpts.cacheComponents &&
!options?.actionResult && // Only for navigations
(await anySegmentNeedsInstantValidation(loaderTree))
const { Viewport, Metadata, MetadataOutlet } = createMetadataComponents({
tree: loaderTree,
parsedQuery: query,
......@@ -542,38 +553,50 @@ async function generateDynamicRSCPayload(
serveStreamingMetadata,
})
const rscHead = createElement(
Fragment,
{
key: flightDataPathHeadKey,
},
createElement(NonIndex, {
createElement,
pagePath: ctx.pagePath,
statusCode: ctx.res.statusCode,
isPossibleServerAction: ctx.isPossibleServerAction,
}),
createElement(Viewport, {
key: getFlightViewportKey(requestId),
}),
createElement(Metadata, {
key: getFlightMetadataKey(requestId),
})
)
flightData = (
await walkTreeWithFlightRouterState({
ctx,
loaderTreeToFilter: loaderTree,
parentParams: {},
flightRouterState,
// For flight, render metadata inside leaf page
rscHead: createElement(
Fragment,
{
key: flightDataPathHeadKey,
},
createElement(NonIndex, {
createElement,
pagePath: ctx.pagePath,
statusCode: ctx.res.statusCode,
isPossibleServerAction: ctx.isPossibleServerAction,
}),
createElement(Viewport, {
key: getFlightViewportKey(requestId),
}),
createElement(Metadata, {
key: getFlightMetadataKey(requestId),
needsFullTree
? await createFullTreeFlightDataForNavigation({
ctx,
loaderTree,
rscHead,
injectedCSS: new Set(),
injectedJS: new Set(),
injectedFontPreloadTags: new Set(),
preloadCallbacks,
MetadataOutlet,
})
: await walkTreeWithFlightRouterState({
ctx,
loaderTreeToFilter: loaderTree,
parentParams: {},
flightRouterState,
rscHead,
injectedCSS: new Set(),
injectedJS: new Set(),
injectedFontPreloadTags: new Set(),
rootLayoutIncluded: false,
preloadCallbacks,
MetadataOutlet,
})
),
injectedCSS: new Set(),
injectedJS: new Set(),
injectedFontPreloadTags: new Set(),
rootLayoutIncluded: false,
preloadCallbacks,
MetadataOutlet,
})
).map((path) => path.slice(1)) // remove the '' (root) segment
}
......@@ -806,7 +829,12 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
renderOpts,
requestId,
workStore,
componentMod: { createElement },
componentMod: {
createElement,
routeModule: {
userland: { loaderTree },
},
},
url,
} = ctx
......@@ -834,11 +862,13 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
onFlightDataRenderError
)
// We only validate RSC requests if it is for HMR refreshes since we know we
// will render all the layouts necessary to perform the validation.
// We validate RSC requests for HMR refreshes and client navigations when
// instant configs exist, since we render all the layouts necessary to perform
// the validation in those cases.
const shouldValidate =
!isBypassingCachesInDev(renderOpts, initialRequestStore) &&
initialRequestStore.isHmrRefresh === true
(initialRequestStore.isHmrRefresh === true ||
(await anySegmentNeedsInstantValidation(loaderTree)))
const getPayload = async (requestStore: RequestStore) => {
const payload: RSCPayload &
......
......@@ -2,6 +2,10 @@ import { getLayoutOrPageModule } from '../../lib/app-dir-module'
import type { LoaderTree } from '../../lib/app-dir-module'
import { parseLoaderTree } from '../../../shared/lib/router/utils/parse-loader-tree'
import type { AppSegmentConfig } from '../../../build/segment-config/app/app-segment-config'
import {
workAsyncStorage,
type WorkStore,
} from '../work-async-storage.external'
export async function anySegmentHasRuntimePrefetchEnabled(
tree: LoaderTree
......@@ -70,53 +74,90 @@ type FoundSegmentWithConfig = {
config: NonNullable<AppSegmentConfig['unstable_instant']>
}
export async function anySegmentNeedsInstantValidation(
rootTree: LoaderTree
): Promise<boolean> {
const segments = await findSegmentsWithInstantConfig(rootTree)
// Check if there's any configs with `prefetch: 'static'` or `mode: 'instant'`.
// (If there's only `false`, there's no need to run validation).
// If any segment has `unstable_disableValidation`, we skip validation for the whole tree.
let needsValidation = false
for (const { config } of segments) {
if (typeof config === 'object') {
if (config.unstable_disableValidation) {
return false
/**
* Checks if any segments in the loader tree have `instant` configs that need validating.
* NOTE: Client navigations call this multiple times, so we cache it.
* */
export const anySegmentNeedsInstantValidation = cacheScopedToWorkStore(
async (rootTree: LoaderTree): Promise<boolean> => {
const segments = await findSegmentsWithInstantConfig(rootTree)
// Check if there's any configs with `prefetch: 'static'` or `mode: 'instant'`.
// (If there's only `false`, there's no need to run validation).
// If any segment has `unstable_disableValidation`, we skip validation for the whole tree.
let needsValidation = false
for (const { config } of segments) {
if (typeof config === 'object') {
if (config.unstable_disableValidation) {
return false
}
// do not short-circuit, some other segment might still have `unstable_disableValidation`
needsValidation = true
}
// do not short-circuit, some other segment might still have `unstable_disableValidation`
needsValidation = true
}
return needsValidation
}
return needsValidation
}
)
export const findSegmentsWithInstantConfig = cacheScopedToWorkStore(
async (rootTree: LoaderTree): Promise<FoundSegmentWithConfig[]> => {
const results: FoundSegmentWithConfig[] = []
async function visit(tree: LoaderTree, path: string[]): Promise<void> {
const { mod: layoutOrPageMod } = await getLayoutOrPageModule(tree)
// TODO(restart-on-cache-miss): Does this work correctly for client page/layout modules?
const instantConfig = layoutOrPageMod
? (layoutOrPageMod as AppSegmentConfig).unstable_instant
: undefined
if (instantConfig !== undefined) {
results.push({
path,
config: instantConfig,
})
}
export async function findSegmentsWithInstantConfig(
rootTree: LoaderTree
): Promise<FoundSegmentWithConfig[]> {
const results: FoundSegmentWithConfig[] = []
async function visit(tree: LoaderTree, path: string[]): Promise<void> {
const { mod: layoutOrPageMod } = await getLayoutOrPageModule(tree)
// TODO(restart-on-cache-miss): Does this work correctly for client page/layout modules?
const instantConfig = layoutOrPageMod
? (layoutOrPageMod as AppSegmentConfig).unstable_instant
: undefined
if (instantConfig !== undefined) {
results.push({
path,
config: instantConfig,
})
const { parallelRoutes } = parseLoaderTree(tree)
for (const parallelRouteKey in parallelRoutes) {
const childTree = parallelRoutes[parallelRouteKey]
await visit(childTree, [...path, parallelRouteKey])
}
}
const { parallelRoutes } = parseLoaderTree(tree)
for (const parallelRouteKey in parallelRoutes) {
const childTree = parallelRoutes[parallelRouteKey]
await visit(childTree, [...path, parallelRouteKey])
}
await visit(rootTree, [])
return results
}
)
/**
* A simple cache wrapper for 1-argument functions.
* The cache will live as long as the current WorkStore,
* i.e. it's scoped to a single request.
*/
function cacheScopedToWorkStore<TArg extends WeakKey, TRes>(
func: (arg: TArg) => TRes
): (arg: TArg) => TRes {
const resultsPerWorkStore = new WeakMap<WorkStore, WeakMap<TArg, TRes>>()
return (arg: TArg): TRes => {
const workStore = workAsyncStorage.getStore()
if (!workStore) {
// No caching.
return func(arg)
}
await visit(rootTree, [])
return results
let results = resultsPerWorkStore.get(workStore)
if (results && results.has(arg)) {
return results.get(arg)!
}
const result = func(arg)
if (!results) {
results = new WeakMap()
resultsPerWorkStore.set(workStore, results)
}
results.set(arg, result)
return result
}
}
......@@ -294,6 +294,70 @@ export async function walkTreeWithFlightRouterState({
return paths
}
/**
* A simplified version of `walkTreeWithFlightRouterState` that doesn't skip any layouts
* but returns a result of the same shape.
* Intended to be used for instant validation, where we need the complete tree.
*/
export async function createFullTreeFlightDataForNavigation({
loaderTree,
rscHead,
injectedCSS,
injectedJS,
injectedFontPreloadTags,
ctx,
preloadCallbacks,
MetadataOutlet,
}: {
loaderTree: LoaderTree
flightRouterState?: FlightRouterState
rscHead: HeadData
injectedCSS: Set<string>
injectedJS: Set<string>
injectedFontPreloadTags: Set<string>
ctx: AppRenderContext
preloadCallbacks: PreloadCallbacks
MetadataOutlet: React.ComponentType
}): Promise<[rootSegment: FlightDataPath]> {
const {
renderOpts: { experimental },
query,
getDynamicParamFromSegment,
} = ctx
const routerState = createFlightRouterStateFromLoaderTree(
loaderTree,
getDynamicParamFromSegment,
query
)
const rootSegment = routerState[0]
const seedData = await createComponentTree({
ctx,
loaderTree,
parentParams: {},
injectedCSS,
injectedJS,
injectedFontPreloadTags,
rootLayoutIncluded: false,
preloadCallbacks,
authInterrupts: experimental.authInterrupts,
MetadataOutlet,
})
return [
[
// TODO: app-render slices this Segment off.
// why is that valid, and why are we including it in the first place?
rootSegment,
routerState,
seedData,
rscHead,
false,
] satisfies FlightDataSegment,
]
}
/*
* This function is used to determine if an existing segment can be overridden
* by the incoming segment.
......
......@@ -3,18 +3,7 @@ import { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body style={{ fontFamily: 'monospace' }}>
<Header />
{children}
</body>
<body style={{ fontFamily: 'monospace' }}>{children}</body>
</html>
)
}
function Header() {
return (
<header>
<a href="/">Home</a>
</header>
)
}
import Link from 'next/link'
export default function IndexPage() {
return (
<main>
<h1>Instant Validation Test</h1>
<ul>
<li>
<Link href="/default">Default (plain root layout)</Link>
</li>
<li>
<Link href="/suspense-in-root">Suspense in Root</Link>
</li>
</ul>
</main>
)
}
import { ReactNode } from 'react'
import { connection } from 'next/server'
import { ReactNode, Suspense } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body style={{ fontFamily: 'monospace' }}>
<Header />
<hr />
{children}
</body>
</html>
......@@ -14,7 +16,17 @@ export default function RootLayout({ children }: { children: ReactNode }) {
function Header() {
return (
<header>
<a href="/">Home</a>
<a href="/">Home</a>{' '}
<Suspense fallback="...">
<div id="root-layout-timestamp">
<Now />
</div>
</Suspense>
</header>
)
}
async function Now() {
await connection()
return Date.now()
}
import { cacheLife } from 'next/cache'
import { DebugLinks } from '../shared'
export default async function Page() {
'use cache'
cacheLife('minutes')
return (
<main>
<h2>Static</h2>
<ul>
<li>
<DebugLinks href="/default/static/valid-blocked-children" />
</li>
<li>
<DebugLinks href="/default/static/valid-blocking-inside-static" />
</li>
</ul>
</main>
)
}
import { cacheLife } from 'next/cache'
import Link from 'next/link'
import { setTimeout } from 'timers/promises'
export function DebugLinks({ href }: { href: string }) {
return (
<span>
<span>{href}</span>{' '}
<Link data-link-type="soft" href={href}>
[SPA]
</Link>{' '}
<a data-link-type="hard" href={href}>
[MPA]
</a>
</span>
)
}
export async function uncachedIO() {
await setTimeout(0)
}
......
import { connection } from 'next/server'
import { ReactNode, Suspense } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
......@@ -9,6 +10,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
<html>
<body style={{ fontFamily: 'monospace' }}>
<Header />
<hr />
{children}
</body>
</html>
......@@ -19,7 +21,17 @@ export default function RootLayout({ children }: { children: ReactNode }) {
function Header() {
return (
<header>
<a href="/">Home</a>
<a href="/">Home</a>{' '}
<Suspense fallback="...">
<div id="root-layout-timestamp">
<Now />
</div>
</Suspense>
</header>
)
}
async function Now() {
await connection()
return Date.now()
}
import { DebugLinkMPA } from '../../components/debug-link'
import { cacheLife } from 'next/cache'
import { DebugLinks } from '../shared'
export default async function Page() {
'use cache'
......@@ -9,90 +9,84 @@ export default async function Page() {
<h2>Runtime</h2>
<ul>
<li>
<DebugLinkMPA href="/runtime/suspense-around-dynamic" />
<DebugLinks href="/suspense-in-root/runtime/suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/runtime/no-suspense-around-params/123" />
<DebugLinks href="/suspense-in-root/runtime/no-suspense-around-params/123" />
</li>
<li>
<DebugLinkMPA href="/runtime/missing-suspense-around-dynamic" />
<DebugLinks href="/suspense-in-root/runtime/missing-suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/runtime/missing-suspense-around-dynamic-layout" />
<DebugLinks href="/suspense-in-root/runtime/missing-suspense-around-dynamic-layout" />
</li>
<li>
<DebugLinkMPA href="/runtime/suspense-too-high" />
<DebugLinks href="/suspense-in-root/runtime/suspense-too-high" />
</li>
<li>
<DebugLinkMPA href="/runtime/invalid-blocking-inside-runtime" />
<DebugLinks href="/suspense-in-root/static/invalid-blocking-inside-static" />
</li>
<li>
<DebugLinkMPA href="/runtime/invalid-sync-io" />
<DebugLinks href="/suspense-in-root/runtime/invalid-blocking-inside-runtime" />
</li>
<li>
<DebugLinkMPA href="/runtime/valid-blocking-inside-runtime" />
<DebugLinks href="/suspense-in-root/runtime/invalid-sync-io" />
</li>
<li>
<DebugLinks href="/suspense-in-root/runtime/valid-blocking-inside-runtime" />
</li>
</ul>
<h2>Static</h2>
<ul>
<li>
<DebugLinkMPA href="/static/suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-around-params/123" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-around-dynamic-layout" />
<DebugLinks href="/suspense-in-root/static/suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/static/suspense-too-high" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/static/blocking-layout" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-around-params/123" />
</li>
<li>
<DebugLinkMPA href="/static/blocking-layout/missing-suspense-around-dynamic" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-around-dynamic-layout" />
</li>
<li>
<DebugLinkMPA href="/static/invalid-only-loading-around-dynamic" />
<DebugLinks href="/suspense-in-root/static/suspense-too-high" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-around-runtime" />
<DebugLinks href="/suspense-in-root/static/blocking-layout" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-in-parallel-route" />
<DebugLinks href="/suspense-in-root/static/blocking-layout/missing-suspense-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-in-parallel-route/foo" />
<DebugLinks href="/suspense-in-root/static/invalid-only-loading-around-dynamic" />
</li>
<li>
<DebugLinkMPA href="/static/missing-suspense-in-parallel-route/bar" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-around-runtime" />
</li>
<li>
<DebugLinkMPA href="/static/invalid-blocking-inside-static" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-in-parallel-route" />
</li>
<li>
<DebugLinkMPA href="/static/valid-blocked-children" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-in-parallel-route/foo" />
</li>
<li>
<DebugLinkMPA href="/static/valid-blocking-inside-static" />
<DebugLinks href="/suspense-in-root/static/missing-suspense-in-parallel-route/bar" />
</li>
</ul>
<h2>Disable Validation</h2>
<ul>
<li>
<DebugLinkMPA href="/disable-validation/in-layout" />
<DebugLinks href="/suspense-in-root/disable-validation/in-layout" />
</li>
<li>
<DebugLinkMPA href="/disable-validation/in-page" />
<DebugLinks href="/suspense-in-root/disable-validation/in-page" />
</li>
<li>
<DebugLinkMPA href="/disable-validation/in-page-with-outer" />
<DebugLinks href="/suspense-in-root/disable-validation/in-page-with-outer" />
</li>
</ul>
</main>
......
import Link from 'next/link'
export const unstable_instant = { prefetch: 'static' }
export default async function Page() {
return (
<main>
<p>This is a static page below a blocking layout</p>
<div>
<Link href="/suspense-in-root/static/blocking-layout/missing-suspense-around-dynamic">
/suspense-in-root/static/blocking-layout/missing-suspense-around-dynamic
</Link>
</div>
</main>
)
}
......@@ -6,7 +6,9 @@ export default async function Page() {
return (
<main>
<p>This is a static page</p>
<Link href="./missing-suspense-in-parallel-route/foo">./foo</Link>
<Link href="/suspense-in-root/static/missing-suspense-in-parallel-route/foo">
./foo
</Link>
</main>
)
}
'use client'
import Link from 'next/link'
import { ComponentProps } from 'react'
export function DebugLink({
href,
...props
}: Omit<ComponentProps<typeof Link>, 'children' | 'href'> & { href: string }) {
return (
<Link href={href} {...props}>
{href}
</Link>
)
}
export function DebugLinkMPA({
href,
...props
......
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