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 2e06e00 commit e6c5d95
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 355 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 @@ -1435,17 +1435,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 @@ -1463,7 +1463,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
url,
implicitTags,
renderOpts.onUpdateCookies,
immutableResumeDataCache,
renderResumeDataCache,
renderOpts.previewProps,
isHmrRefresh,
serverComponentsHmrCache
Expand Down Expand Up @@ -1987,7 +1987,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 @@ -2000,7 +2000,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [],
mutableResumeDataCache,
prerenderResumeDataCache,
}

const initialClientController = new AbortController()
Expand All @@ -2016,7 +2016,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [],
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 @@ -2148,7 +2148,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [],
mutableResumeDataCache,
prerenderResumeDataCache,
}

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

const finalServerPayload = await workUnitAsyncStorage.run(
Expand Down Expand Up @@ -2439,7 +2439,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 @@ -2453,7 +2453,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...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 @@ -2540,7 +2540,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}

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

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

let clientIsDynamic = false
Expand Down Expand Up @@ -2772,12 +2772,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 @@ -2896,7 +2896,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 @@ -2910,7 +2910,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

const initialClientController = new AbortController()
Expand All @@ -2926,7 +2926,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...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 @@ -3069,7 +3069,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})

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

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

const mutableResumeDataCache = createMutableResumeDataCache()
const prerenderResumeDataCache = createPrerenderResumeDataCache()
const reactServerPrerenderStore: PrerenderStore = (prerenderStore = {
type: 'prerender-ppr',
phase: 'render',
Expand All @@ -3282,7 +3282,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
})
const RSCPayload = await workUnitAsyncStorage.run(
reactServerPrerenderStore,
Expand Down Expand Up @@ -3314,7 +3314,7 @@ async function prerenderToStream(
expire: INFINITE_CACHE,
stale: INFINITE_CACHE,
tags: [...implicitTags],
mutableResumeDataCache,
prerenderResumeDataCache,
}
const prerender = require('react-dom/static.edge')
.prerender as (typeof import('react-dom/static.edge'))['prerender']
Expand Down Expand Up @@ -3388,12 +3388,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 @@ -3417,7 +3417,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 e6c5d95

Please sign in to comment.