Unverified Commit b61c8e24 authored by Josh Story's avatar Josh Story Committed by GitHub

Finalize the App Router dev indicator on output completion (#99384)

## Summary

Continue removing request-event dependencies from App Router rendering.
The request body's `end` event is not a render-completion signal and may
already have fired before a custom server invokes Next.

Keep the dev indicator Pending until HTML or Flight output completes,
then classify from render-phase dynamic API usage and route
configuration, including usage discovered during Client Component SSR.
Completion handling stays local and dev-only, without shared lifecycle
callbacks or stream helper APIs.

Actions and prefetches do not reclassify the page. HEAD requests
preserve the last completed classification because their output is not
consumed. Pages Router and Cache Components behavior is unchanged.

## Verification

- Gated browser regressions fail before the change and pass afterward,
including a Client Component that calls `unstable_noStore()` after
suspending during SSR.
- All 21 dev-indicator tests pass in each combination of
Turbopack/Webpack and Cache Components on/off.
- Isolated `pnpm test-start-webpack
test/e2e/app-dir/app/standalone.test.ts` passes (2/2).

<!-- NEXT_JS_LLM -->
parent 1cfcc177
......@@ -2763,20 +2763,8 @@ async function prepareAppPageRender(
nextFontManifest,
assetPrefix = '',
enableTainting,
cacheComponents,
setIsrStatus,
} = renderOpts
if (process.env.__NEXT_DEV_SERVER && setIsrStatus && !cacheComponents) {
// Reset the ISR status at start of request.
const { pathname } = new URL(req.url || '/', 'http://n')
setIsrStatus(
pathname,
// Only pages using the Node runtime can use ISR, Edge is always dynamic.
process.env.NEXT_RUNTIME === 'edge' ? false : undefined
)
}
if (
// The type check here ensures that `req` is correctly typed, and the
// environment variable check provides dead code elimination.
......@@ -3070,9 +3058,11 @@ async function renderAppPage(
const { cachedNavigations } = renderOpts.experimental
const {
isHmrRefresh,
isPrefetchRequest,
isRSCRequest,
isRuntimePrefetchRequest,
isAppShellPrefetchRequest,
isRouteTreePrefetchRequest,
} = parsedRequestHeaders
const isPossibleActionRequest = ctx.isPossibleServerAction
......@@ -3112,22 +3102,26 @@ async function renderAppPage(
)
const requestStore = createRequestStore()
if (
const setDevIsrStatus =
process.env.__NEXT_DEV_SERVER &&
setIsrStatus &&
!cacheComponents &&
// Only pages using the Node runtime can use ISR, so we only need to
// update the status for those.
// The type check here ensures that `req` is correctly typed, and the
// environment variable check provides dead code elimination.
process.env.NEXT_RUNTIME !== 'edge' &&
isNodeNextRequest(req)
) {
req.originalRequest.on('end', () => {
const { pathname } = new URL(req.url || '/', 'http://n')
const isStatic = !requestStore.usedDynamic && !workStore.forceDynamic
setIsrStatus(pathname, isStatic)
})
!isPossibleActionRequest &&
!isPrefetchRequest &&
!isRuntimePrefetchRequest &&
!isAppShellPrefetchRequest &&
!isRouteTreePrefetchRequest
? setIsrStatus
: undefined
if (setDevIsrStatus) {
if (process.env.NEXT_RUNTIME === 'edge') {
// Edge routes cannot use ISR, so there is no dynamic transition to watch.
setDevIsrStatus(url.pathname, false)
} else {
// The indicator remains pending until the output has finished rendering.
setDevIsrStatus(url.pathname, undefined)
}
}
// MARK: RSC request
......@@ -3163,12 +3157,26 @@ async function renderAppPage(
)
} else {
// MARK: RSC dynamic
return generateDynamicFlightRenderResult(
const result = await generateDynamicFlightRenderResult(
req,
ctx,
requestStore,
undefined
requestStore
)
if (setDevIsrStatus && process.env.NEXT_RUNTIME !== 'edge') {
result.pipeThrough(
new TransformStream({
// Only a normally completed output can classify the route.
// Stream errors and cancellation leave the indicator pending.
flush() {
setDevIsrStatus(
url.pathname,
!requestStore.usedDynamic && !workStore.forceDynamic
)
},
})
)
}
return result
}
}
}
......@@ -3304,7 +3312,20 @@ async function renderAppPage(
}
// Create the new render result for the response.
return new RenderResult(stream, options)
const result = new RenderResult(stream, options)
if (setDevIsrStatus && process.env.NEXT_RUNTIME !== 'edge') {
result.pipeThrough(
new TransformStream({
flush() {
setDevIsrStatus(
url.pathname,
!requestStore.usedDynamic && !workStore.forceDynamic
)
},
})
)
}
return result
} catch (renderError) {
// Returning a stream may precede SSR readiness, which finishes success.
// Only failures finish here; a finally would seal successful renders early.
......
......@@ -322,7 +322,6 @@ async function createComponentTreeInternal(
workStore.dynamicShouldError = true
} else if (dynamic === 'force-dynamic') {
workStore.forceDynamic = true
if (isPrerendering) {
const err = new DynamicServerError(
`Page with \`dynamic = "force-dynamic"\` won't be rendered statically.`
......
......@@ -22,6 +22,7 @@
import type { WorkStore } from './work-async-storage.external'
import type {
RequestStore,
WorkUnitStore,
PrerenderStoreLegacy,
PrerenderStoreModern,
......@@ -221,9 +222,7 @@ export function markCurrentScopeAsDynamic(
throw err
case 'request':
if (process.env.NODE_ENV !== 'production') {
workUnitStore.usedDynamic = true
}
markRequestStoreAsDynamic(workUnitStore)
break
case 'build-time-generator':
break
......@@ -284,15 +283,19 @@ export function trackDynamicDataInDynamicRender(workUnitStore: WorkUnitStore) {
case 'build-time-generator':
break
case 'request':
if (process.env.NODE_ENV !== 'production') {
workUnitStore.usedDynamic = true
}
markRequestStoreAsDynamic(workUnitStore)
break
default:
workUnitStore satisfies never
}
}
function markRequestStoreAsDynamic(requestStore: RequestStore): void {
if (process.env.__NEXT_DEV_SERVER && requestStore.phase === 'render') {
requestStore.usedDynamic = true
}
}
function abortOnSynchronousDynamicDataAccess(
route: string,
expression: string,
......
......@@ -117,7 +117,7 @@ export interface RequestStore extends CommonWorkUnitStore {
validationSamples?: InstantValidationSamples
validationSampleTracking?: InstantValidationSampleTracking | null
// DEV-only
// DEV-only. Whether a dynamic API was used during this render.
usedDynamic?: boolean
}
......
......@@ -347,6 +347,7 @@ export function createRequestStore(inputs: RequestStoreInputs): RequestStore {
(globalThis as any).__serverComponentsHmrCache,
hmrRefreshHash,
stagedFallbackParams,
usedDynamic: false,
}
}
......
......@@ -174,7 +174,11 @@ export class AppPageRouteModule extends RouteModule<
context.page,
context.query,
context.fallbackRouteParams,
context.renderOpts,
// HEAD responses do not consume the render stream, so they cannot
// establish a new status for the dev indicator.
process.env.__NEXT_DEV_SERVER && req.method === 'HEAD'
? { ...context.renderOpts, setIsrStatus: undefined }
: context.renderOpts,
context.serverComponentsHmrCache,
context.sharedContext,
context.routeMatch
......
......@@ -1502,11 +1502,6 @@ function trackDynamic(
throw err
case 'request':
if (process.env.NODE_ENV !== 'production') {
// TODO: This is currently not really needed for route handlers, as it
// only controls the ISR status that's shown for pages.
workUnitStore.usedDynamic = true
}
break
case 'build-time-generator':
break
......
......@@ -205,14 +205,8 @@ function revalidate(
throw err
case 'request':
if (process.env.NODE_ENV !== 'production') {
// TODO: This is most likely incorrect. It would lead to the ISR
// status being flipped when revalidating a static page with a server
// action.
workUnitStore.usedDynamic = true
// TODO(restart-on-cache-miss): we should do a sync IO error here in dev
// to match prerender behavior
}
// TODO(restart-on-cache-miss): we should do a sync IO error here in dev
// to match prerender behavior
break
default:
workUnitStore satisfies never
......
import { connection } from 'next/server'
import { setTimeout } from 'timers/promises'
import { ActionButton } from '../static/action-button'
export default async function Page() {
await connection()
await setTimeout(100)
return <p>This is a dynamic app router page.</p>
return (
<>
<p>This is a dynamic app router page.</p>
<ActionButton />
</>
)
}
'use client'
import { use } from 'react'
import { unstable_noStore } from 'next/cache'
let gate: Promise<Response> | undefined
export function ClientDynamic() {
if (typeof window === 'undefined') {
if (!gate) {
const origin = process.env['DEV_INDICATOR_GATE_ORIGIN']
if (!origin) throw new Error('Missing gate origin')
gate = fetch(`${origin}/__gate/client`, { cache: 'force-cache' })
}
use(gate)
unstable_noStore()
}
return <p id="client-dynamic">Client SSR used noStore.</p>
}
import { Suspense } from 'react'
import { ClientDynamic } from './client'
export default function Page() {
return (
<Suspense fallback={<p id="client-gate-pending">Loading client gate...</p>}>
<ClientDynamic />
</Suspense>
)
}
export default async function Page() {
const origin = process.env['DEV_INDICATOR_GATE_ORIGIN']
if (!origin) throw new Error('Missing gate origin')
await fetch(`${origin}/__gate/static`, { cache: 'force-cache' })
return <p id="static-gate-released">The static gate was released.</p>
}
import { headers } from 'next/headers'
export default async function Page() {
const host = (await headers()).get('host')
await fetch(`http://${host}/__gate/dynamic`, {
cache: 'force-cache',
})
return <p id="gate-released">The gate was released.</p>
}
'use client'
import { useState, useTransition } from 'react'
import { readRequestHeaders } from './actions'
export function ActionButton() {
const [result, setResult] = useState('')
const [isPending, startTransition] = useTransition()
return (
<>
<button
onClick={() => {
startTransition(async () => {
setResult(await readRequestHeaders())
})
}}
>
Run action
</button>
<p id="action-result">{isPending ? 'Pending action' : result}</p>
</>
)
}
'use server'
import { headers } from 'next/headers'
export async function readRequestHeaders(): Promise<string> {
await headers()
return 'Action complete'
}
export default function Page() {
return <p>This is a static app router page.</p>
import { connection } from 'next/server' // eslint-disable-line @typescript-eslint/no-unused-vars
import { ActionButton } from './action-button'
export default async function Page() {
// await connection()
return (
<>
<p>This is a static app router page.</p>
<ActionButton />
</>
)
}
export default function NotFound() {
return <main id="not-found-page">This static page was not found.</main>
}
import { nextTestSetup } from 'e2e-utils'
import { waitForStaticIndicator } from 'next-test-utils'
import { gate, retry, waitForStaticIndicator } from 'next-test-utils'
const withCacheComponents = process.env.__NEXT_CACHE_COMPONENTS === 'true'
......@@ -91,6 +91,48 @@ describe('dev indicator - route type', () => {
})
describe('with App Router', () => {
it('classifies a completed static not-found page', async () => {
const path = '/app/no-such-page'
const response = await next.fetch(path)
expect(response.status).toBe(404)
const browser = await next.browser(path)
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
expect(await browser.elementByCss('#not-found-page').text()).toBe(
'This static page was not found.'
)
await waitForStaticIndicator(
browser,
hasCacheComponents ? undefined : 'Static'
)
})
it.each([
['static', 'Static'],
['dynamic', 'Dynamic'],
] as const)(
'preserves the %s route indicator after a Server Action',
async (route, routeType) => {
const browser = await next.browser(`/app/static-indicator/${route}`)
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
const expected = hasCacheComponents ? undefined : routeType
await waitForStaticIndicator(browser, expected)
await browser.elementByCss('main button').click()
await retry(async () => {
expect(await browser.elementByCss('#action-result').text()).toBe(
'Action complete'
)
})
await waitForStaticIndicator(browser, expected)
}
)
describe('when loading a dynamic page', () => {
if (withCacheComponents) {
describe('with Cache Components enabled', () => {
......@@ -105,6 +147,12 @@ describe('dev indicator - route type', () => {
await browser.elementByCss("[href='/pages']").click()
await retry(async () => {
expect(await browser.elementByCss('main > p').text()).toBe(
'hello world'
)
})
await waitForStaticIndicator(browser, 'Static')
})
})
......@@ -145,6 +193,12 @@ describe('dev indicator - route type', () => {
await browser.elementByCss("[href='/pages/gssp']").click()
await retry(async () => {
expect(await browser.elementByCss('main > p').text()).toBe(
'hello world'
)
})
await waitForStaticIndicator(browser, 'Dynamic')
})
})
......@@ -172,3 +226,230 @@ describe('dev indicator - route type', () => {
})
})
})
describe('dev indicator after a custom server consumes the request', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
startCommand: 'node server.js',
serverReadyPattern: /- Local:/,
skipDeployment: true,
})
if (skipped) return
async function expectPendingRoute(
browser: Awaited<ReturnType<typeof next.browser>>,
hasCacheComponents: boolean
) {
await browser.locateDevToolsIndicator().click()
await retry(async () => {
const pendingRoute = browser.locator(
'nextjs-portal .dev-tools-indicator-item[title="Loading..."]'
)
const routeType = browser.locator(
'nextjs-portal [data-nextjs-route-type]'
)
if (hasCacheComponents) {
expect(await pendingRoute.count()).toBe(0)
} else {
expect(await pendingRoute.count()).toBe(1)
expect(await pendingRoute.innerText()).toContain('Route')
}
expect(await routeType.count()).toBe(0)
})
}
it('keeps a dynamic document Pending until its output finishes', async () => {
let browser: Awaited<ReturnType<typeof next.browser>> | undefined
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
try {
browser = await next.browser('/app/static-indicator/gated', {
waitUntil: 'commit',
waitHydration: false,
})
await retry(async () => {
expect((await next.fetch('/__gate-arrived')).status).toBe(200)
expect(await browser.locator('main > p').textContent()).toBe(
'Loading...'
)
})
await expectPendingRoute(browser, hasCacheComponents)
} finally {
expect((await next.fetch('/__release-gate')).status).toBe(200)
}
await retry(async () => {
expect(await browser!.elementByCss('#gate-released').text()).toBe(
'The gate was released.'
)
})
if (!hasCacheComponents) {
await retry(async () => {
expect(
await browser!
.locator('nextjs-portal [data-nextjs-route-type]')
.innerText()
).toContain('Dynamic')
})
}
})
it('keeps a static document Pending until its output finishes', async () => {
let browser: Awaited<ReturnType<typeof next.browser>> | undefined
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
try {
browser = await next.browser('/app/static-indicator/gated-static', {
waitUntil: 'commit',
waitHydration: false,
})
await retry(async () => {
expect((await next.fetch('/__gate-arrived')).status).toBe(200)
expect(await browser.locator('main > p').textContent()).toBe(
'Loading...'
)
})
await expectPendingRoute(browser, hasCacheComponents)
} finally {
expect((await next.fetch('/__release-gate')).status).toBe(200)
}
expect(await browser!.elementByCss('#static-gate-released').text()).toBe(
'The static gate was released.'
)
if (!hasCacheComponents) {
await retry(async () => {
expect(
await browser!
.locator('nextjs-portal [data-nextjs-route-type]')
.innerText()
).toContain('Static')
})
}
})
it('waits for Client Component SSR usage before classifying', async () => {
let browser: Awaited<ReturnType<typeof next.browser>> | undefined
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
try {
browser = await next.browser('/app/static-indicator/gated-client', {
waitUntil: 'commit',
waitHydration: false,
})
await retry(async () => {
expect((await next.fetch('/__gate-arrived')).status).toBe(200)
expect(
await browser.locator('#client-gate-pending').textContent()
).toBe('Loading client gate...')
expect(await browser.locator('#client-dynamic').count()).toBe(0)
})
await expectPendingRoute(browser, hasCacheComponents)
} finally {
expect((await next.fetch('/__release-gate')).status).toBe(200)
}
expect(await browser!.elementByCss('#client-dynamic').text()).toBe(
'Client SSR used noStore.'
)
if (!hasCacheComponents) {
await retry(async () => {
expect(
await browser!
.locator('nextjs-portal [data-nextjs-route-type]')
.innerText()
).toContain('Dynamic')
})
}
})
it('classifies App Router document loads and navigation after request-body consumption', async () => {
const browser = await next.browser('/app/static-indicator/dynamic')
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
await waitForStaticIndicator(
browser,
hasCacheComponents ? undefined : 'Dynamic'
)
await browser.elementByCss("[href='/app/static-indicator/static']").click()
await waitForStaticIndicator(
browser,
hasCacheComponents ? undefined : 'Static'
)
})
it('reclassifies an App Router page after HMR and restores Static', async () => {
const browser = await next.browser('/app/static-indicator/static')
const hasCacheComponents = await gate(
(conditions) => conditions.cacheComponents
)
await waitForStaticIndicator(
browser,
hasCacheComponents ? undefined : 'Static'
)
const original = await next.readFile(
'app/app/static-indicator/static/page.tsx'
)
await next.patchFile(
'app/app/static-indicator/static/page.tsx',
original
.replace('// await connection()', 'await connection()')
.replace(
'This is a static app router page.',
'This page used connection().'
)
)
try {
await retry(async () => {
expect(await browser.elementByCss('main > p').text()).toBe(
'This page used connection().'
)
})
await waitForStaticIndicator(
browser,
hasCacheComponents ? undefined : 'Dynamic'
)
} finally {
await next.patchFile('app/app/static-indicator/static/page.tsx', original)
}
await retry(async () => {
expect(await browser.elementByCss('main > p').text()).toBe(
'This is a static app router page.'
)
})
await waitForStaticIndicator(
browser,
hasCacheComponents ? undefined : 'Static'
)
if (!hasCacheComponents) {
await next.patchFile(
'app/app/static-indicator/static/page.tsx',
`export const dynamic = 'force-dynamic'\n${original.replace('This is a static app router page.', 'This page is forced dynamic.')}`
)
try {
await retry(async () => {
expect(await browser.elementByCss('main > p').text()).toBe(
'This page is forced dynamic.'
)
})
await waitForStaticIndicator(browser, 'Dynamic')
} finally {
await next.patchFile(
'app/app/static-indicator/static/page.tsx',
original
)
}
}
})
})
const http = require('http')
const { once } = require('events')
const next = require('next')
const app = next({ dev: true, dir: __dirname })
const handle = app.getRequestHandler()
const port = Number(process.env.PORT || 3000)
let gateResponse
app.prepare().then(() => {
const server = http.createServer(async (req, res) => {
if (req.url?.startsWith('/__gate/')) {
gateResponse = res
return
}
if (req.url === '/__gate-arrived') {
res.writeHead(gateResponse ? 200 : 425).end()
return
}
if (req.url === '/__release-gate') {
gateResponse?.end('released')
gateResponse = undefined
res.end('released')
return
}
if (req.method === 'GET' && req.url?.startsWith('/app/static-indicator/')) {
const consumed = once(req, 'end')
req.resume()
await consumed
}
await handle(req, res)
})
server.listen(port, () => {
const address = server.address()
process.env.DEV_INDICATOR_GATE_ORIGIN = `http://localhost:${address.port}`
console.log(`- Local: http://localhost:${address.port}`)
})
})
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