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