Unverified Commit 4dad02d1 authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] abort warmup after caches are complete (#72474)

When we warmup we are trying to fill caches so the actual dev dynamic
render can have the right environment for logs to help with debugging.
If we have no inflight cache reads/fills happening there is no reason to
continue the warmup.

This change updates the warmup function to wait for the cacheSignal to
indicate it is complete before aborting. This should not alter any
program behavior but is observable if you contrive a very slow prefetch
request like awaiting a setTimeout inside loading.tsx

This allows us to remove the dev only property tracking the
prerenderResumeCache in the request store type. I also moved the
prerender->render cache conversion into the warmup function since it
leaks less about the implementation generatlized the name since it being
a cache from a warmup is sort of an implementation detail from the
render interface perspective so we'll just call it a
devRenderResumeCache. still quite the mouthful
parent 81271bae
......@@ -171,7 +171,10 @@ import './clean-async-snapshot.external'
import { INFINITE_CACHE } from '../../lib/constants'
import { createComponentStylesAndScripts } from './create-component-styles-and-scripts'
import { parseLoaderTree } from './parse-loader-tree'
import { createPrerenderResumeDataCache } from '../resume-data-cache/resume-data-cache'
import {
createPrerenderResumeDataCache,
createRenderResumeDataCache,
} from '../resume-data-cache/resume-data-cache'
export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug]
......@@ -590,8 +593,7 @@ async function generateDynamicFlightRenderResult(
*/
async function warmupDevRender(
req: BaseNextRequest,
ctx: AppRenderContext,
requestStore: RequestStore
ctx: AppRenderContext
): Promise<RenderResult> {
const renderOpts = ctx.renderOpts
if (!renderOpts.dev) {
......@@ -614,50 +616,60 @@ async function warmupDevRender(
// We're doing a dev warmup, so we should create a new resume data cache so
// we can fill it.
const devWarmupPrerenderResumeDataCache = createPrerenderResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()
// Attach this to the request store so that it can be used during the
// render.
requestStore.devWarmupPrerenderResumeDataCache =
devWarmupPrerenderResumeDataCache
const renderController = new AbortController()
const prerenderController = new AbortController()
const cacheSignal = new CacheSignal()
const prerenderStore: PrerenderStore = {
type: 'prerender',
phase: 'render',
implicitTags: [],
renderSignal: renderController.signal,
controller: prerenderController,
cacheSignal,
dynamicTracking: null,
revalidate: INFINITE_CACHE,
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [],
prerenderResumeDataCache,
}
const rscPayload = await workUnitAsyncStorage.run(
requestStore,
prerenderStore,
generateDynamicRSCPayload,
ctx
)
// For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
// which contains the subset React.
const flightReadableStream = workUnitAsyncStorage.run(
requestStore,
workUnitAsyncStorage.run(
prerenderStore,
ctx.componentMod.renderToReadableStream,
rscPayload,
ctx.clientReferenceManifest.clientModules,
{
onError,
signal: renderController.signal,
}
)
const reader = flightReadableStream.getReader()
// Read the entire stream.
while (true) {
if ((await reader.read()).done) {
break
}
}
// As we're finished rendering, remove the reference to the prerender resume
// data cache so it can't be written to again.
requestStore.devWarmupPrerenderResumeDataCache = null
// Wait for all caches to be finished filling
await cacheSignal.cacheReady()
// We unset the cache so any late over-run renders aren't able to write into this cache
prerenderStore.prerenderResumeDataCache = null
// Abort the render
renderController.abort()
// We don't really want to return a result here but the stack of functions
// that calls into renderToHTML... expects a result. We should refactor this to
// lift the warmup pathway outside of renderToHTML... but for now this suffices
return new FlightRenderResult('', {
fetchMetrics: ctx.workStore.fetchMetrics,
devWarmupPrerenderResumeDataCache,
devRenderResumeDataCache: createRenderResumeDataCache(
prerenderResumeDataCache
),
})
}
......@@ -1270,7 +1282,7 @@ async function renderToHTMLOrFlightImpl(
} else {
// We're rendering dynamically
const renderResumeDataCache =
renderOpts.devWarmupRenderResumeDataCache ??
renderOpts.devRenderResumeDataCache ??
postponedState?.renderResumeDataCache
const requestStore = createRequestStoreForRender(
......@@ -1304,7 +1316,7 @@ async function renderToHTMLOrFlightImpl(
}
if (isDevWarmupRequest) {
return warmupDevRender(req, ctx, requestStore)
return warmupDevRender(req, ctx)
} else if (isRSCRequest) {
return generateDynamicFlightRenderResult(req, ctx, requestStore)
}
......@@ -1453,7 +1465,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
if (
postponedState?.renderResumeDataCache &&
renderOpts.devWarmupRenderResumeDataCache
renderOpts.devRenderResumeDataCache
) {
throw new InvariantError(
'postponed state and dev warmup immutable resume data cache should not be provided together'
......
......@@ -189,7 +189,7 @@ export interface RenderOptsPartial {
* The resume data cache that was generated for this partially prerendered
* page during dev warmup.
*/
devWarmupRenderResumeDataCache?: RenderResumeDataCache
devRenderResumeDataCache?: RenderResumeDataCache
/**
* When true, only the static shell of the page will be rendered. This will
......
......@@ -61,11 +61,6 @@ export type RequestStore = {
// DEV-only
usedDynamic?: boolean
prerenderPhase?: boolean
/**
* The resume data cache for this request. This will be a mutable cache.
*/
devWarmupPrerenderResumeDataCache: PrerenderResumeDataCache | null
} & PhasePartial
/**
......@@ -220,14 +215,9 @@ export function getPrerenderResumeDataCache(
workUnitStore: WorkUnitStore
): PrerenderResumeDataCache | null {
if (
workUnitStore.type !== 'prerender-legacy' &&
workUnitStore.type !== 'cache' &&
workUnitStore.type !== 'unstable-cache'
workUnitStore.type === 'prerender' ||
workUnitStore.type === 'prerender-ppr'
) {
if (workUnitStore.type === 'request') {
return workUnitStore.devWarmupPrerenderResumeDataCache
}
return workUnitStore.prerenderResumeDataCache
}
......
......@@ -248,7 +248,6 @@ function createRequestStoreImpl(
return cache.draftMode
},
renderResumeDataCache: renderResumeDataCache ?? null,
devWarmupPrerenderResumeDataCache: null,
isHmrRefresh,
serverComponentsHmrCache:
serverComponentsHmrCache ||
......
......@@ -175,7 +175,6 @@ import type { RouteModule } from './route-modules/route-module'
import { FallbackMode, parseFallbackField } from '../lib/fallback'
import { toResponseCacheEntry } from './response-cache/utils'
import { scheduleOnNextTick } from '../lib/scheduler'
import { createRenderResumeDataCache } from './resume-data-cache/resume-data-cache'
export type FindComponentsResult = {
components: LoadComponentsReturnType
......@@ -2654,11 +2653,9 @@ export default abstract class Server<
// If the warmup is successful, we should use the resume data
// cache from the warmup.
if (warmup.metadata.devWarmupPrerenderResumeDataCache) {
renderOpts.devWarmupRenderResumeDataCache =
createRenderResumeDataCache(
warmup.metadata.devWarmupPrerenderResumeDataCache
)
if (warmup.metadata.devRenderResumeDataCache) {
renderOpts.devRenderResumeDataCache =
warmup.metadata.devRenderResumeDataCache
}
}
......
......@@ -10,7 +10,7 @@ import {
streamToString,
} from './stream-utils/node-web-streams-helper'
import { isAbortError, pipeToNodeResponse } from './pipe-readable'
import type { PrerenderResumeDataCache } from './resume-data-cache/resume-data-cache'
import type { RenderResumeDataCache } from './resume-data-cache/resume-data-cache'
type ContentTypeOption = string | undefined
......@@ -40,7 +40,7 @@ export type AppPageRenderResultMetadata = {
* In development, the cache is warmed up before the render. This is attached
* to the metadata so that it can be used during the render.
*/
devWarmupPrerenderResumeDataCache?: PrerenderResumeDataCache
devRenderResumeDataCache?: RenderResumeDataCache
}
export type PagesRenderResultMetadata = {
......
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