Skip to content

Commit

Permalink
refactor: renamed immutable and mutable as prerender and render
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Nov 5, 2024
1 parent 4070640 commit 383f09e
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 306 deletions.
76 changes: 38 additions & 38 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ 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 {
createMutableResumeDataCache,
sealResumeDataCache,
} from '../resume-data-cache/resume-data-cache'
import { createPrerenderResumeDataCache } from '../resume-data-cache/resume-data-cache'

export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug]
Expand Down Expand Up @@ -615,12 +612,13 @@ async function warmupDevRender(

// We're doing a dev warmup, so we should create a new resume data cache so
// we can fill it.
const mutableResumeDataCache = createMutableResumeDataCache()
const devWarmupPrerenderResumeDataCache = createPrerenderResumeDataCache()

// Attach this to the request store so that it can be used during the
// render.
const { requestStore } = ctx
requestStore.mutableResumeDataCache = mutableResumeDataCache
requestStore.devWarmupPrerenderResumeDataCache =
devWarmupPrerenderResumeDataCache

const rscPayload = await workUnitAsyncStorage.run(
requestStore,
Expand Down Expand Up @@ -650,14 +648,16 @@ async function warmupDevRender(
}
}

// 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

// 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,
devWarmupImmutableResumeDataCache: sealResumeDataCache(
mutableResumeDataCache
),
devWarmupPrerenderResumeDataCache,
})
}

Expand Down Expand Up @@ -1433,17 +1433,17 @@ export const renderToHTMLOrFlight: AppPageRender = (
}

if (
postponedState?.immutableResumeDataCache &&
renderOpts.devWarmupImmutableResumeDataCache
postponedState?.renderResumeDataCache &&
renderOpts.devWarmupRenderResumeDataCache
) {
throw new InvariantError(
'postponed state and dev warmup immutable resume data cache should not be provided together'
)
}

const immutableResumeDataCache =
renderOpts.devWarmupImmutableResumeDataCache ??
postponedState?.immutableResumeDataCache
const renderResumeDataCache =
renderOpts.devWarmupRenderResumeDataCache ??
postponedState?.renderResumeDataCache

