Unverified Commit 29a573dd authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] update prerender cache scoping and cache warming for validation (#71822)

Updates the heuristic to scoping prerender caches during dev. A cache
scope will be created for every request if one does not already exist.
If the request is a server action we create a new one. existing caches
already expire after 30 seconds (unchanged)

This updates the prefetch cache purge to be 5 seconds since so we don't
mask short lifetime caches

This also adds cache tracking to the first RSC render of the validation
render. This means we now need to always do a second render regardless
of whether there is a sync abort.
parent af4321c5
...@@ -1942,6 +1942,7 @@ async function spawnDynamicValidationInDev( ...@@ -1942,6 +1942,7 @@ async function spawnDynamicValidationInDev(
): Promise<void> { ): Promise<void> {
const { componentMod: ComponentMod } = ctx const { componentMod: ComponentMod } = ctx
const cacheSignal = new CacheSignal()
const firstAttemptServerController = new AbortController() const firstAttemptServerController = new AbortController()
let serverDynamicTracking = createDynamicTrackingState(false) let serverDynamicTracking = createDynamicTrackingState(false)
...@@ -1950,7 +1951,7 @@ async function spawnDynamicValidationInDev( ...@@ -1950,7 +1951,7 @@ async function spawnDynamicValidationInDev(
phase: 'render', phase: 'render',
implicitTags: [], implicitTags: [],
renderSignal: firstAttemptServerController.signal, renderSignal: firstAttemptServerController.signal,
cacheSignal: null, cacheSignal,
// During the prospective render we don't want to synchronously abort on dynamic access // During the prospective render we don't want to synchronously abort on dynamic access
// because it could prevent us from discovering all caches in siblings. So we omit the controller // because it could prevent us from discovering all caches in siblings. So we omit the controller
// from the prerender store this time. // from the prerender store this time.
...@@ -1958,12 +1959,12 @@ async function spawnDynamicValidationInDev( ...@@ -1958,12 +1959,12 @@ async function spawnDynamicValidationInDev(
// With PPR during Prerender we don't need to track individual dynamic reasons // With PPR during Prerender we don't need to track individual dynamic reasons
// because we will always do a final render after caches have filled and we // because we will always do a final render after caches have filled and we
// will track it again there // will track it again there
dynamicTracking: serverDynamicTracking, dynamicTracking: null,
revalidate: INFINITE_CACHE, revalidate: INFINITE_CACHE,
expire: INFINITE_CACHE, expire: INFINITE_CACHE,
stale: INFINITE_CACHE, stale: INFINITE_CACHE,
tags: [], tags: [],
// Dev only property that allows certain logs to be supressed // Dev only property that allows certain logs to be suppressed
validating: true, validating: true,
} }
...@@ -1977,9 +1978,7 @@ async function spawnDynamicValidationInDev( ...@@ -1977,9 +1978,7 @@ async function spawnDynamicValidationInDev(
let reactServerStream = await workUnitAsyncStorage.run( let reactServerStream = await workUnitAsyncStorage.run(
firstAttemptServerPrerenderStore, firstAttemptServerPrerenderStore,
scheduleInSequentialTasks, ComponentMod.renderToReadableStream,
() => {
const stream = ComponentMod.renderToReadableStream(
firstAttemptRSCPayload, firstAttemptRSCPayload,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
...@@ -1987,16 +1986,9 @@ async function spawnDynamicValidationInDev( ...@@ -1987,16 +1986,9 @@ async function spawnDynamicValidationInDev(
onError: () => {}, onError: () => {},
} }
) )
return asHaltedStream(stream, firstAttemptServerController.signal)
},
() => {
firstAttemptServerController.abort()
}
)
if (serverDynamicTracking.syncDynamicErrorWithStack) { await cacheSignal.cacheReady()
// If we had a sync dynamic error then we need to retry without firstAttemptServerController.abort()
reactServerStream.cancel()
const secondAttemptServerController = new AbortController() const secondAttemptServerController = new AbortController()
serverDynamicTracking = createDynamicTrackingState(false) serverDynamicTracking = createDynamicTrackingState(false)
...@@ -2019,7 +2011,7 @@ async function spawnDynamicValidationInDev( ...@@ -2019,7 +2011,7 @@ async function spawnDynamicValidationInDev(
expire: INFINITE_CACHE, expire: INFINITE_CACHE,
stale: INFINITE_CACHE, stale: INFINITE_CACHE,
tags: [], tags: [],
// Dev only property that allows certain logs to be supressed // Dev only property that allows certain logs to be suppressed
validating: true, validating: true,
} }
...@@ -2049,7 +2041,6 @@ async function spawnDynamicValidationInDev( ...@@ -2049,7 +2041,6 @@ async function spawnDynamicValidationInDev(
secondAttemptServerController.abort() secondAttemptServerController.abort()
} }
) )
}
const [warmupStream, renderStream] = reactServerStream.tee() const [warmupStream, renderStream] = reactServerStream.tee()
......
...@@ -3085,34 +3085,27 @@ export default abstract class Server< ...@@ -3085,34 +3085,27 @@ export default abstract class Server<
if (this.renderOpts.dev) { if (this.renderOpts.dev) {
let cache = this.prefetchCacheScopesDev.get(urlPathname) let cache = this.prefetchCacheScopesDev.get(urlPathname)
if (isServerAction || !cache) {
cache = new Map()
this.prefetchCacheScopesDev.set(urlPathname, cache)
}
// we need to seed the prefetch cache scope in dev // we need to seed the prefetch cache scope in dev
// since we did not have a prefetch cache available // since we did not have a prefetch cache available
// and this is not a prefetch request // and this is not a prefetch request
if ( if (
!cache &&
!isPrefetchRSCRequest && !isPrefetchRSCRequest &&
routeModule?.definition.kind === RouteKind.APP_PAGE && routeModule?.definition.kind === RouteKind.APP_PAGE &&
!isServerAction !isServerAction
) { ) {
cache = new Map()
await runWithCacheScope({ cache }, () => await runWithCacheScope({ cache }, () =>
originalResponseGenerator({ ...state, isDevWarmup: true }) originalResponseGenerator({ ...state, isDevWarmup: true })
) )
this.prefetchCacheScopesDev.set(urlPathname, cache)
} }
if (cache) {
return runWithCacheScope({ cache }, () => return runWithCacheScope({ cache }, () =>
originalResponseGenerator(state) originalResponseGenerator(state)
).finally(() => { )
if (isPrefetchRSCRequest) {
this.prefetchCacheScopesDev.set(urlPathname, cache)
} else {
this.prefetchCacheScopesDev.del(urlPathname)
}
})
}
} }
return originalResponseGenerator(state) return originalResponseGenerator(state)
......
...@@ -13,7 +13,7 @@ export class PrefetchCacheScopes { ...@@ -13,7 +13,7 @@ export class PrefetchCacheScopes {
private evict() { private evict() {
for (const [key, value] of this.cacheScopes) { for (const [key, value] of this.cacheScopes) {
if (value.timestamp < Date.now() - 30_000) { if (value.timestamp < Date.now() - 5_000) {
this.cacheScopes.delete(key) this.cacheScopes.delete(key)
} }
} }
...@@ -23,7 +23,14 @@ export class PrefetchCacheScopes { ...@@ -23,7 +23,14 @@ export class PrefetchCacheScopes {
// filter _rsc query // filter _rsc query
get(url: string) { get(url: string) {
setImmediate(() => this.evict()) setImmediate(() => this.evict())
return this.cacheScopes.get(url)?.cache const currentScope = this.cacheScopes.get(url)
if (currentScope) {
if (currentScope.timestamp < Date.now() - 5_000) {
return undefined
}
return currentScope.cache
}
return undefined
} }
set(url: string, cache: CacheScopeStore['cache']) { set(url: string, cache: CacheScopeStore['cache']) {
......
...@@ -516,6 +516,14 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -516,6 +516,14 @@ export function cache(kind: string, id: string, fn: any) {
const cacheScope: undefined | CacheScopeStore = const cacheScope: undefined | CacheScopeStore =
cacheScopeAsyncLocalStorage.getStore() cacheScopeAsyncLocalStorage.getStore()
if (cacheScope) { if (cacheScope) {
const cacheSignal =
workUnitStore && workUnitStore.type === 'prerender'
? workUnitStore.cacheSignal
: null
if (cacheSignal) {
cacheSignal.beginRead()
}
const cachedEntry: undefined | Promise<CacheEntry> = const cachedEntry: undefined | Promise<CacheEntry> =
cacheScope.cache.get(serializedCacheKey) cacheScope.cache.get(serializedCacheKey)
if (cachedEntry !== undefined) { if (cachedEntry !== undefined) {
...@@ -532,6 +540,9 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -532,6 +540,9 @@ export function cache(kind: string, id: string, fn: any) {
// expire time is under 5 minutes, then we consider this cache entry dynamic // expire time is under 5 minutes, then we consider this cache entry dynamic
// as it's not worth generating static pages for such data. It's better to leave // as it's not worth generating static pages for such data. It's better to leave
// a PPR hole that can be filled in dynamically with a potentially cached entry. // a PPR hole that can be filled in dynamically with a potentially cached entry.
if (cacheSignal) {
cacheSignal.endRead()
}
return makeHangingPromise( return makeHangingPromise(
workUnitStore.renderSignal, workUnitStore.renderSignal,
'dynamic "use cache"' 'dynamic "use cache"'
...@@ -539,8 +550,35 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -539,8 +550,35 @@ export function cache(kind: string, id: string, fn: any) {
} }
const [streamA, streamB] = existingEntry.value.tee() const [streamA, streamB] = existingEntry.value.tee()
existingEntry.value = streamB existingEntry.value = streamB
if (cacheSignal) {
// When we have a cacheSignal we need to block on reading the cache
// entry before ending the read.
const buffer: any[] = []
const reader = streamA.getReader()
for (let entry; !(entry = await reader.read()).done; ) {
buffer.push(entry.value)
}
let idx = 0
stream = new ReadableStream({
pull(controller) {
if (idx < buffer.length) {
controller.enqueue(buffer[idx++])
} else {
controller.close()
}
},
})
cacheSignal.endRead()
} else {
stream = streamA stream = streamA
} }
} else {
if (cacheSignal) {
cacheSignal.endRead()
}
}
} }
if (stream === undefined) { if (stream === undefined) {
......
function delay() {
return new Promise((resolve) => {
setTimeout(resolve, 100)
})
}
export async function fetchData() {
await delay()
return '' + Math.random()
}
import {
revalidateTag,
unstable_cacheLife as cacheLife,
unstable_cacheTag,
} from 'next/cache'
import { fetchData } from '../api/data'
// import { Suspense } from 'react'
// import { cookies, headers } from 'next/headers'
function InnerComponent({ children }) {
return <span id="value">{children}</span>
}
async function refresh() {
'use server'
revalidateTag('hello')
}
async function reload() {
'use server'
}
async function Component() {
'use cache'
cacheLife({ revalidate: 30 })
unstable_cacheTag('hello')
return <InnerComponent>{await fetchData()}</InnerComponent>
}
export default async function Home() {
return (
<>
<form action={refresh}>
<button id="refresh">Refresh</button>
</form>
<form action={reload}>
<button id="reload">Reload</button>
</form>
<Component />
</>
)
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { fetchData } from '../api/data'
// import { Suspense } from 'react'
// import { cookies, headers } from 'next/headers'
function InnerComponent({ children }) {
return <span id="value">{children}</span>
}
async function refresh() {
'use server'
}
async function reload() {
'use server'
}
async function Component() {
return <InnerComponent>{await fetchData()}</InnerComponent>
}
export default async function Home() {
return (
<>
<form action={refresh}>
<button id="refresh">Refresh</button>
</form>
<form action={reload}>
<button id="reload">Reload</button>
</form>
<Component />
</>
)
}
import { nextTestSetup } from 'e2e-utils'
import {
getRedboxDescription,
waitForAndOpenRuntimeError,
assertNoRedbox,
retry,
} from 'next-test-utils'
describe('Dynamic IO Dev Errors', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should not show a red box error on the SSR render', async () => {
const browser = await next.browser('/cached')
await assertNoRedbox(browser)
let latestValue = await browser.elementByCss('#value').text()
await browser.refresh()
await assertNoRedbox(browser)
let priorValue = latestValue
latestValue = await browser.elementByCss('#value').text()
expect(latestValue).toBe(priorValue)
await browser.elementByCss('#refresh').click()
await retry(async () => {
await assertNoRedbox(browser)
let priorValue = latestValue
latestValue = await browser.elementByCss('#value').text()
expect(latestValue).not.toBe(priorValue)
})
await browser.elementByCss('#refresh').click()
await retry(async () => {
await assertNoRedbox(browser)
let priorValue = latestValue
latestValue = await browser.elementByCss('#value').text()
expect(latestValue).not.toBe(priorValue)
})
// TODO CI is too flakey to run tests like this b/c timing cannot be controlled.
// we need to land this so we're deactivating these assertions for now.
// you should be able to reproduce the assertions below when testing locally
// await browser.elementByCss('#reload').click()
// await retry(async () => {
// await assertNoRedbox(browser)
// let priorValue = latestValue
// latestValue = await browser.elementByCss('#value').text()
// expect(latestValue).toBe(priorValue)
// })
// await browser.elementByCss('#reload').click()
// await retry(async () => {
// await assertNoRedbox(browser)
// let priorValue = latestValue
// latestValue = await browser.elementByCss('#value').text()
// expect(latestValue).toBe(priorValue)
// })
// await browser.elementByCss('#refresh').click()
// await retry(async () => {
// await assertNoRedbox(browser)
// let priorValue = latestValue
// latestValue = await browser.elementByCss('#value').text()
// expect(latestValue).not.toBe(priorValue)
// })g
})
it('should show a red box error on the SSR render when data is uncached', async () => {
let desc
const browser = await next.browser('/uncached')
await waitForAndOpenRuntimeError(browser)
desc = await getRedboxDescription(browser)
expect(desc).toContain(
'Route "/uncached": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it'
)
await browser.refresh()
await waitForAndOpenRuntimeError(browser)
desc = await getRedboxDescription(browser)
expect(desc).toContain(
'Route "/uncached": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it'
)
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
...@@ -48,6 +48,8 @@ describe('dynamic-io', () => { ...@@ -48,6 +48,8 @@ describe('dynamic-io', () => {
expect.stringContaining('`draftMode().isEnabled`'), expect.stringContaining('`draftMode().isEnabled`'),
// TODO need to figure out why deduping isn't working here // TODO need to figure out why deduping isn't working here
expect.stringContaining('`draftMode().isEnabled`'), expect.stringContaining('`draftMode().isEnabled`'),
// TODO need to figure out why deduping isn't working here
expect.stringContaining('`draftMode().isEnabled`'),
]) ])
} else { } else {
expect($('#layout').text()).toBe('at buildtime') expect($('#layout').text()).toBe('at buildtime')
......
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