Unverified Commit 81d0e851 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix instant validation blocking navigations (#95939)

### What?

- Run development instant validation only after the navigation response
has finished.
- Let a newer request supersede stale validation work and yield between
validation render attempts.
- Forward the cancellation signal into React validation prerenders so
asynchronous validation work can stop promptly.
- Add focused scheduler/response tests and assert that development
validation starts after the response finishes.

### Why?

Instant validation was detached from the response promise, but its
additional React renders still ran on the same Node.js event loop. A
subsequent navigation could therefore wait hundreds of milliseconds for
stale diagnostic work from the previous request.

Instant validation still runs when the server is idle. When another
navigation arrives, foreground request work now takes priority and the
obsolete validation is discarded.

### How?

Each Cache Components development request receives a validation
generation signal. Starting a newer request aborts the previous
generation. Validation waits for the Node response `finish` event,
yields through the event-loop poll phase between attempts, and combines
the generation signal with React's existing prerender abort signals.

Tests:

- `pnpm --filter=next build`
- `pnpm --filter=next types`
- `pnpm exec jest --runTestsByPath
packages/next/src/server/app-render/dev-validation-scheduler.test.ts
packages/next/src/server/app-render/wait-for-response.test.ts`
- `NEXT_SKIP_ISOLATE=1 NEXT_TEST_PREFER_OFFLINE=1 pnpm test-dev-turbo
test/e2e/app-dir/instant-validation/suspense-boundaries.test.ts -t
"valid - static prefetch - suspense around runtime and dynamic"`

<!-- NEXT_JS_LLM_PR -->
parent 390eff3b
...@@ -1420,5 +1420,6 @@ ...@@ -1420,5 +1420,6 @@
"1419": "TypeScript CLI interrupted by %s", "1419": "TypeScript CLI interrupted by %s",
"1420": "shouldTrackSyncInterrupt allowed a sync IO interrupt in an unexpected stage: %s", "1420": "shouldTrackSyncInterrupt allowed a sync IO interrupt in an unexpected stage: %s",
"1421": "The lazy module is still loading. It must be awaited with `waitUntilLoaded()` before it can be accessed.", "1421": "The lazy module is still loading. It must be awaited with `waitUntilLoaded()` before it can be accessed.",
"1422": "MIN_PREFETCHABLE_STALE must not exceed MIN_SHELL_STALE." "1422": "MIN_PREFETCHABLE_STALE must not exceed MIN_SHELL_STALE.",
"1423": "DevValidationScheduler requires at least one active validation"
} }
import {
DevValidationScheduler,
yieldToForegroundRequest,
} from './dev-validation-scheduler'
describe('dev validation scheduler', () => {
it('supersedes validation from an older request in the same document', () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
expect(first.signal.aborted).toBe(false)
const second = scheduler.begin('document-a')
expect(first.signal.aborted).toBe(true)
expect(second.signal.aborted).toBe(false)
expect(scheduler.size).toBe(1)
})
it('does not supersede validation from another document', () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
scheduler.begin('document-b')
expect(first.signal.aborted).toBe(false)
expect(scheduler.size).toBe(2)
})
it.each(['success', 'failure'])(
'removes a generation after %s',
(outcome) => {
const scheduler = new DevValidationScheduler(100)
const generation = scheduler.begin('document-a')
try {
if (outcome === 'failure') {
throw new Error('validation failed')
}
} catch {
// Validation failures use the same terminal cleanup callback.
} finally {
generation.finish()
}
expect(generation.signal.aborted).toBe(false)
expect(scheduler.size).toBe(0)
}
)
it('does not let stale cleanup remove a replacement generation', () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
const second = scheduler.begin('document-a')
first.finish()
expect(scheduler.size).toBe(1)
expect(second.signal.aborted).toBe(false)
second.finish()
expect(scheduler.size).toBe(0)
})
it('bounds active validations and aborts the oldest generation', () => {
const scheduler = new DevValidationScheduler(2)
const first = scheduler.begin('document-a')
const second = scheduler.begin('document-b')
const third = scheduler.begin('document-c')
expect(first.signal.aborted).toBe(true)
expect(second.signal.aborted).toBe(false)
expect(third.signal.aborted).toBe(false)
expect(scheduler.size).toBe(2)
first.finish()
second.finish()
third.finish()
expect(scheduler.size).toBe(0)
})
it('remains bounded after more than 100 unique documents', () => {
const scheduler = new DevValidationScheduler(100)
const generations = Array.from({ length: 101 }, (_, index) =>
scheduler.begin(`document-${index}`)
)
expect(generations[0].signal.aborted).toBe(true)
expect(generations[1].signal.aborted).toBe(false)
expect(scheduler.size).toBe(100)
})
it('observes supersession while yielding to incoming requests', async () => {
const scheduler = new DevValidationScheduler(100)
const first = scheduler.begin('document-a')
const canContinue = yieldToForegroundRequest(first.signal)
scheduler.begin('document-a')
await expect(canContinue).resolves.toBe(false)
})
})
import { unpatchedSetImmediate } from '../node-environment-extensions/fast-set-immediate.external'
import { InvariantError } from '../../shared/lib/invariant-error'
const MAX_ACTIVE_DEV_VALIDATIONS = 100
export interface DevValidationGeneration {
readonly signal: AbortSignal
finish(): void
}
export class DevValidationScheduler {
private readonly currentValidationByDocument = new Map<
string,
AbortController
>()
constructor(private readonly maxActiveValidations: number) {
if (maxActiveValidations < 1) {
throw new InvariantError(
'DevValidationScheduler requires at least one active validation'
)
}
}
get size(): number {
return this.currentValidationByDocument.size
}
begin(htmlRequestId: string): DevValidationGeneration {
const previousController =
this.currentValidationByDocument.get(htmlRequestId)
if (previousController !== undefined) {
this.currentValidationByDocument.delete(htmlRequestId)
previousController.abort()
}
if (this.currentValidationByDocument.size >= this.maxActiveValidations) {
const oldestEntry = this.currentValidationByDocument
.entries()
.next().value
if (oldestEntry !== undefined) {
const [oldestHtmlRequestId, oldestController] = oldestEntry
this.currentValidationByDocument.delete(oldestHtmlRequestId)
oldestController.abort()
}
}
const controller = new AbortController()
this.currentValidationByDocument.set(htmlRequestId, controller)
return {
signal: controller.signal,
finish: () => {
// A superseded generation may settle after its replacement. Only the
// current generation is allowed to remove this document's entry.
if (
this.currentValidationByDocument.get(htmlRequestId) === controller
) {
this.currentValidationByDocument.delete(htmlRequestId)
}
},
}
}
}
// Only active validation work is retained. The hard cap also bounds the
// registry if user code suspends indefinitely and the work never settles.
const devValidationScheduler = new DevValidationScheduler(
MAX_ACTIVE_DEV_VALIDATIONS
)
export function beginDevValidation(
htmlRequestId: string
): DevValidationGeneration {
return devValidationScheduler.begin(htmlRequestId)
}
/**
* Give incoming requests a chance to enter app rendering and supersede the
* current validation before another expensive render attempt starts.
*
* The regular global `setImmediate` is patched by staged rendering and can run
* inside the current timer task. The original immediate is required here so we
* actually pass through the event-loop poll phase where HTTP requests arrive.
*/
export async function yieldToForegroundRequest(
validationSignal: AbortSignal | undefined
): Promise<boolean> {
if (validationSignal?.aborted) {
return false
}
await new Promise<void>((resolve) => unpatchedSetImmediate(resolve))
return validationSignal?.aborted !== true
}
import { MockedResponse } from '../lib/mock-request'
import { waitForResponseToFinish } from './wait-for-response'
describe('waitForResponseToFinish', () => {
it('waits for the response to finish', async () => {
const response = new MockedResponse()
const onSettled = jest.fn()
const completion = waitForResponseToFinish(response)
void completion.then(onSettled)
await Promise.resolve()
expect(onSettled).not.toHaveBeenCalled()
response.end()
await expect(completion).resolves.toBe(true)
})
it('resolves immediately when the response has already finished', async () => {
const response = new MockedResponse()
const finished = new Promise<void>((resolve) => {
response.once('finish', resolve)
})
response.end()
await finished
await expect(waitForResponseToFinish(response)).resolves.toBe(true)
})
it('returns false when the connection closes before the response finishes', async () => {
const response = new MockedResponse()
const completion = waitForResponseToFinish(response)
response.destroy()
await expect(completion).resolves.toBe(false)
})
})
import type { ServerResponse } from 'node:http'
/**
* Waits for a Node response to finish normally. Returns false when the
* connection closes before the response has finished.
*/
export function waitForResponseToFinish(
response: ServerResponse
): Promise<boolean> {
if (response.writableFinished) {
return Promise.resolve(true)
}
if (response.destroyed) {
return Promise.resolve(false)
}
return new Promise((resolve) => {
function cleanup() {
response.off('finish', onFinish)
response.off('close', onClose)
}
function onFinish() {
cleanup()
resolve(true)
}
function onClose() {
cleanup()
resolve(response.writableFinished)
}
response.on('finish', onFinish)
response.on('close', onClose)
})
}
'use client'
import Link from 'next/link'
import { useState, type ReactNode } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: ReactNode
prefetch?: boolean
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
import { LinkAccordion } from './link-accordion'
export function RoutePage({ slug }: { slug: 'a' | 'b' | 'c' }) {
return (
<main>
<p id="route">route-{slug}</p>
{slug !== 'a' && (
<LinkAccordion href="/routes/a" prefetch={false}>
Navigate to A
</LinkAccordion>
)}
{slug !== 'b' && (
<LinkAccordion href="/routes/b" prefetch={false}>
Navigate to B
</LinkAccordion>
)}
{slug !== 'c' && (
<LinkAccordion href="/routes/c" prefetch={false}>
Navigate to C
</LinkAccordion>
)}
</main>
)
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from './components/link-accordion'
export default function Page() {
return (
<main>
<h1>Document B hub</h1>
<LinkAccordion href="/routes/a" prefetch={false}>
Navigate to A
</LinkAccordion>
<LinkAccordion href="/routes/b" prefetch={false}>
Navigate to B
</LinkAccordion>
</main>
)
}
import { RoutePage } from '../../components/route-page'
export const instant = true
export default function Page() {
return <RoutePage slug="a" />
}
import { RoutePage } from '../../components/route-page'
export const instant = true
export default function Page() {
return <RoutePage slug="b" />
}
import { RoutePage } from '../../components/route-page'
export const instant = true
export default function Page() {
return <RoutePage slug="c" />
}
import { nextTestSetup } from 'e2e-utils'
import {
parseValidationMessages,
waitForValidationEnd,
waitForValidationStart,
type ValidationEvent,
} from 'e2e-utils/instant-validation'
import { retry } from 'next-test-utils'
import { createRouterAct } from 'router-act'
import type * as Playwright from 'playwright'
describe('instant-validation-scheduling', () => {
const { next } = nextTestSetup({
files: __dirname,
env: {
NEXT_TEST_LOG_VALIDATION: '1',
// Keep validation active while the next document request reaches the
// scheduler; cancellation releases this delay immediately.
NEXT_TEST_DEV_VALIDATION_DELAY_MS: '2000',
},
})
async function waitForValidationTerminal(
requestId: string,
getOutput: () => string
): Promise<ValidationEvent> {
return retry(async () => {
const terminal = parseValidationMessages(getOutput()).find(
(event) =>
event.requestId === requestId &&
(event.type === 'validation_end' ||
event.type === 'validation_aborted')
)
expect(terminal).toBeDefined()
return terminal!
})
}
async function navigate(
browser: Awaited<ReturnType<typeof next.browser>>,
act: ReturnType<typeof createRouterAct>,
href: string,
destinationText: string
) {
await act(async () => {
await browser.elementByCss(`input[data-link-accordion="${href}"]`).click()
await browser.elementByCss(`a[href="${href}"]`).click()
})
await retry(async () => {
expect(await browser.elementById('route').text()).toBe(destinationText)
})
}
it('aborts superseded validations from successive navigations in one document', async () => {
let page!: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(browserPage: Playwright.Page) {
page = browserPage
},
})
const act = createRouterAct(page)
const cliStart = next.cliOutput.length
const getOutput = () => next.cliOutput.slice(cliStart)
await navigate(browser, act, '/routes/a', 'route-a')
const validationA = await waitForValidationStart(
await browser.url(),
getOutput
)
await navigate(browser, act, '/routes/b', 'route-b')
await expect(
waitForValidationTerminal(validationA.requestId, getOutput)
).resolves.toMatchObject({ type: 'validation_aborted' })
const validationB = await waitForValidationStart(
await browser.url(),
getOutput
)
await navigate(browser, act, '/routes/c', 'route-c')
await expect(
waitForValidationTerminal(validationB.requestId, getOutput)
).resolves.toMatchObject({ type: 'validation_aborted' })
const validationC = await waitForValidationStart(
await browser.url(),
getOutput
)
await expect(
waitForValidationEnd(validationC, getOutput)
).resolves.toMatchObject({ type: 'validation_end' })
})
it('does not supersede validation from another browser document', async () => {
let pageB!: Playwright.Page
const browserB = await next.browser('/', {
beforePageLoad(browserPage: Playwright.Page) {
pageB = browserPage
},
})
const actB = createRouterAct(pageB)
const cliStart = next.cliOutput.length
const getOutput = () => next.cliOutput.slice(cliStart)
const pageA = await pageB.context().newPage()
await pageA.goto(`${next.url}/routes/a`)
const validationA = await waitForValidationStart(pageA.url(), getOutput)
await navigate(browserB, actB, '/routes/b', 'route-b')
const validationB = await waitForValidationStart(
await browserB.url(),
getOutput
)
await expect(
waitForValidationEnd(validationA, getOutput)
).resolves.toMatchObject({ type: 'validation_end' })
await expect(
waitForValidationEnd(validationB, getOutput)
).resolves.toMatchObject({ type: 'validation_end' })
await pageA.close()
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
...@@ -98,7 +98,10 @@ export function runInstantValidationTests( ...@@ -98,7 +98,10 @@ export function runInstantValidationTests(
browser: Playwright, browser: Playwright,
url: string url: string
): Promise<void> { ): Promise<void> {
await waitForValidation(url, getCliOutputSinceMark) const { start } = await waitForValidation(url, getCliOutputSinceMark)
if (start.responseFinished !== undefined) {
expect(start.responseFinished).toBe(true)
}
await waitForNoErrorToast(browser, NO_VALIDATION_ERRORS_WAIT) await waitForNoErrorToast(browser, NO_VALIDATION_ERRORS_WAIT)
} }
......
...@@ -11,14 +11,17 @@ type ValidationStartEvent = { ...@@ -11,14 +11,17 @@ type ValidationStartEvent = {
type: 'validation_start' type: 'validation_start'
requestId: string requestId: string
url: string url: string
/** Only reported for development validation. */
responseFinished?: boolean
} }
type ValidationEndEvent = { type ValidationEndEvent = {
type: 'validation_end' type: 'validation_end'
requestId: string requestId: string
url: string url: string
} }
// Emitted instead of a start/end pair when a request is aborted before its // Emitted when detached validation is aborted. It can appear without a start
// detached validation runs (e.g. Server Components HMR cancellation). // when cancellation happens before validation runs, or after a start instead
// of an end when in-flight validation is cancelled.
type ValidationAbortedEvent = { type ValidationAbortedEvent = {
type: 'validation_aborted' type: 'validation_aborted'
requestId: string requestId: string
......
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