Unverified Commit ce5206fa authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[dynamicIO] Fix dev warmup (#77829)

When `dynamicIO` is enabled, we're triggering a warmup request in dev mode. This ensures that replayed logs are associated with the correct environment (`Prerender` vs. `Server`), by seeding the caches before the actual render.

This PR fixes two issues with the dev warmup:
- Ensures that cache keys are identical between the warmup, the subsequent dynamic render, and the dynamic validation, by providing the HMR refresh hash (part of the cache key) for all dev render phases.
- Ensures that stale cache entries are discarded during the warmup, by providing the implicit tags also during the warmup (and the dynamic validation).
parent f1312ef9
...@@ -12,6 +12,7 @@ export const NEXT_ROUTER_PREFETCH_HEADER = 'Next-Router-Prefetch' as const ...@@ -12,6 +12,7 @@ export const NEXT_ROUTER_PREFETCH_HEADER = 'Next-Router-Prefetch' as const
export const NEXT_ROUTER_SEGMENT_PREFETCH_HEADER = export const NEXT_ROUTER_SEGMENT_PREFETCH_HEADER =
'Next-Router-Segment-Prefetch' as const 'Next-Router-Segment-Prefetch' as const
export const NEXT_HMR_REFRESH_HEADER = 'Next-HMR-Refresh' as const export const NEXT_HMR_REFRESH_HEADER = 'Next-HMR-Refresh' as const
export const NEXT_HMR_REFRESH_HASH_COOKIE = '__next_hmr_refresh_hash__' as const
export const NEXT_URL = 'Next-Url' as const export const NEXT_URL = 'Next-Url' as const
export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const export const RSC_CONTENT_TYPE_HEADER = 'text/x-component' as const
......
...@@ -46,6 +46,7 @@ import type { GlobalErrorComponent } from '../../error-boundary' ...@@ -46,6 +46,7 @@ import type { GlobalErrorComponent } from '../../error-boundary'
import type { DevIndicatorServerState } from '../../../../server/dev/dev-indicator-server-state' import type { DevIndicatorServerState } from '../../../../server/dev/dev-indicator-server-state'
import reportHmrLatency from '../utils/report-hmr-latency' import reportHmrLatency from '../utils/report-hmr-latency'
import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common' import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common'
import { NEXT_HMR_REFRESH_HASH_COOKIE } from '../../app-router-headers'
export interface Dispatcher { export interface Dispatcher {
onBuildOk(): void onBuildOk(): void
...@@ -412,7 +413,7 @@ function processMessage( ...@@ -412,7 +413,7 @@ function processMessage(
// Store the latest hash in a session cookie so that it's sent back to the // Store the latest hash in a session cookie so that it's sent back to the
// server with any subsequent requests. // server with any subsequent requests.
document.cookie = `__next_hmr_refresh_hash__=${obj.hash}` document.cookie = `${NEXT_HMR_REFRESH_HASH_COOKIE}=${obj.hash}`
if (RuntimeErrorHandler.hadRuntimeError) { if (RuntimeErrorHandler.hadRuntimeError) {
if (reloading) return if (reloading) return
......
...@@ -16,13 +16,14 @@ import type { ...@@ -16,13 +16,14 @@ import type {
import type { Params } from '../request/params' import type { Params } from '../request/params'
import type { ImplicitTags } from '../lib/implicit-tags' import type { ImplicitTags } from '../lib/implicit-tags'
import type { WorkStore } from './work-async-storage.external' import type { WorkStore } from './work-async-storage.external'
import { NEXT_HMR_REFRESH_HASH_COOKIE } from '../../client/components/app-router-headers'
export type WorkUnitPhase = 'action' | 'render' | 'after' export type WorkUnitPhase = 'action' | 'render' | 'after'
export interface CommonWorkUnitStore { export interface CommonWorkUnitStore {
/** NOTE: Will be mutated as phases change */ /** NOTE: Will be mutated as phases change */
phase: WorkUnitPhase phase: WorkUnitPhase
readonly implicitTags: ImplicitTags | undefined readonly implicitTags: ImplicitTags
} }
export interface RequestStore extends CommonWorkUnitStore { export interface RequestStore extends CommonWorkUnitStore {
...@@ -121,6 +122,13 @@ export interface PrerenderStoreModern extends CommonWorkUnitStore { ...@@ -121,6 +122,13 @@ export interface PrerenderStoreModern extends CommonWorkUnitStore {
// not part of the primary render path and are just prerendering to produce // not part of the primary render path and are just prerendering to produce
// validation results // validation results
validating?: boolean validating?: boolean
/**
* The HMR refresh hash is only provided in dev mode. It is needed for the dev
* warmup render to ensure that the cache keys will be identical for the
* subsequent dynamic render.
*/
readonly hmrRefreshHash: string | undefined
} }
export interface PrerenderStorePPR extends CommonWorkUnitStore { export interface PrerenderStorePPR extends CommonWorkUnitStore {
...@@ -154,7 +162,16 @@ export type PrerenderStore = ...@@ -154,7 +162,16 @@ export type PrerenderStore =
| PrerenderStorePPR | PrerenderStorePPR
| PrerenderStoreModern | PrerenderStoreModern
export interface UseCacheStore extends CommonWorkUnitStore { export interface CommonCacheStore
extends Omit<CommonWorkUnitStore, 'implicitTags'> {
/**
* A cache work unit store might not always have an outer work unit store,
* from which implicit tags could be inherited.
*/
readonly implicitTags: ImplicitTags | undefined
}
export interface UseCacheStore extends CommonCacheStore {
type: 'cache' type: 'cache'
// Collected revalidate times and tags for this cache entry during the cache render. // Collected revalidate times and tags for this cache entry during the cache render.
revalidate: number // implicit revalidate time from inner caches / fetches revalidate: number // implicit revalidate time from inner caches / fetches
...@@ -173,7 +190,7 @@ export interface UseCacheStore extends CommonWorkUnitStore { ...@@ -173,7 +190,7 @@ export interface UseCacheStore extends CommonWorkUnitStore {
readonly draftMode: DraftModeProvider | undefined readonly draftMode: DraftModeProvider | undefined
} }
export interface UnstableCacheStore extends CommonWorkUnitStore { export interface UnstableCacheStore extends CommonCacheStore {
type: 'unstable-cache' type: 'unstable-cache'
// Draft mode is only available if the outer work unit store is a request // Draft mode is only available if the outer work unit store is a request
// store and draft mode is enabled. // store and draft mode is enabled.
...@@ -276,10 +293,10 @@ export function getHmrRefreshHash( ...@@ -276,10 +293,10 @@ export function getHmrRefreshHash(
return undefined return undefined
} }
return workUnitStore.type === 'cache' return workUnitStore.type === 'cache' || workUnitStore.type === 'prerender'
? workUnitStore.hmrRefreshHash ? workUnitStore.hmrRefreshHash
: workUnitStore.type === 'request' : workUnitStore.type === 'request'
? workUnitStore.cookies.get('__next_hmr_refresh_hash__')?.value ? workUnitStore.cookies.get(NEXT_HMR_REFRESH_HASH_COOKIE)?.value
: undefined : undefined
} }
......
...@@ -68,7 +68,7 @@ type RequestContext = RequestResponsePair & { ...@@ -68,7 +68,7 @@ type RequestContext = RequestResponsePair & {
renderOpts?: WrapperRenderOpts renderOpts?: WrapperRenderOpts
isHmrRefresh?: boolean isHmrRefresh?: boolean
serverComponentsHmrCache?: ServerComponentsHmrCache serverComponentsHmrCache?: ServerComponentsHmrCache
implicitTags: ImplicitTags | undefined implicitTags: ImplicitTags
} }
type RequestResponsePair = type RequestResponsePair =
......
...@@ -393,6 +393,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -393,6 +393,7 @@ export class AppRouteRouteModule extends RouteModule<
stale: INFINITE_CACHE, stale: INFINITE_CACHE,
tags: [...implicitTags.tags], tags: [...implicitTags.tags],
prerenderResumeDataCache: null, prerenderResumeDataCache: null,
hmrRefreshHash: undefined,
}) })
let prospectiveResult let prospectiveResult
...@@ -478,6 +479,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -478,6 +479,7 @@ export class AppRouteRouteModule extends RouteModule<
stale: INFINITE_CACHE, stale: INFINITE_CACHE,
tags: [...implicitTags.tags], tags: [...implicitTags.tags],
prerenderResumeDataCache: null, prerenderResumeDataCache: null,
hmrRefreshHash: undefined,
}) })
let responseHandled = false let responseHandled = false
......
...@@ -33,6 +33,7 @@ import { MiddlewareSpan } from '../lib/trace/constants' ...@@ -33,6 +33,7 @@ import { MiddlewareSpan } from '../lib/trace/constants'
import { CloseController } from './web-on-close' import { CloseController } from './web-on-close'
import { getEdgePreviewProps } from './get-edge-preview-props' import { getEdgePreviewProps } from './get-edge-preview-props'
import { getBuiltinRequestContext } from '../after/builtin-request-context' import { getBuiltinRequestContext } from '../after/builtin-request-context'
import { getImplicitTags } from '../lib/implicit-tags'
export class NextRequestHint extends NextRequest { export class NextRequestHint extends NextRequest {
sourcePage: string sourcePage: string
...@@ -251,18 +252,26 @@ export async function adapter( ...@@ -251,18 +252,26 @@ export async function adapter(
cookiesFromResponse = cookies cookiesFromResponse = cookies
} }
const previewProps = getEdgePreviewProps() const previewProps = getEdgePreviewProps()
const page = '/' // Fake Work
const fallbackRouteParams = null
const implicitTags = await getImplicitTags(
page,
request.nextUrl,
fallbackRouteParams
)
const requestStore = createRequestStoreForAPI( const requestStore = createRequestStoreForAPI(
request, request,
request.nextUrl, request.nextUrl,
undefined, implicitTags,
onUpdateCookies, onUpdateCookies,
previewProps previewProps
) )
const workStore = createWorkStore({ const workStore = createWorkStore({
page: '/', // Fake Work page,
fallbackRouteParams: null, fallbackRouteParams,
renderOpts: { renderOpts: {
cacheLifeProfiles: cacheLifeProfiles:
params.request.nextConfig?.experimental?.cacheLife, params.request.nextConfig?.experimental?.cacheLife,
......
import { revalidatePath } from 'next/cache'
export async function GET() {
revalidatePath('/')
return Response.json({ revalidated: true })
}
...@@ -21,13 +21,21 @@ describe('dynamic-io-dev-warmup', () => { ...@@ -21,13 +21,21 @@ describe('dynamic-io-dev-warmup', () => {
it('logs with Prerender or Server environment depending based on whether the timing of when the log runs relative to this environment boundary', async () => { it('logs with Prerender or Server environment depending based on whether the timing of when the log runs relative to this environment boundary', async () => {
let browser = await next.browser('/') let browser = await next.browser('/')
// At the moment this second render is required for the logs to resolves in the expected environment let logs = await browser.log()
// This doesn't reproduce locally but I suspect some kind of lazy initialization during dev that leads the initial render
// to not resolve in a microtask on the first render. assertLog(logs, 'after layout cache read', 'Prerender')
await browser.close() assertLog(logs, 'after page cache read', 'Prerender')
browser = await next.browser('/') assertLog(logs, 'after cached layout fetch', 'Prerender')
assertLog(logs, 'after cached page fetch', 'Prerender')
assertLog(logs, 'after uncached layout fetch', 'Server')
assertLog(logs, 'after uncached page fetch', 'Server')
const logs = await browser.log() // After a revalidation the subsequent warmup render must discard stale
// cache entries.
await next.fetch('/revalidate')
browser = await next.browser('/')
logs = await browser.log()
assertLog(logs, 'after layout cache read', 'Prerender') assertLog(logs, 'after layout cache read', 'Prerender')
assertLog(logs, 'after page cache read', 'Prerender') assertLog(logs, 'after page cache read', 'Prerender')
......
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