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

Pass App Page HMR state through a dev render context (#99487)

## Summary

Continue decoupling App Router rendering from incoming request objects
by capturing HMR state at the handler boundary and passing it through
DevRenderContext.

The context contains the HMR refresh hash and shared server-component
HMR cache, and is constructed only under `next dev`. Production omits
this state, and prerendering does not consume it. Rendering no longer
reads the hash from request metadata. Existing upstream metadata
transport and cache invalidation behavior are preserved; status and
debugging callbacks are left for separate migrations.

## Verification

- `pnpm test-dev-turbo
test/development/app-dir/use-cache-dev/use-cache-dev.test.ts` (7 tests)
- `pnpm test-dev-webpack
test/development/app-dir/use-cache-dev/use-cache-dev.test.ts` (7 tests)
- `pnpm test-start-webpack test/e2e/app-dir/app/standalone.test.ts`
(isolated production build and Edge coverage)

<!-- NEXT_JS_LLM -->
parent 59715b7b
...@@ -5,8 +5,10 @@ import type { FallbackRouteParam } from '../static-paths/types' ...@@ -5,8 +5,10 @@ import type { FallbackRouteParam } from '../static-paths/types'
import { import {
AppPageRouteModule, AppPageRouteModule,
type AppPageRouteHandlerContext, type AppPageRouteHandlerContext,
type DevRenderContext,
type RouteMatch, type RouteMatch,
} from '../../server/route-modules/app-page/module.compiled' with { 'turbopack-transition': 'next-ssr' } } from '../../server/route-modules/app-page/module.compiled' with { 'turbopack-transition': 'next-ssr' }
import { createDevRenderContext } from '../../server/route-modules/app-page/dev-render-context' with { 'turbopack-transition': 'next-server-utility' }
import { RouteKind } from '../../server/route-kind' with { 'turbopack-transition': 'next-server-utility' } import { RouteKind } from '../../server/route-kind' with { 'turbopack-transition': 'next-server-utility' }
...@@ -873,6 +875,7 @@ export function createAppPageEntrypoint({ ...@@ -873,6 +875,7 @@ export function createAppPageEntrypoint({
renderOperation: AppPageRenderOperation renderOperation: AppPageRenderOperation
}): Promise<ResponseCacheEntry | PrerenderFailure> => { }): Promise<ResponseCacheEntry | PrerenderFailure> => {
const routeMatch: RouteMatch = { resolvedPathname } const routeMatch: RouteMatch = { resolvedPathname }
const dev: DevRenderContext | undefined = createDevRenderContext(req)
const context: AppPageRouteHandlerContext = { const context: AppPageRouteHandlerContext = {
query, query,
params, params,
...@@ -883,10 +886,7 @@ export function createAppPageEntrypoint({ ...@@ -883,10 +886,7 @@ export function createAppPageEntrypoint({
deploymentId, deploymentId,
clientAssetToken, clientAssetToken,
}, },
serverComponentsHmrCache: getRequestMeta( dev,
req,
'serverComponentsHmrCache'
),
fallbackRouteParams, fallbackRouteParams,
renderOpts: { renderOpts: {
App: () => null, App: () => null,
......
...@@ -22,6 +22,7 @@ import type { NextFetchEvent } from '../../server/web/spec-extension/fetch-event ...@@ -22,6 +22,7 @@ import type { NextFetchEvent } from '../../server/web/spec-extension/fetch-event
import type { import type {
AppPageRouteHandlerContext, AppPageRouteHandlerContext,
AppPageRouteModule, AppPageRouteModule,
DevRenderContext,
RouteMatch, RouteMatch,
} from '../../server/route-modules/app-page/module.compiled' } from '../../server/route-modules/app-page/module.compiled'
import type { AppPageRenderResultMetadata } from '../../server/render-result' import type { AppPageRenderResultMetadata } from '../../server/render-result'
...@@ -36,6 +37,7 @@ import { CloseController } from '../../server/web/web-on-close' ...@@ -36,6 +37,7 @@ import { CloseController } from '../../server/web/web-on-close'
import { parseMaxPostponedStateSize } from '../../shared/lib/size-limit' import { parseMaxPostponedStateSize } from '../../shared/lib/size-limit'
import { toNodeOutgoingHttpHeaders } from '../../server/web/utils' import { toNodeOutgoingHttpHeaders } from '../../server/web/utils'
import type { RequestMeta } from '../../server/request-meta' import type { RequestMeta } from '../../server/request-meta'
import { createDevRenderContext } from '../../server/route-modules/app-page/dev-render-context'
declare const incrementalCacheHandler: any declare const incrementalCacheHandler: any
// OPTIONAL_IMPORT:incrementalCacheHandler // OPTIONAL_IMPORT:incrementalCacheHandler
...@@ -112,11 +114,13 @@ async function requestHandler( ...@@ -112,11 +114,13 @@ async function requestHandler(
const closeController = new CloseController() const closeController = new CloseController()
const routeMatch: RouteMatch = { resolvedPathname } const routeMatch: RouteMatch = { resolvedPathname }
const dev: DevRenderContext | undefined = createDevRenderContext(baseReq)
const renderContext: AppPageRouteHandlerContext = { const renderContext: AppPageRouteHandlerContext = {
page: normalizedSrcPage, page: normalizedSrcPage,
routeMatch, routeMatch,
query, query,
params, params,
dev,
sharedContext: { sharedContext: {
buildId, buildId,
......
...@@ -88,7 +88,7 @@ export async function exportAppPage( ...@@ -88,7 +88,7 @@ export async function exportAppPage(
query, query,
fallbackRouteParams, fallbackRouteParams,
renderOpts, renderOpts,
undefined, undefined, // dev
sharedContext, sharedContext,
routeMatch routeMatch
) )
......
...@@ -35,6 +35,7 @@ import type { LoaderTree } from '../lib/app-dir-module' ...@@ -35,6 +35,7 @@ import type { LoaderTree } from '../lib/app-dir-module'
import { MIN_PRERENDERABLE_EXPIRE } from '../use-cache/constants' import { MIN_PRERENDERABLE_EXPIRE } from '../use-cache/constants'
import type { import type {
AppPageModule, AppPageModule,
DevRenderContext,
RouteMatch, RouteMatch,
} from '../route-modules/app-page/module' } from '../route-modules/app-page/module'
import type { BaseNextRequest, BaseNextResponse } from '../base-http' import type { BaseNextRequest, BaseNextResponse } from '../base-http'
...@@ -229,7 +230,6 @@ import { ...@@ -229,7 +230,6 @@ import {
type ParsedRelativeUrl, type ParsedRelativeUrl,
} from '../../shared/lib/router/utils/parse-relative-url' } from '../../shared/lib/router/utils/parse-relative-url'
import AppRouter from '../../client/components/app-router' import AppRouter from '../../client/components/app-router'
import type { ServerComponentsHmrCache } from '../response-cache'
import type { RequestErrorContext } from '../instrumentation/types' import type { RequestErrorContext } from '../instrumentation/types'
import { getIsPossibleServerAction } from '../lib/server-action-request-meta' import { getIsPossibleServerAction } from '../lib/server-action-request-meta'
import { createInitialRouterState } from '../../client/components/router-reducer/create-initial-router-state' import { createInitialRouterState } from '../../client/components/router-reducer/create-initial-router-state'
...@@ -3033,7 +3033,7 @@ async function prerenderAppPage({ ...@@ -3033,7 +3033,7 @@ async function prerenderAppPage({
async function renderAppPage( async function renderAppPage(
{ req, ctx, metadata, loaderTree }: PreparedAppPageRender, { req, ctx, metadata, loaderTree }: PreparedAppPageRender,
postponedState: PostponedState | null, postponedState: PostponedState | null,
serverComponentsHmrCache: ServerComponentsHmrCache | undefined dev: DevRenderContext | undefined
) { ) {
const { const {
res, res,
...@@ -3073,8 +3073,6 @@ async function renderAppPage( ...@@ -3073,8 +3073,6 @@ async function renderAppPage(
: stagedFallbackParams : stagedFallbackParams
? new Set(stagedFallbackParams.keys()) ? new Set(stagedFallbackParams.keys())
: null : null
const hmrRefreshHash = getRequestMeta(req, 'hmrRefreshHash')
const createRequestStore = createRequestStoreForRender.bind( const createRequestStore = createRequestStoreForRender.bind(
null, null,
req, req,
...@@ -3085,10 +3083,10 @@ async function renderAppPage( ...@@ -3085,10 +3083,10 @@ async function renderAppPage(
renderOpts.onUpdateCookies, renderOpts.onUpdateCookies,
renderOpts.previewProps, renderOpts.previewProps,
isHmrRefresh, isHmrRefresh,
serverComponentsHmrCache, dev?.serverComponentsHmrCache,
renderResumeDataCache, renderResumeDataCache,
stagedFallbackParamNames, stagedFallbackParamNames,
hmrRefreshHash dev?.hmrRefreshHash
) )
const requestStore = createRequestStore() const requestStore = createRequestStore()
...@@ -3325,7 +3323,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -3325,7 +3323,7 @@ async function renderToHTMLOrFlightImpl(
workStore: WorkStore, workStore: WorkStore,
parsedRequestHeaders: ParsedRequestHeaders, parsedRequestHeaders: ParsedRequestHeaders,
postponedState: PostponedState | null, postponedState: PostponedState | null,
serverComponentsHmrCache: ServerComponentsHmrCache | undefined, dev: DevRenderContext | undefined,
sharedContext: AppSharedContext, sharedContext: AppSharedContext,
interpolatedParams: Params, interpolatedParams: Params,
fallbackRouteParams: OpaqueFallbackRouteParams | null, fallbackRouteParams: OpaqueFallbackRouteParams | null,
...@@ -3361,7 +3359,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -3361,7 +3359,7 @@ async function renderToHTMLOrFlightImpl(
supportsPerSegmentPrefetching: renderOpts.cacheComponents, supportsPerSegmentPrefetching: renderOpts.cacheComponents,
} }
) )
return renderAppPage(prepared, postponedState, serverComponentsHmrCache) return renderAppPage(prepared, postponedState, dev)
} }
async function prerenderToHTMLOrFlightImpl( async function prerenderToHTMLOrFlightImpl(
...@@ -3419,7 +3417,7 @@ export type AppPageRender = ( ...@@ -3419,7 +3417,7 @@ export type AppPageRender = (
query: NextParsedUrlQuery, query: NextParsedUrlQuery,
fallbackRouteParams: OpaqueFallbackRouteParams | null, fallbackRouteParams: OpaqueFallbackRouteParams | null,
renderOpts: RenderOpts, renderOpts: RenderOpts,
serverComponentsHmrCache: ServerComponentsHmrCache | undefined, dev: DevRenderContext | undefined,
sharedContext: AppSharedContext, sharedContext: AppSharedContext,
routeMatch: RouteMatch routeMatch: RouteMatch
) => Promise<RenderResult<AppPageRenderResultMetadata>> ) => Promise<RenderResult<AppPageRenderResultMetadata>>
...@@ -3516,7 +3514,7 @@ export const renderToHTMLOrFlight: AppPageRender = ( ...@@ -3516,7 +3514,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
query, query,
fallbackRouteParams, fallbackRouteParams,
renderOpts, renderOpts,
serverComponentsHmrCache, dev,
sharedContext, sharedContext,
routeMatch routeMatch
) => { ) => {
...@@ -3547,7 +3545,7 @@ export const renderToHTMLOrFlight: AppPageRender = ( ...@@ -3547,7 +3545,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
workStore, workStore,
parsedRequestHeaders, parsedRequestHeaders,
postponedState, postponedState,
serverComponentsHmrCache, dev,
sharedContext, sharedContext,
interpolatedParams, interpolatedParams,
fallbackRouteParams, fallbackRouteParams,
...@@ -3562,7 +3560,7 @@ export const prerenderToHTMLOrFlight: AppPagePrerender = ( ...@@ -3562,7 +3560,7 @@ export const prerenderToHTMLOrFlight: AppPagePrerender = (
query, query,
fallbackRouteParams, fallbackRouteParams,
renderOpts, renderOpts,
_serverComponentsHmrCache, _dev,
sharedContext, sharedContext,
routeMatch routeMatch
) => { ) => {
......
...@@ -25,7 +25,11 @@ import type { ...@@ -25,7 +25,11 @@ import type {
import type { Params } from './request/params' import type { Params } from './request/params'
import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher' import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher'
import type { RouteMatch } from './route-matches/route-match' import type { RouteMatch } from './route-matches/route-match'
import type { RouteMatch as AppRenderRouteMatch } from './route-modules/app-page/module' import type {
DevRenderContext,
RouteMatch as AppRenderRouteMatch,
} from './route-modules/app-page/module'
import { createDevRenderContext } from './route-modules/app-page/dev-render-context'
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { ParsedUrl } from '../shared/lib/router/utils/parse-url' import type { ParsedUrl } from '../shared/lib/router/utils/parse-url'
...@@ -678,6 +682,12 @@ export default class NextNodeServer extends BaseServer< ...@@ -678,6 +682,12 @@ export default class NextNodeServer extends BaseServer<
!renderOpts.isPossibleServerAction !renderOpts.isPossibleServerAction
? lazyPrerenderAppPage ? lazyPrerenderAppPage
: lazyRenderAppPage : lazyRenderAppPage
const dev: DevRenderContext | undefined = createDevRenderContext(
req,
process.env.__NEXT_DEV_SERVER
? this.getServerComponentsHmrCache()
: undefined
)
const result = await renderAppPage( const result = await renderAppPage(
req, req,
...@@ -688,7 +698,7 @@ export default class NextNodeServer extends BaseServer< ...@@ -688,7 +698,7 @@ export default class NextNodeServer extends BaseServer<
// shells. As a result, we don't need to pass in the unknown params. // shells. As a result, we don't need to pass in the unknown params.
null, null,
renderOpts as LoadedRenderOpts<AppPageModule>, renderOpts as LoadedRenderOpts<AppPageModule>,
this.getServerComponentsHmrCache(), dev,
{ {
buildId: this.buildId, buildId: this.buildId,
deploymentId: this.deploymentId, deploymentId: this.deploymentId,
......
import { getRequestMeta, type NextIncomingMessage } from '../../request-meta'
import type { ServerComponentsHmrCache } from '../../response-cache'
import type { DevRenderContext } from './module'
/** Capture HMR inputs at the App Page handler boundary in dev. */
export function createDevRenderContext(
req: NextIncomingMessage,
hmrCacheFallback?: ServerComponentsHmrCache
): DevRenderContext | undefined {
if (!process.env.__NEXT_DEV_SERVER) {
return undefined
}
const { serverComponentsHmrCache, hmrRefreshHash } = getRequestMeta(req)
return {
serverComponentsHmrCache: serverComponentsHmrCache ?? hmrCacheFallback,
hmrRefreshHash,
}
}
...@@ -78,13 +78,19 @@ export type RouteMatch = { ...@@ -78,13 +78,19 @@ export type RouteMatch = {
readonly resolvedPathname: string readonly resolvedPathname: string
} }
/** Live dev-server state captured for a render; absent in production and build prerenders. */
export type DevRenderContext = {
readonly serverComponentsHmrCache: ServerComponentsHmrCache | undefined
readonly hmrRefreshHash: string | undefined
}
export interface AppPageRouteHandlerContext extends RouteModuleHandleContext { export interface AppPageRouteHandlerContext extends RouteModuleHandleContext {
page: string page: string
routeMatch: RouteMatch routeMatch: RouteMatch
query: NextParsedUrlQuery query: NextParsedUrlQuery
fallbackRouteParams: OpaqueFallbackRouteParams | null fallbackRouteParams: OpaqueFallbackRouteParams | null
renderOpts: RenderOpts renderOpts: RenderOpts
serverComponentsHmrCache?: ServerComponentsHmrCache dev: DevRenderContext | undefined
sharedContext: AppSharedContext sharedContext: AppSharedContext
} }
...@@ -179,7 +185,7 @@ export class AppPageRouteModule extends RouteModule< ...@@ -179,7 +185,7 @@ export class AppPageRouteModule extends RouteModule<
process.env.__NEXT_DEV_SERVER && req.method === 'HEAD' process.env.__NEXT_DEV_SERVER && req.method === 'HEAD'
? { ...context.renderOpts, setIsrStatus: undefined } ? { ...context.renderOpts, setIsrStatus: undefined }
: context.renderOpts, : context.renderOpts,
context.serverComponentsHmrCache, context.dev,
context.sharedContext, context.sharedContext,
context.routeMatch context.routeMatch
) )
...@@ -197,7 +203,7 @@ export class AppPageRouteModule extends RouteModule< ...@@ -197,7 +203,7 @@ export class AppPageRouteModule extends RouteModule<
context.query, context.query,
context.fallbackRouteParams, context.fallbackRouteParams,
context.renderOpts, context.renderOpts,
context.serverComponentsHmrCache, undefined,
context.sharedContext, context.sharedContext,
context.routeMatch context.routeMatch
) )
......
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