const implicitTags = getImplicitTags(
renderOpts.routeModule.definition.page,
Expand All @@ -1461,7 +1461,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
url,
implicitTags,
renderOpts.onUpdateCookies,
immutableResumeDataCache,
renderResumeDataCache,
renderOpts.previewProps,
isHmrRefresh,
serverComponentsHmrCache
Expand Down Expand Up @@ -1984,7 +1984,7 @@ async function spawnDynamicValidationInDev(
const initialServerRenderController = new AbortController()

const cacheSignal = new CacheSignal()
const mutableResumeDataCache = createMutableResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()
const initialServerPrerenderStore: PrerenderStore = {
type: 'prerender',
phase: 'render',
Expand All @@ -1997,7 +1997,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

const initialClientController = new AbortController()
Expand All @@ -2013,7 +2013,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

// We're not going to use the result of this render because the only time it could be used
Expand Down Expand Up @@ -2145,7 +2145,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

const finalClientController = new AbortController()
Expand All @@ -2165,7 +2165,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

const finalServerPayload = await workUnitAsyncStorage.run(
Expand Down Expand Up @@ -2437,7 +2437,7 @@ async function prerenderToStream(
// performing a fresh prerender. If we get to implementing the
// prerendering of an already prerendered page, we should use the passed
// resume data cache instead.
const mutableResumeDataCache = createMutableResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()

const initialServerPrerenderStore: PrerenderStore = (prerenderStore = {
type: 'prerender',
Expand All @@ -2451,7 +2451,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

// We're not going to use the result of this render because the only time it could be used
Expand Down Expand Up @@ -2538,7 +2538,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

const prerender = require('react-dom/static.edge')
Expand Down Expand Up @@ -2618,7 +2618,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

const finalAttemptRSCPayload = await workUnitAsyncStorage.run(
Expand Down Expand Up @@ -2675,7 +2675,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

let clientIsDynamic = false
Expand Down Expand Up @@ -2769,12 +2769,12 @@ async function prerenderToStream(
metadata.postponed = await getDynamicHTMLPostponedState(
postponed,
fallbackRouteParams,
sealResumeDataCache(mutableResumeDataCache)
prerenderResumeDataCache
)
} else {
// Dynamic Data case
metadata.postponed = await getDynamicDataPostponedState(
sealResumeDataCache(mutableResumeDataCache)
prerenderResumeDataCache
)
}
reactServerResult.consume()
Expand Down Expand Up @@ -2893,7 +2893,7 @@ async function prerenderToStream(
const initialServerRenderController = new AbortController()

const cacheSignal = new CacheSignal()
const mutableResumeDataCache = createMutableResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()

const initialServerPrerenderStore: PrerenderStore = (prerenderStore = {
type: 'prerender',
Expand All @@ -2907,7 +2907,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

const initialClientController = new AbortController()
Expand All @@ -2923,7 +2923,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

// We're not going to use the result of this render because the only time it could be used
Expand Down Expand Up @@ -3066,7 +3066,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

let clientIsDynamic = false
Expand All @@ -3089,7 +3089,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

const finalServerPayload = await workUnitAsyncStorage.run(
Expand Down Expand Up @@ -3268,7 +3268,7 @@ async function prerenderToStream(
renderOpts.isDebugDynamicAccesses
)

const mutableResumeDataCache = createMutableResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()
const reactServerPrerenderStore: PrerenderStore = (prerenderStore = {
type: 'prerender-ppr',
phase: 'render',
Expand All @@ -3278,7 +3278,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})
const RSCPayload = await workUnitAsyncStorage.run(
reactServerPrerenderStore,
Expand Down Expand Up @@ -3310,7 +3310,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...ctx.requestStore.implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}
const prerender = require('react-dom/static.edge')
.prerender as (typeof import('react-dom/static.edge'))['prerender']
Expand Down Expand Up @@ -3383,12 +3383,12 @@ async function prerenderToStream(
metadata.postponed = await getDynamicHTMLPostponedState(
postponed,
fallbackRouteParams,
sealResumeDataCache(mutableResumeDataCache)
prerenderResumeDataCache
)
} else {
// Dynamic Data case.
metadata.postponed = await getDynamicDataPostponedState(
sealResumeDataCache(mutableResumeDataCache)
prerenderResumeDataCache
)
}
// Regardless of whether this is the Dynamic HTML or Dynamic Data case we need to ensure we include
Expand All @@ -3412,7 +3412,7 @@ async function prerenderToStream(
} else if (fallbackRouteParams && fallbackRouteParams.size > 0) {
// Rendering the fallback case.
metadata.postponed = await getDynamicDataPostponedState(
sealResumeDataCache(mutableResumeDataCache)
prerenderResumeDataCache
)

return {
Expand Down
33 changes: 11 additions & 22 deletions packages/next/src/server/app-render/postponed-state.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
createMutableResumeDataCache,
sealResumeDataCache,
} from '../resume-data-cache/resume-data-cache'
import { createPrerenderResumeDataCache } from '../resume-data-cache/resume-data-cache'
import { streamFromString } from '../stream-utils/node-web-streams-helper'
import {
DynamicState,
Expand All @@ -14,9 +11,9 @@ describe('getDynamicHTMLPostponedState', () => {
it('serializes a HTML postponed state with fallback params', async () => {
const key = '%%drp:slug:e9615126684e5%%'
const fallbackRouteParams = new Map([['slug', key]])
const mutableResumeDataCache = createMutableResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()

mutableResumeDataCache.cache.set(
prerenderResumeDataCache.cache.set(
'1',
Promise.resolve({
value: streamFromString('hello'),
Expand All @@ -31,7 +28,7 @@ describe('getDynamicHTMLPostponedState', () => {
const state = await getDynamicHTMLPostponedState(
{ [key]: key, nested: { [key]: key } },
fallbackRouteParams,
sealResumeDataCache(mutableResumeDataCache)
prerenderResumeDataCache
)

expect(state).toMatchInlineSnapshot(
Expand All @@ -43,7 +40,7 @@ describe('getDynamicHTMLPostponedState', () => {
const state = await getDynamicHTMLPostponedState(
{ key: 'value' },
null,
sealResumeDataCache(createMutableResumeDataCache())
createPrerenderResumeDataCache()
)
expect(state).toMatchInlineSnapshot(`"15:{"key":"value"}null"`)
})
Expand All @@ -54,7 +51,7 @@ describe('getDynamicHTMLPostponedState', () => {
const state = await getDynamicHTMLPostponedState(
{ [key]: key },
fallbackRouteParams,
sealResumeDataCache(createMutableResumeDataCache())
createPrerenderResumeDataCache()
)

const value = 'hello'
Expand All @@ -63,9 +60,7 @@ describe('getDynamicHTMLPostponedState', () => {
expect(parsed).toEqual({
type: DynamicState.HTML,
data: { [value]: value },
immutableResumeDataCache: sealResumeDataCache(
createMutableResumeDataCache()
),
renderResumeDataCache: createPrerenderResumeDataCache(),
})

// The replacements have been replaced.
Expand All @@ -76,7 +71,7 @@ describe('getDynamicHTMLPostponedState', () => {
describe('getDynamicDataPostponedState', () => {
it('serializes a data postponed state with fallback params', async () => {
const state = await getDynamicDataPostponedState(
sealResumeDataCache(createMutableResumeDataCache())
createPrerenderResumeDataCache()
)
expect(state).toMatchInlineSnapshot(`"4:nullnull"`)
})
Expand All @@ -94,9 +89,7 @@ describe('parsePostponedState', () => {
expect(parsed).toEqual({
type: DynamicState.HTML,
data: expect.any(Object),
immutableResumeDataCache: sealResumeDataCache(
createMutableResumeDataCache()
),
renderResumeDataCache: createPrerenderResumeDataCache(),
})

// Ensure that the replacement worked and removed all the placeholders.
Expand All @@ -112,9 +105,7 @@ describe('parsePostponedState', () => {
expect(parsed).toEqual({
type: DynamicState.HTML,
data: expect.any(Object),
immutableResumeDataCache: sealResumeDataCache(
createMutableResumeDataCache()
),
renderResumeDataCache: createPrerenderResumeDataCache(),
})
})

Expand All @@ -125,9 +116,7 @@ describe('parsePostponedState', () => {
// Ensure that it parsed it correctly.
expect(parsed).toEqual({
type: DynamicState.DATA,
immutableResumeDataCache: sealResumeDataCache(
createMutableResumeDataCache()
),
renderResumeDataCache: createPrerenderResumeDataCache(),
})
})
})
Loading

0 comments on commit 383f09e

Please sign in to comment.