Unverified Commit 6f4d94ac authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Stream Cache Components dev render instead of restarting on cache miss (#94457)

When Cache Components is enabled, `next dev` previously simulated a
production loading experience on every cold request. The render did a
prospective pass to detect cache misses, and on any miss it waited for
every cache to fill via `cacheSignal.cacheReady()` and then restarted
the render with warm caches before streaming anything, so the browser
saw nothing until the slowest cache had filled. Every cold load blocked
on cache population.

This change replaces the restart-on-cache-miss flow with a single
non-abandoning staged render that streams immediately and fills caches
as a side effect. On a cold load the Suspense fallbacks stream right
away and the cached content resolves as its cache fills; on a warm
reload the staged progression matches the previous no-cache-miss path.
The render is split into clearly owned pieces: `setUpStagedDevRender`
builds the staged controller, cache signal, and resume cache;
`streamStagedRenderInDev{Node,Web}` runs the streaming render and
reports a result once the stream has fully finished; and
`stagedRenderWithCachesInDev{Node,Web}` returns the stream and leaves
the validation follow-up detached so it never blocks the response.

The render advances its stages in sequential tasks, and the stream is
not handed back the instant it exists: it is held until the render has
advanced through the stage whose content belongs in the shell. That is
the static stage for initial loads, HMR refreshes, and plain
navigations, or the runtime stage for client navigations to a route with
a runtime prefetch config, whose runtime-prefetchable content the
navigation's prefetch would have settled. It never waits for the dynamic
stage. Buffering the shell before the first flush keeps the streaming
renderer from emitting a premature Suspense fallback for content that
belongs in the shell, and it mirrors production, where the static shell
(plus runtime-prefetchable content where configured) is served and the
remaining holes stream in as fallbacks.

Two internal reads that would otherwise register as synchronous IO and
wrongly force the render to the dynamic stage, the cache handler's
tag-expiry clock check and the hot reloader's module-scope dev client
id, are now read untracked: via `performance.timeOrigin +
performance.now()` like the `'use cache'` handler, and only in the
browser where the HMR connection reads it, respectively.

Cache Components rules validation now runs in that background follow-up,
once the streamed render has fully settled. `planDevValidation` inspects
the finished render and picks one of three paths: forward an invalid
dynamic usage error the streamed render already recorded and stop (for
example a request API used inside `'use cache'`); validate the streamed
render's own chunks when it neither missed caches nor hit sync IO; or,
when it did either, validate a dedicated warm-cache render instead.
Because that warm render reads the filled caches back rather than
filling them, it can surface an invalid dynamic usage error the cold
streamed render cannot, such as a nested dynamic `use cache` cache life
that propagated to a parent with no explicit `cacheLife`; that error is
forwarded and validation is skipped, just as one recorded by the
streamed render is.

Since cold loads no longer block on cache fills, the transient
cache-status indicator that reflected that wait is no longer emitted; a
follow-up will instead add an indicator that tells the user whether a
render streamed with cache misses, and so wasn't representative of
production.
parent 1e601b6b
...@@ -52,7 +52,14 @@ const createFromReadableStream = ...@@ -52,7 +52,14 @@ const createFromReadableStream =
createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream'] createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream']
let mostRecentCompilationHash: any = null let mostRecentCompilationHash: any = null
let __nextDevClientId = Math.round(Math.random() * 100 + Date.now()) // The dev client id is only read by the browser-side HMR websocket connection.
// Compute it only in the browser: there is no client during SSR, and calling
// `Math.random()`/`Date.now()` at module scope would be tracked as sync IO by
// Cache Components and advance the render stage.
let __nextDevClientId =
typeof window !== 'undefined'
? Math.round(Math.random() * 100 + Date.now())
: 0
let reloading = false let reloading = false
let webpackStartMsSinceEpoch: number | null = null let webpackStartMsSinceEpoch: number | null = null
const turbopackHmr: TurbopackHmr | null = process.env.TURBOPACK const turbopackHmr: TurbopackHmr | null = process.env.TURBOPACK
......
...@@ -81,21 +81,25 @@ export function createReactServerErrorHandler( ...@@ -81,21 +81,25 @@ export function createReactServerErrorHandler(
// If the error already has a digest, respect the original digest, // If the error already has a digest, respect the original digest,
// so it won't get re-generated into another new error. // so it won't get re-generated into another new error.
if (err.digest) { if (err.digest) {
if ( const originalError = reactServerErrors.get(err.digest)
process.env.NODE_ENV === 'production' &&
reactServerErrors.has(err.digest) if (originalError) {
) { // This error crossed a react-server boundary (e.g. from a `'use cache'`
// This error is likely an obfuscated error from another react-server // render). Reaching the handler means it surfaced (it wasn't caught in
// environment (e.g. 'use cache'). We recover the original error here // userland), so stamp the digest onto the original to mark it surfaced.
// for reporting purposes. // If the original was recorded as `invalidDynamicUsageError` without a
err = reactServerErrors.get(err.digest)! // digest (a cache that aborted across the boundary), this is what lets
// We don't log it again though, as it was already logged in the // the dev overlay dedup: the separate forwarding checks for that digest
// original environment. // and skips it.
silenceLog = true originalError.digest ??= err.digest
} else {
// Either we're in development (where we want to keep the transported if (process.env.NODE_ENV === 'production') {
// error with environmentName), or the error is not in reactServerErrors // In production we use the recovered original (de-obfuscated!) error
// but has a digest from other means. Keep the error as-is. // for reporting, and don't log it again as it was already logged in
// the original environment.
err = originalError
silenceLog = true
}
} }
} else { } else {
// TODO-APP: look at using webcrypto instead of string-hash. Requires a promise to be awaited. // TODO-APP: look at using webcrypto instead of string-hash. Requires a promise to be awaited.
......
...@@ -15,7 +15,7 @@ export const areTagsExpired = (tags: string[], timestamp: Timestamp) => { ...@@ -15,7 +15,7 @@ export const areTagsExpired = (tags: string[], timestamp: Timestamp) => {
const expiredAt = entry?.expired const expiredAt = entry?.expired
if (typeof expiredAt === 'number') { if (typeof expiredAt === 'number') {
const now = Date.now() const now = performance.timeOrigin + performance.now()
// For immediate expiration (expiredAt <= now) and tag was invalidated after entry was created // For immediate expiration (expiredAt <= now) and tag was invalidated after entry was created
// OR for future expiration that has now passed (expiredAt > timestamp && expiredAt <= now) // OR for future expiration that has now passed (expiredAt > timestamp && expiredAt <= now)
const isImmediatelyExpired = expiredAt <= now && expiredAt > timestamp const isImmediatelyExpired = expiredAt <= now && expiredAt > timestamp
......
...@@ -56,7 +56,12 @@ import type { CacheEntry } from '../lib/cache-handlers/types' ...@@ -56,7 +56,12 @@ import type { CacheEntry } from '../lib/cache-handlers/types'
import type { CacheSignal } from '../app-render/cache-signal' import type { CacheSignal } from '../app-render/cache-signal'
import { decryptActionBoundArgs } from '../app-render/encryption' import { decryptActionBoundArgs } from '../app-render/encryption'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { createReactServerErrorHandler } from '../app-render/create-error-handler' import {
createReactServerErrorHandler,
type DigestedError,
} from '../app-render/create-error-handler'
import { createDigestWithErrorCode } from '../../lib/error-telemetry-utils'
import stringHash from 'next/dist/compiled/string-hash'
import { DYNAMIC_EXPIRE, RUNTIME_PREFETCH_DYNAMIC_STALE } from './constants' import { DYNAMIC_EXPIRE, RUNTIME_PREFETCH_DYNAMIC_STALE } from './constants'
import { NEXT_CACHE_ROOT_PARAM_TAG_ID } from '../../lib/constants' import { NEXT_CACHE_ROOT_PARAM_TAG_ID } from '../../lib/constants'
import type { CacheHandler } from '../lib/cache-handlers/types' import type { CacheHandler } from '../lib/cache-handlers/types'
...@@ -1215,29 +1220,36 @@ async function generateCacheEntryImpl( ...@@ -1215,29 +1220,36 @@ async function generateCacheEntryImpl(
// relevant in restart-on-cache-miss in general, so when we implement that // relevant in restart-on-cache-miss in general, so when we implement that
// for cached navs, it'll also be needed in prod // for cached navs, it'll also be needed in prod
if (process.env.__NEXT_DEV_SERVER && outerWorkUnitStore.cacheSignal) { if (process.env.__NEXT_DEV_SERVER && outerWorkUnitStore.cacheSignal) {
const stagedRendering = outerWorkUnitStore.stagedRendering
// Capture the render stage at the start of this cache read, before the
// yield below. A streamed staged render advances its controller on its
// own schedule, independently of this read, so by the time the yield
// resolves the controller may have raced ahead to the Dynamic stage even
// though the read began in an earlier (prerender) stage.
const stageAtReadStart = stagedRendering?.currentStage
// If we're filling caches for a staged render, make sure that it takes // If we're filling caches for a staged render, make sure that it takes
// at least a task, so we'll always notice a cache miss between stages. // at least a task, so we'll always notice a cache miss between stages.
// //
// TODO(restart-on-cache-miss): This is suboptimal. Ideally we wouldn't // TODO(restart-on-cache-miss): This is suboptimal. Ideally microtasky
// need to restart for microtasky caches, but the current logic for // caches wouldn't register as a miss, but short-lived caches are only
// omitting short-lived caches only works correctly if we do a second // omitted correctly when read back in a separate render (now the
// render, so that's the best we can do until we refactor that. // background validation render, not a restart of the streamed
// response), so forcing the miss is the best we can do until that's
// refactored.
await new Promise((resolve) => setTimeout(resolve)) await new Promise((resolve) => setTimeout(resolve))
// Start a cache-fill timeout so a hanging `'use cache'` entry surfaces // Start a cache-fill timeout so a hanging `'use cache'` entry surfaces
// the same error in dev as during prerender. Cleared when // the same error in dev as during prerender. Cleared when
// pendingCacheResult settles. // pendingCacheResult settles.
// //
// Only skip the timeout when we're exactly in the Dynamic stage. That // Skip the timeout only when the read began in the Dynamic stage, which
// mirrors prerender, where caches guarded by e.g. `await connection()` // mirrors prerender: a cache guarded by e.g. `await connection()` is a
// aren't executed at all. We can't use `< RenderStage.Dynamic` here // legitimate dynamic hole and isn't executed there. We use the stage
// because `RenderStage.Abandoned` is numerically higher than Dynamic, // captured at read start, not the current one, because the staged render
// but semantically it means the initial prospective render was aborted // may have advanced past it during the yield above.
// while caches are still pending — the outer flow then awaits if (stageAtReadStart !== RenderStage.Dynamic) {
// `cacheSignal.cacheReady()`, so we need the timer to break a potential
// deadlock.
const stagedRendering = outerWorkUnitStore.stagedRendering
if (stagedRendering?.currentStage !== RenderStage.Dynamic) {
const devRenderAbortController = new AbortController() const devRenderAbortController = new AbortController()
const fillTimeoutMs = getUseCacheFillTimeoutMs( const fillTimeoutMs = getUseCacheFillTimeoutMs(
workStore, workStore,
...@@ -1269,9 +1281,32 @@ async function generateCacheEntryImpl( ...@@ -1269,9 +1281,32 @@ async function generateCacheEntryImpl(
onError(error) { onError(error) {
if ( if (
devRenderAbortController.signal.aborted && devRenderAbortController.signal.aborted &&
devRenderAbortController.signal.reason === error devRenderAbortController.signal.reason === error &&
error instanceof Error
) { ) {
return undefined // The abort reason is the same error stored as
// `workStore.invalidDynamicUsageError` (a fill timeout or
// deadlock). Register it under a digest and return that
// digest, so the error that surfaces on the consumer side of
// this Flight boundary carries it and the outer render's
// handler can recover *this* object via
// `reactServerErrorsByDigest`.
//
// We deliberately do not set `error.digest` here: whether the
// error actually surfaces (vs. being caught in userland) is
// the consumer's decision, so the "surfaced" mark is left to
// the outer handler.
const digest = createDigestWithErrorCode(
error,
stringHash(error.message + (error.stack || '')).toString()
)
workStore.reactServerErrorsByDigest.set(
digest,
error as DigestedError
)
return digest
} }
return handleError(error) return handleError(error)
...@@ -2133,9 +2168,11 @@ export async function cache( ...@@ -2133,9 +2168,11 @@ export async function cache(
// We delay the cache here so that it doesn't resolve in the static task -- // We delay the cache here so that it doesn't resolve in the static task --
// in a regular static prerender, it'd be a hanging promise, and we need to reflect that, // in a regular static prerender, it'd be a hanging promise, and we need to reflect that,
// so it has to resolve later. // so it has to resolve later.
// TODO(restart-on-cache-miss): Optimize this to avoid unnecessary restarts. // TODO(restart-on-cache-miss): Optimize away this phantom miss.
// We don't end the cache read here, so this will always appear as a cache miss in the static stage, // We don't end the cache read here, so it always appears as a
// and thus will cause a restart even if all caches are filled. // cache miss in the static stage even when all caches are
// filled, which now routes validation through the background
// warm render rather than restarting the streamed response.
const stagedRendering = workUnitStore.stagedRendering const stagedRendering = workUnitStore.stagedRendering
const stage = stagedRendering const stage = stagedRendering
...@@ -2188,9 +2225,11 @@ export async function cache( ...@@ -2188,9 +2225,11 @@ export async function cache(
// We delay the cache here so that it doesn't resolve in the runtime phase -- // We delay the cache here so that it doesn't resolve in the runtime phase --
// in a regular runtime prerender, it'd be a hanging promise, and we need to reflect that, // in a regular runtime prerender, it'd be a hanging promise, and we need to reflect that,
// so it has to resolve later. // so it has to resolve later.
// TODO(restart-on-cache-miss): Optimize this to avoid unnecessary restarts. // TODO(restart-on-cache-miss): Optimize away this phantom miss.
// We don't end the cache read here, so this will always appear as a cache miss in the runtime stage, // We don't end the cache read here, so it always appears as a
// and thus will cause a restart even if all caches are filled. // cache miss in the runtime stage even when all caches are
// filled, which now routes validation through the background
// warm render rather than restarting the streamed response.
await makeDevtoolsIOAwarePromise( await makeDevtoolsIOAwarePromise(
undefined, undefined,
workUnitStore, workUnitStore,
...@@ -2687,10 +2726,12 @@ export async function cache( ...@@ -2687,10 +2726,12 @@ export async function cache(
// static task -- in a regular static prerender, it'd be a // static task -- in a regular static prerender, it'd be a
// hanging promise, and we need to reflect that, so it has to // hanging promise, and we need to reflect that, so it has to
// resolve later. // resolve later.
// TODO(restart-on-cache-miss): Optimize this to avoid // TODO(restart-on-cache-miss): Optimize away this phantom miss.
// unnecessary restarts. We don't end the cache read here, so // We don't end the cache read here, so on a warm handler hit it
// this will always appear as a cache miss in the static stage, // still appears as a cache miss in the static stage even when
// and thus will cause a restart even if all caches are filled. // all caches are filled, which now routes validation through the
// background warm render rather than restarting the streamed
// response.
const stagedRendering = workUnitStore.stagedRendering const stagedRendering = workUnitStore.stagedRendering
const stage = stagedRendering const stage = stagedRendering
......
import { connection } from 'next/server'
import { Suspense } from 'react'
import { setTimeout } from 'timers/promises'
async function SlowCached() {
'use cache'
// A long-running cache fill. The dynamic sibling below must not wait for it.
await setTimeout(5000)
return <p id="cached">{new Date().toISOString()}</p>
}
async function Dynamic() {
await connection()
return <p id="dynamic">dynamic content</p>
}
export default function Page() {
return (
<main>
<Suspense fallback={<p id="cached-fallback">Loading cache...</p>}>
<SlowCached />
</Suspense>
<Suspense fallback={<p id="dynamic-fallback">Loading dynamic...</p>}>
<Dynamic />
</Suspense>
</main>
)
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<ul>
<li>
<Link href="/use-cache">/use-cache</Link>
</li>
<li>
<Link href="/runtime-prefetch">/runtime-prefetch</Link>
</li>
</ul>
)
}
import { connection } from 'next/server'
import { cookies } from 'next/headers'
import { Suspense } from 'react'
import { setTimeout } from 'timers/promises'
export const prefetch = 'allow-runtime'
async function RuntimePrefetchable() {
await cookies()
return <p id="runtime">runtime content</p>
}
async function Dynamic() {
await connection()
await setTimeout(1000)
return <p id="dynamic">dynamic content</p>
}
export default function Page() {
return (
<main>
<Suspense fallback={<p id="runtime-fallback">Loading runtime...</p>}>
<RuntimePrefetchable />
</Suspense>
<Suspense fallback={<p id="dynamic-fallback">Loading dynamic...</p>}>
<Dynamic />
</Suspense>
</main>
)
}
import { Suspense } from 'react'
import { setTimeout } from 'timers/promises'
async function Cached() {
'use cache'
await setTimeout(1000)
return <p id="cached">{new Date().toISOString()}</p>
}
function SyncIO() {
return <p id="sync">{Date()}</p>
}
export default function Page() {
return (
<main>
<Suspense fallback={<p id="loading">Loading...</p>}>
<Cached />
</Suspense>
<SyncIO />
</main>
)
}
import { Suspense } from 'react'
import { setTimeout } from 'timers/promises'
async function Cached() {
'use cache'
await setTimeout(1000)
return <p id="cached">{new Date().toISOString()}</p>
}
async function UncachedIO() {
await setTimeout(100)
return <p id="uncached">uncached</p>
}
export default function Page() {
return (
<main>
<Suspense fallback={<p id="loading">Loading...</p>}>
<Cached />
</Suspense>
<UncachedIO />
</main>
)
}
import { Suspense } from 'react'
import { setTimeout } from 'timers/promises'
async function Cached() {
'use cache'
await setTimeout(2000)
return <p>{new Date().toISOString()}</p>
}
export default async function Page() {
return (
<Suspense fallback={<p>Loading...</p>}>
<Cached />
</Suspense>
)
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('cache-components-dev-streaming', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should stream suspense boundaries while filling caches in the background', async () => {
const browser = await next.browser('/use-cache', {
waitHydration: false,
// do not wait for "load", we want to inspect the page as it streams in
waitUntil: 'commit',
})
// The loading boundary should be streamed immediately, without waiting for
// the cache to be filled.
expect(await browser.elementByCss('p').text()).toBe('Loading...')
// Eventually, the cache content should be streamed in.
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBeDateString()
})
// Now that the cache is filled, it should be served immediately on the next
// page load.
await browser.refresh()
expect(await browser.elementByCss('p').text()).toBeDateString()
})
it('streams dynamic content immediately while a sibling cache is still filling', async () => {
const browser = await next.browser('/dynamic-streaming', {
waitHydration: false,
// do not wait for "load", we want to inspect the page as it streams in
waitUntil: 'commit',
})
// The dynamic content (guarded by `await connection()`) must stream right
// away, in parallel with the sibling's long-running cache fill, rather than
// being withheld until the cache finishes filling.
//
// Use instant checks throughout: `elementByCss`/`elementById` also wait for
// the page "load" event, which (with `waitUntil: 'commit'`) only fires once
// the slow cache has filled — that would advance past the very window this
// test inspects.
await retry(async () => {
expect(await browser.elementByCssInstant('#dynamic').text()).toBe(
'dynamic content'
)
})
// The slow cache is still filling at this point, so its fallback is shown
// and its content hasn't streamed in yet.
expect(await browser.hasElementByCss('#cached-fallback')).toBe(true)
expect(await browser.hasElementByCss('#cached')).toBe(false)
// Eventually the cache fills and its content streams in.
await retry(async () => {
expect(
await browser.elementByCssInstant('#cached').text()
).toBeDateString()
}, 10000)
})
it('does not show a Suspense fallback for runtime-prefetchable content on a client navigation', async () => {
const browser = await next.browser('/')
// Record whether each Suspense fallback ever enters the DOM during the
// upcoming navigation. We inspect added nodes rather than the live DOM, so
// a fallback that's added and then quickly replaced is still caught.
await browser.eval(() => {
const seen = { runtime: false, dynamic: false }
;(window as any).__fallbacksSeen = seen
const check = (node: Node) => {
if (!(node instanceof Element)) {
return
}
if (
node.id === 'runtime-fallback' ||
node.querySelector('#runtime-fallback')
) {
seen.runtime = true
}
if (
node.id === 'dynamic-fallback' ||
node.querySelector('#dynamic-fallback')
) {
seen.dynamic = true
}
}
new MutationObserver((records) => {
records.forEach((record) => record.addedNodes.forEach(check))
}).observe(document.body, { childList: true, subtree: true })
})
await browser.elementByCss('a[href="/runtime-prefetch"]').click()
// Wait for the navigation to fully settle.
await retry(async () => {
expect(await browser.elementByCssInstant('#runtime').text()).toBe(
'runtime content'
)
expect(await browser.elementByCssInstant('#dynamic').text()).toBe(
'dynamic content'
)
})
// The runtime-prefetchable content was resolved before the response started
// streaming, so its fallback was never rendered; the dynamic content's
// fallback was.
expect(await browser.eval(() => (window as any).__fallbacksSeen)).toEqual({
runtime: false,
dynamic: true,
})
})
// The following are smoke tests that Cache Components validation still
// surfaces errors for both cold-cache renders (validated via a separate
// warm-cache render) and warm-cache renders (validated via the streamed
// render's own chunks). The full variety of validation errors is covered by
// the cache-components-errors test suite.
it('shows a validation redbox for uncached IO on cold and warm caches', async () => {
const browser = await next.browser('/uncached-io')
// Cold cache miss: validation runs against a separate warm-cache render.
await expect(browser).toDisplayCollapsedRedbox(`
{
"code": "E1290",
"description": "Next.js encountered uncached data during prerendering.",
"environmentLabel": "Server",
"label": "Blocking Route",
"source": "app/uncached-io/page.tsx (11:19) @ UncachedIO
> 11 | await setTimeout(100)
| ^",
"stack": [
"UncachedIO app/uncached-io/page.tsx (11:19)",
"Page app/uncached-io/page.tsx (21:7)",
],
}
`)
// The cache filled while streaming; capture the value to confirm the
// refresh is a hit.
const cachedDate = await browser.elementByCss('#cached').text()
expect(cachedDate).toBeDateString()
await browser.refresh()
// Warm cache hit: validation runs against the streamed render's own chunks,
// and the cached value is re-served rather than recomputed.
await expect(browser).toDisplayCollapsedRedbox(`
{
"code": "E1290",
"description": "Next.js encountered uncached data during prerendering.",
"environmentLabel": "Server",
"label": "Blocking Route",
"source": "app/uncached-io/page.tsx (11:19) @ UncachedIO
> 11 | await setTimeout(100)
| ^",
"stack": [
"UncachedIO app/uncached-io/page.tsx (11:19)",
"Page app/uncached-io/page.tsx (21:7)",
],
}
`)
expect(await browser.elementByCss('#cached').text()).toBe(cachedDate)
})
it('shows a validation redbox for sync IO on cold and warm caches', async () => {
const browser = await next.browser('/sync-io')
// Cold cache miss: validation runs against a separate warm-cache render.
await expect(browser).toDisplayCollapsedRedbox(`
{
"code": "E1295",
"description": "Next.js encountered the unstable value Date() while prerendering.",
"environmentLabel": "Server",
"label": "Blocking Route",
"source": "app/sync-io/page.tsx (11:24) @ SyncIO
> 11 | return <p id="sync">{Date()}</p>
| ^",
"stack": [
"SyncIO app/sync-io/page.tsx (11:24)",
"Page app/sync-io/page.tsx (20:7)",
],
}
`)
// The cache filled while streaming; capture the value to confirm the
// refresh is a hit.
const cachedDate = await browser.elementByCss('#cached').text()
expect(cachedDate).toBeDateString()
await browser.refresh()
// Warm cache hit: validation runs against the streamed render's own chunks,
// and the cached value is re-served rather than recomputed.
await expect(browser).toDisplayCollapsedRedbox(`
{
"code": "E1295",
"description": "Next.js encountered the unstable value Date() while prerendering.",
"environmentLabel": "Server",
"label": "Blocking Route",
"source": "app/sync-io/page.tsx (11:24) @ SyncIO
> 11 | return <p id="sync">{Date()}</p>
| ^",
"stack": [
"SyncIO app/sync-io/page.tsx (11:24)",
"Page app/sync-io/page.tsx (20:7)",
],
}
`)
expect(await browser.elementByCss('#cached').text()).toBe(cachedDate)
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
...@@ -74,15 +74,18 @@ describe.each([ ...@@ -74,15 +74,18 @@ describe.each([
) { ) {
const browser = await next.browser(path) const browser = await next.browser(path)
// Initial load. // The initial load fills caches while streaming, so cached content
await retry(() => assertLogs(browser)) // resolves in a later phase than it will once the caches are warm. That's
// an accepted, non-representative tradeoff of the streaming dev render,
// so we don't assert the logs here — this load just fills the caches.
// We should not see any errors related to the aborted render. // We should not see any errors related to the aborted render.
expect(next.cliOutput).not.toContain( expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted' 'AbortError: This operation was aborted'
) )
// After another load (with warm caches) the logs should be the same. // After a warm reload the caches are filled, so the logs resolve in the
// correct phase.
await browser.loadPage(next.url + path) // clears old logs await browser.loadPage(next.url + path) // clears old logs
await retry(() => assertLogs(browser)) await retry(() => assertLogs(browser))
...@@ -103,11 +106,18 @@ describe.each([ ...@@ -103,11 +106,18 @@ describe.each([
return return
} }
// After a revalidation the subsequent warmup render must discard stale // After a revalidation the subsequent render must discard the stale cache
// cache entries. // entries. This should not affect the environment labels once the caches
// This should not affect the environment labels. // are warm again.
await revalidatePath(path) await revalidatePath(path)
// The first load after revalidation is a cold cache-miss request that we
// stream, so its stages aren't representative; it just refills the
// caches.
await browser.loadPage(next.url + path)
// After a warm reload the caches are filled, so the logs resolve in the
// correct phase.
await browser.loadPage(next.url + path) // clears old logs await browser.loadPage(next.url + path) // clears old logs
await retry(() => assertLogs(browser)) await retry(() => assertLogs(browser))
...@@ -123,16 +133,27 @@ describe.each([ ...@@ -123,16 +133,27 @@ describe.each([
) { ) {
const browser = await next.browser('/') const browser = await next.browser('/')
// Initial nav (first time loading the page) // The initial nav fills caches while streaming, so cached content
// resolves in a later phase than it will once the caches are warm. That's
// an accepted, non-representative tradeoff of the streaming dev render,
// so we don't assert the logs here — this nav just fills the caches.
const initialNavOutputIndex = next.cliOutput.length
await browser.elementByCss(`a[href="${path}"]`).click() await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => assertLogs(browser)) // Wait for the nav's request to finish before reloading, to ensure all
// caches were filled.
await retry(() => {
expect(next.cliOutput.slice(initialNavOutputIndex)).toContain(
`GET ${path} 200`
)
}, 10_000)
// We should not see any errors related to the aborted render. // We should not see any errors related to the aborted render.
expect(next.cliOutput).not.toContain( expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted' 'AbortError: This operation was aborted'
) )
// Reload, and perform another nav (with warm caches). the logs should be the same. // After a warm reload + nav the caches are filled, so the logs resolve in
// the correct phase.
await browser.loadPage(next.url + '/') // clears old logs await browser.loadPage(next.url + '/') // clears old logs
await browser.elementByCss(`a[href="${path}"]`).click() await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => assertLogs(browser)) await retry(() => assertLogs(browser))
...@@ -154,11 +175,25 @@ describe.each([ ...@@ -154,11 +175,25 @@ describe.each([
return return
} }
// After a revalidation the subsequent warmup render must discard stale // After a revalidation the subsequent render must discard the stale cache
// cache entries. // entries. This should not affect the environment labels once the caches
// This should not affect the environment labels. // are warm again.
await revalidatePath(path) await revalidatePath(path)
// The first navigation after revalidation is a cold cache-miss request
// that we stream, so its stages aren't representative; it just refills
// the caches. Wait for its request to finish before navigating again.
await browser.loadPage(next.url + '/')
const revalidatedNavOutputIndex = next.cliOutput.length
await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => {
expect(next.cliOutput.slice(revalidatedNavOutputIndex)).toContain(
`GET ${path} 200`
)
}, 10_000)
// After a warm reload + nav the caches are filled, so the logs resolve in
// the correct phase.
await browser.loadPage(next.url + '/') // clears old logs await browser.loadPage(next.url + '/') // clears old logs
await browser.elementByCss(`a[href="${path}"]`).click() await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => assertLogs(browser)) await retry(() => assertLogs(browser))
...@@ -207,7 +242,12 @@ describe.each([ ...@@ -207,7 +242,12 @@ describe.each([
} }
}) })
it('cached data + private cache', async () => { // TODO: Skipped until private caches are persisted in dev. They aren't
// persisted yet, so a warm reload re-runs them as a cache miss that
// resolves in the dynamic stage rather than the runtime stage.
// Re-enable once private caches are persisted, after which a warm
// reload is a hit that resolves in the runtime stage.
it.skip('cached data + private cache', async () => {
const path = '/private-cache' const path = '/private-cache'
const assertLogs = async (browser: Playwright) => { const assertLogs = async (browser: Playwright) => {
...@@ -236,7 +276,12 @@ describe.each([ ...@@ -236,7 +276,12 @@ describe.each([
} }
}) })
it('cached data + short-lived cached data', async () => { // TODO: Skipped until the cache read is ended for short-lived handler
// hits. Today a warm reload defers the value without ending the read,
// so it triggers as a phantom cache miss whose phase the streaming dev
// render doesn't pin down. Re-enable once the read is ended, after
// which a warm reload should resolve in the runtime stage.
it.skip('cached data + short-lived cached data', async () => {
const path = '/short-lived-cache' const path = '/short-lived-cache'
const assertLogs = async (browser: Playwright) => { const assertLogs = async (browser: Playwright) => {
......
...@@ -13,11 +13,22 @@ describe('cache-indicator', () => { ...@@ -13,11 +13,22 @@ describe('cache-indicator', () => {
const badge = await browser.elementByCss('[data-next-badge]') const badge = await browser.elementByCss('[data-next-badge]')
const cacheStatus = await badge.getAttribute('data-status') const cacheStatus = await badge.getAttribute('data-status')
expect(cacheStatus).toBe('none')
// If compilation is still in progress (e.g. on a slow CI machine), the
// cache status might briefly be "compiling" before becoming "none", so we
// allow both here, before eventually asserting that it becomes "none".
expect(cacheStatus).toBeOneOf(['none', 'compiling'])
await retry(async () => {
const badge = await browser.elementByCss('[data-next-badge]')
const cacheStatus = await badge.getAttribute('data-status')
expect(cacheStatus).toBe('none')
})
}) })
if (enableCacheComponents) { if (enableCacheComponents) {
it('renders the cache warming indicator when navigating to a page that needs to warm the cache', async () => { // TODO: Replace this with tests that assert a cache miss indicator is shown
// instead when this is implemented.
it.skip('renders the cache warming indicator when navigating to a page that needs to warm the cache', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
// navigate to the navigation page // navigate to the navigation page
......
...@@ -60,7 +60,7 @@ describe('use-cache-errors', () => { ...@@ -60,7 +60,7 @@ describe('use-cache-errors', () => {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "kaputt!", "description": "kaputt!",
"environmentLabel": "Prerender", "environmentLabel": "Cache",
"label": "Runtime Error", "label": "Runtime Error",
"source": "app/anonymous/page.tsx (4:11) @ <anonymous> "source": "app/anonymous/page.tsx (4:11) @ <anonymous>
> 4 | throw new Error('kaputt!') > 4 | throw new Error('kaputt!')
...@@ -74,7 +74,7 @@ describe('use-cache-errors', () => { ...@@ -74,7 +74,7 @@ describe('use-cache-errors', () => {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "kaputt!", "description": "kaputt!",
"environmentLabel": "Prerender", "environmentLabel": "Cache",
"label": "Runtime Error", "label": "Runtime Error",
"source": "app/anonymous/page.tsx (4:11) @ eval "source": "app/anonymous/page.tsx (4:11) @ eval
> 4 | throw new Error('kaputt!') > 4 | throw new Error('kaputt!')
......
...@@ -2712,12 +2712,12 @@ describe('Cache Components Errors', () => { ...@@ -2712,12 +2712,12 @@ describe('Cache Components Errors', () => {
it('should show a redbox error', async () => { it('should show a redbox error', async () => {
const browser = await next.browser(pathname) const browser = await next.browser(pathname)
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E831", "code": "E831",
"description": "Route /use-cache-cookies used \`cookies()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`cookies()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-cookies used \`cookies()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`cookies()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-cookies/page.tsx (22:18) @ CookiesReadingComponent "source": "app/use-cache-cookies/page.tsx (22:18) @ CookiesReadingComponent
> 22 | await cookies() > 22 | await cookies()
| ^", | ^",
...@@ -2821,12 +2821,12 @@ describe('Cache Components Errors', () => { ...@@ -2821,12 +2821,12 @@ describe('Cache Components Errors', () => {
it('should show a redbox error', async () => { it('should show a redbox error', async () => {
const browser = await next.browser(pathname) const browser = await next.browser(pathname)
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E829", "code": "E829",
"description": "Route /use-cache-draft-mode used "draftMode().enable()" inside "use cache". The enabled status of \`draftMode()\` can be read in caches but you must not enable or disable \`draftMode()\` inside a cache. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-draft-mode used "draftMode().enable()" inside "use cache". The enabled status of \`draftMode()\` can be read in caches but you must not enable or disable \`draftMode()\` inside a cache. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-draft-mode/page.tsx (20:26) @ DraftModeEnablingComponent "source": "app/use-cache-draft-mode/page.tsx (20:26) @ DraftModeEnablingComponent
> 20 | ;(await draftMode()).enable() > 20 | ;(await draftMode()).enable()
| ^", | ^",
...@@ -2929,12 +2929,12 @@ describe('Cache Components Errors', () => { ...@@ -2929,12 +2929,12 @@ describe('Cache Components Errors', () => {
it('should show a redbox error', async () => { it('should show a redbox error', async () => {
const browser = await next.browser(pathname) const browser = await next.browser(pathname)
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E833", "code": "E833",
"description": "Route /use-cache-headers used \`headers()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`headers()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-headers used \`headers()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`headers()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-headers/page.tsx (21:18) @ HeadersReadingComponent "source": "app/use-cache-headers/page.tsx (21:18) @ HeadersReadingComponent
> 21 | await headers() > 21 | await headers()
| ^", | ^",
...@@ -3036,12 +3036,12 @@ describe('Cache Components Errors', () => { ...@@ -3036,12 +3036,12 @@ describe('Cache Components Errors', () => {
it('should show a redbox error', async () => { it('should show a redbox error', async () => {
const browser = await next.browser('/use-cache-connection') const browser = await next.browser('/use-cache-connection')
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E841", "code": "E841",
"description": "Route /use-cache-connection used \`connection()\` inside "use cache". The \`connection()\` function is used to indicate the subsequent code must only run when there is an actual request, but caches must be able to be produced before a request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-connection used \`connection()\` inside "use cache". The \`connection()\` function is used to indicate the subsequent code must only run when there is an actual request, but caches must be able to be produced before a request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-connection/page.tsx (21:21) @ ConnectionCallingComponent "source": "app/use-cache-connection/page.tsx (21:21) @ ConnectionCallingComponent
> 21 | await connection() > 21 | await connection()
| ^", | ^",
...@@ -4229,12 +4229,12 @@ Ways to fix this: ...@@ -4229,12 +4229,12 @@ Ways to fix this:
'/use-cache-cookies-third-party' '/use-cache-cookies-third-party'
) )
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E831", "code": "E831",
"description": "Route /use-cache-cookies-third-party used \`cookies()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`cookies()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-cookies-third-party used \`cookies()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`cookies()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-cookies-third-party/page.tsx (10:7) @ Page "source": "app/use-cache-cookies-third-party/page.tsx (10:7) @ Page
> 10 | <CachedCookiesReader /> > 10 | <CachedCookiesReader />
| ^", | ^",
...@@ -4328,12 +4328,12 @@ Ways to fix this: ...@@ -4328,12 +4328,12 @@ Ways to fix this:
'/use-cache-draft-mode-third-party' '/use-cache-draft-mode-third-party'
) )
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E829", "code": "E829",
"description": "Route /use-cache-draft-mode-third-party used "draftMode().enable()" inside "use cache". The enabled status of \`draftMode()\` can be read in caches but you must not enable or disable \`draftMode()\` inside a cache. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-draft-mode-third-party used "draftMode().enable()" inside "use cache". The enabled status of \`draftMode()\` can be read in caches but you must not enable or disable \`draftMode()\` inside a cache. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-draft-mode-third-party/page.tsx (10:7) @ Page "source": "app/use-cache-draft-mode-third-party/page.tsx (10:7) @ Page
> 10 | <CachedDraftModeEnabler /> > 10 | <CachedDraftModeEnabler />
| ^", | ^",
...@@ -4426,12 +4426,12 @@ Ways to fix this: ...@@ -4426,12 +4426,12 @@ Ways to fix this:
'/use-cache-headers-third-party' '/use-cache-headers-third-party'
) )
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E833", "code": "E833",
"description": "Route /use-cache-headers-third-party used \`headers()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`headers()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-headers-third-party used \`headers()\` inside "use cache". Accessing Dynamic data sources inside a cache scope is not supported. If you need this data inside a cached function use \`headers()\` outside of the cached function and pass the required dynamic data in as an argument. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-headers-third-party/page.tsx (10:7) @ Page "source": "app/use-cache-headers-third-party/page.tsx (10:7) @ Page
> 10 | <CachedHeadersReader /> > 10 | <CachedHeadersReader />
| ^", | ^",
...@@ -4525,12 +4525,12 @@ Ways to fix this: ...@@ -4525,12 +4525,12 @@ Ways to fix this:
'/use-cache-connection-third-party' '/use-cache-connection-third-party'
) )
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"code": "E841", "code": "E841",
"description": "Route /use-cache-connection-third-party used \`connection()\` inside "use cache". The \`connection()\` function is used to indicate the subsequent code must only run when there is an actual request, but caches must be able to be produced before a request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache", "description": "Route /use-cache-connection-third-party used \`connection()\` inside "use cache". The \`connection()\` function is used to indicate the subsequent code must only run when there is an actual request, but caches must be able to be produced before a request, so this function is not allowed in this scope. See more info here: https://nextjs.org/docs/messages/next-request-in-use-cache",
"environmentLabel": "Prerender", "environmentLabel": "Server",
"label": "Runtime Error", "label": "Console Error",
"source": "app/use-cache-connection-third-party/page.tsx (10:7) @ Page "source": "app/use-cache-connection-third-party/page.tsx (10:7) @ Page
> 10 | <CachedConnectionCaller /> > 10 | <CachedConnectionCaller />
| ^", | ^",
...@@ -4760,7 +4760,7 @@ Ways to fix this: ...@@ -4760,7 +4760,7 @@ Ways to fix this:
{ {
"code": "E1001", "code": "E1001",
"description": ""use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".", "description": ""use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".",
"environmentLabel": "Prerender", "environmentLabel": "Cache",
"label": "Runtime Error", "label": "Runtime Error",
"source": "app/use-cache-private-in-use-cache/page.tsx (15:1) @ Private "source": "app/use-cache-private-in-use-cache/page.tsx (15:1) @ Private
> 15 | async function Private() { > 15 | async function Private() {
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
describe('ppr-root-param-fallback', () => { describe('ppr-root-param-fallback', () => {
const { next } = nextTestSetup({ const { next, isNextDev } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
...@@ -15,7 +15,14 @@ describe('ppr-root-param-fallback', () => { ...@@ -15,7 +15,14 @@ describe('ppr-root-param-fallback', () => {
for (const locale of ['en', 'fr']) { for (const locale of ['en', 'fr']) {
// next.render$ doesn't stream, so we get just the shell content // next.render$ doesn't stream, so we get just the shell content
const $ = await next.render$(`/${locale}/blog/new-post`) let $ = await next.render$(`/${locale}/blog/new-post`)
// In dev the initial request fills the caches while streaming the
// response. So we need to do a second page load after caches were filled
// to get the production-like response.
if (isNextDev) {
$ = await next.render$(`/${locale}/blog/new-post`)
}
// The shell should have the locale-header with cached content, // The shell should have the locale-header with cached content,
// NOT the locale-loading Suspense fallback // NOT the locale-loading Suspense fallback
......
...@@ -40,7 +40,7 @@ describe('use-cache-configured-timeout', () => { ...@@ -40,7 +40,7 @@ describe('use-cache-configured-timeout', () => {
{ {
"code": "E236", "code": "E236",
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".", "description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": "Prerender", "environmentLabel": "Cache",
"label": "Runtime Error", "label": "Runtime Error",
"source": "app/above-dev-timeout/page.tsx (4:1) @ getCachedData "source": "app/above-dev-timeout/page.tsx (4:1) @ getCachedData
> 4 | async function getCachedData(): Promise<string> { > 4 | async function getCachedData(): Promise<string> {
......
...@@ -8,7 +8,15 @@ const expectedTimeoutErrorMessage = ...@@ -8,7 +8,15 @@ const expectedTimeoutErrorMessage =
const expectedDeadlockMessage = const expectedDeadlockMessage =
'Filling a "use cache" entry appears to be stuck on shared state from the outer render scope. The same function completed when run in isolation, which usually means a module-scoped value (for example a top-level Map used to dedupe fetches) is joining a promise created outside the cache. "use cache" already dedupes calls with the same arguments — within a request and across requests on the same server instance — so the surrounding dedupe layer is both unnecessary and the likely cause. Remove it and rely on "use cache" alone for deduping.' 'Filling a "use cache" entry appears to be stuck on shared state from the outer render scope. The same function completed when run in isolation, which usually means a module-scoped value (for example a top-level Map used to dedupe fetches) is joining a promise created outside the cache. "use cache" already dedupes calls with the same arguments — within a request and across requests on the same server instance — so the surrounding dedupe layer is both unnecessary and the likely cause. Remove it and rely on "use cache" alone for deduping.'
describe('use-cache-deadlock-probe', () => { // TODO: The `'use cache'` deadlock probe is disabled in dev for now. The
// streaming dev render now advances to the dynamic stage without waiting for
// cache fills, so dynamic content streams to the browser immediately instead of
// being withheld until the slowest cache fills. A fill that depends on
// dynamic-stage IO is therefore unblocked by reaching the dynamic stage rather
// than detected as a deadlock. Revisit by surfacing these deadlocks at build
// time via `next build --debug-prerender`, then re-enable (and retarget) this
// suite.
describe.skip('use-cache-deadlock-probe', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
...@@ -228,12 +236,12 @@ describe('use-cache-deadlock-probe', () => { ...@@ -228,12 +236,12 @@ describe('use-cache-deadlock-probe', () => {
waitUntil: 'commit', waitUntil: 'commit',
}) })
await expect(browser).toDisplayCollapsedRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"code": "E1181", "code": "E1181",
"description": "Filling a "use cache" entry appears to be stuck on shared state from the outer render scope. The same function completed when run in isolation, which usually means a module-scoped value (for example a top-level Map used to dedupe fetches) is joining a promise created outside the cache. "use cache" already dedupes calls with the same arguments — within a request and across requests on the same server instance — so the surrounding dedupe layer is both unnecessary and the likely cause. Remove it and rely on "use cache" alone for deduping.", "description": "Filling a "use cache" entry appears to be stuck on shared state from the outer render scope. The same function completed when run in isolation, which usually means a module-scoped value (for example a top-level Map used to dedupe fetches) is joining a promise created outside the cache. "use cache" already dedupes calls with the same arguments — within a request and across requests on the same server instance — so the surrounding dedupe layer is both unnecessary and the likely cause. Remove it and rely on "use cache" alone for deduping.",
"environmentLabel": "Server", "environmentLabel": "Cache",
"label": "Console Error", "label": "Runtime Error",
"source": "app/recovery-stuck/page.tsx (24:1) @ getCachedData "source": "app/recovery-stuck/page.tsx (24:1) @ getCachedData
> 24 | async function getCachedData() { > 24 | async function getCachedData() {
| ^", | ^",
......
...@@ -153,9 +153,7 @@ describe('use-cache-hanging-inputs', () => { ...@@ -153,9 +153,7 @@ describe('use-cache-hanging-inputs', () => {
expect({ count, title, description }).toEqual({ expect({ count, title, description }).toEqual({
count: 1, count: 1,
// TODO(restart-on-cache-miss): fix environment labelling title: 'Runtime Error\nCache',
// title: 'Runtime Error\nCache',
title: 'Runtime Error\nPrerender',
description: 'kaputt!', description: 'kaputt!',
}) })
}) })
......
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { Suspense } from 'react' import { Suspense } from 'react'
import { getData, preload } from '../shared'
const sharedUrl =
'https://next-data-api-endpoint.vercel.app/api/random?page=runtime'
async function getCachedData(): Promise<string> { async function getCachedData(): Promise<string> {
'use cache' 'use cache'
// Joins the outer fetch via the module-scoped dedupe map (see note in // A cache fill that never completes, exceeding the configured
// ../shared.ts). // `useCacheTimeout`, so the fill times out.
return getData(sharedUrl).then((res) => res.text()) await new Promise<void>(() => {})
return 'data'
} }
async function Cached() { async function Cached() {
...@@ -28,11 +26,6 @@ async function Cached() { ...@@ -28,11 +26,6 @@ async function Cached() {
async function Runtime() { async function Runtime() {
await cookies() await cookies()
// Simulate another part of the tree (e.g. a sibling component or a shared
// data loader) kicking off the same fetch in outer scope before `Cached`
// runs.
preload(sharedUrl)
return <Cached /> return <Cached />
} }
......
// This module simulates a realistic migration hazard. The documented "preload"
// pattern (see the "Preloading data" section of the
// caching-without-cache-components guide in the Next.js docs) wraps the loader
// in `React.cache`, so its store is tied to the ALS snapshot — `'use cache'`
// bodies run in a clean snapshot and miss, re-executing the loader in cache
// scope. That path is safe during migration.
//
// What's modeled here is the common-in-the-wild variant that uses a
// module-scoped `Map` instead of `React.cache`. The map lives above ALS, so a
// `'use cache'` body joining it reuses the outer-scope promise (request scope
// or prerender scope) — a promise that was never meant to resolve for the
// cache:
// - During prerendering, an uncached fetch in the outer scope returns a
// hanging promise that intentionally never resolves.
// - In dev, the outer fetch is parked on the Dynamic stage, which can't
// advance until the cache fills. The cache awaits the outer fetch via the
// shared loader, so neither can make progress.
// In both cases, the cache never fills and times out.
const pendingFetches = new Map<string, Promise<Response>>()
export function getData(url: string): Promise<Response> {
let promise = pendingFetches.get(url)
if (!promise) {
promise = fetch(url)
pendingFetches.set(url, promise)
promise.then(
() => pendingFetches.delete(url),
() => pendingFetches.delete(url)
)
}
return promise
}
// "Preload" primes the shared loader without awaiting so later callers reuse
// the stored promise.
export function preload(url: string): void {
void getData(url)
}
import { getData, preload } from '../shared'
const sharedUrl =
'https://next-data-api-endpoint.vercel.app/api/random?page=static'
async function getCachedData(): Promise<string> { async function getCachedData(): Promise<string> {
'use cache' 'use cache'
// Joins the outer fetch via the module-scoped dedupe map (see note in // A cache fill that never completes, exceeding the configured
// ../shared.ts). // `useCacheTimeout`, so the fill times out.
return getData(sharedUrl).then((res) => res.text()) await new Promise<void>(() => {})
return 'data'
} }
async function Cached() { async function Cached() {
...@@ -24,10 +21,5 @@ async function Cached() { ...@@ -24,10 +21,5 @@ async function Cached() {
} }
export default function Page() { export default function Page() {
// Simulate another part of the tree (e.g. a sibling component or a shared
// data loader) kicking off the same fetch in outer scope before `Cached`
// runs.
preload(sharedUrl)
return <Cached /> return <Cached />
} }
...@@ -28,13 +28,13 @@ describe('use-cache-hanging', () => { ...@@ -28,13 +28,13 @@ describe('use-cache-hanging', () => {
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".", "description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": "Server", "environmentLabel": "Server",
"label": "Console Error", "label": "Console Error",
"source": "app/static/page.tsx (6:1) @ getCachedData "source": "app/static/page.tsx (1:1) @ getCachedData
> 6 | async function getCachedData(): Promise<string> { > 1 | async function getCachedData(): Promise<string> {
| ^", | ^",
"stack": [ "stack": [
"getCachedData app/static/page.tsx (6:1)", "getCachedData app/static/page.tsx (1:1)",
"Cached app/static/page.tsx (18:24)", "Cached app/static/page.tsx (15:24)",
"Page app/static/page.tsx (32:10)", "Page app/static/page.tsx (24:10)",
], ],
} }
`) `)
...@@ -42,7 +42,7 @@ describe('use-cache-hanging', () => { ...@@ -42,7 +42,7 @@ describe('use-cache-hanging', () => {
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage} expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage}
at getCachedData (app/static/page.tsx:6:1)`) at getCachedData (app/static/page.tsx:1:1)`)
}) })
}) })
...@@ -57,13 +57,13 @@ describe('use-cache-hanging', () => { ...@@ -57,13 +57,13 @@ describe('use-cache-hanging', () => {
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".", "description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": "Server", "environmentLabel": "Server",
"label": "Console Error", "label": "Console Error",
"source": "app/runtime/page.tsx (8:1) @ getCachedData "source": "app/runtime/page.tsx (4:1) @ getCachedData
> 8 | async function getCachedData(): Promise<string> { > 4 | async function getCachedData(): Promise<string> {
| ^", | ^",
"stack": [ "stack": [
"getCachedData app/runtime/page.tsx (8:1)", "getCachedData app/runtime/page.tsx (4:1)",
"Cached app/runtime/page.tsx (20:24)", "Cached app/runtime/page.tsx (18:24)",
"Page app/runtime/page.tsx (42:7)", "Page app/runtime/page.tsx (35:7)",
], ],
} }
`) `)
...@@ -71,7 +71,7 @@ describe('use-cache-hanging', () => { ...@@ -71,7 +71,7 @@ describe('use-cache-hanging', () => {
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage} expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage}
at getCachedData (app/runtime/page.tsx:8:1)`) at getCachedData (app/runtime/page.tsx:4:1)`)
}) })
}) })
...@@ -86,7 +86,7 @@ describe('use-cache-hanging', () => { ...@@ -86,7 +86,7 @@ describe('use-cache-hanging', () => {
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage} expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage}
at getCachedData (app/static/page.tsx:6:1)`) at getCachedData (app/static/page.tsx:1:1)`)
}, 20_000) }, 20_000)
await expect(browser).toDisplayCollapsedRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
...@@ -95,13 +95,13 @@ describe('use-cache-hanging', () => { ...@@ -95,13 +95,13 @@ describe('use-cache-hanging', () => {
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".", "description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": "Server", "environmentLabel": "Server",
"label": "Console Error", "label": "Console Error",
"source": "app/static/page.tsx (6:1) @ getCachedData "source": "app/static/page.tsx (1:1) @ getCachedData
> 6 | async function getCachedData(): Promise<string> { > 1 | async function getCachedData(): Promise<string> {
| ^", | ^",
"stack": [ "stack": [
"getCachedData app/static/page.tsx (6:1)", "getCachedData app/static/page.tsx (1:1)",
"Cached app/static/page.tsx (18:24)", "Cached app/static/page.tsx (15:24)",
"Page app/static/page.tsx (32:10)", "Page app/static/page.tsx (24:10)",
], ],
} }
`) `)
...@@ -119,7 +119,7 @@ describe('use-cache-hanging', () => { ...@@ -119,7 +119,7 @@ describe('use-cache-hanging', () => {
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage} expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage}
at getCachedData (app/runtime/page.tsx:8:1)`) at getCachedData (app/runtime/page.tsx:4:1)`)
}, 20_000) }, 20_000)
await expect(browser).toDisplayCollapsedRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
...@@ -128,13 +128,13 @@ describe('use-cache-hanging', () => { ...@@ -128,13 +128,13 @@ describe('use-cache-hanging', () => {
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".", "description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": "Server", "environmentLabel": "Server",
"label": "Console Error", "label": "Console Error",
"source": "app/runtime/page.tsx (8:1) @ getCachedData "source": "app/runtime/page.tsx (4:1) @ getCachedData
> 8 | async function getCachedData(): Promise<string> { > 4 | async function getCachedData(): Promise<string> {
| ^", | ^",
"stack": [ "stack": [
"getCachedData app/runtime/page.tsx (8:1)", "getCachedData app/runtime/page.tsx (4:1)",
"Cached app/runtime/page.tsx (20:24)", "Cached app/runtime/page.tsx (18:24)",
"Page app/runtime/page.tsx (42:7)", "Page app/runtime/page.tsx (35:7)",
], ],
} }
`) `)
...@@ -167,7 +167,7 @@ describe('use-cache-hanging', () => { ...@@ -167,7 +167,7 @@ describe('use-cache-hanging', () => {
if (isTurbopack) { if (isTurbopack) {
expect(next.cliOutput) expect(next.cliOutput)
.toContain(`Error: ${expectedTimeoutErrorMessage} .toContain(`Error: ${expectedTimeoutErrorMessage}
at <unknown> (app/static/page.tsx:6:1)`) at <unknown> (app/static/page.tsx:1:1)`)
} else { } else {
// Webpack production builds don't have source maps by default. // Webpack production builds don't have source maps by default.
expect(next.cliOutput).toContain(expectedTimeoutErrorMessage) expect(next.cliOutput).toContain(expectedTimeoutErrorMessage)
......
...@@ -1186,6 +1186,13 @@ describe('use-cache', () => { ...@@ -1186,6 +1186,13 @@ describe('use-cache', () => {
if (withCacheComponents) { if (withCacheComponents) {
it('can resume a cached generateMetadata function', async () => { it('can resume a cached generateMetadata function', async () => {
// In dev the initial request fills the caches while streaming the
// response. The second request will have filled caches and server a
// prod-like shell.
if (isNextDev) {
await next.fetch('/generate-metadata-resume/nested')
}
// First load the page with JavaScript disabled, to ensure that the // First load the page with JavaScript disabled, to ensure that the
// generateMetadata result was included in the prerendered shell. // generateMetadata result was included in the prerendered shell.
let browser = await next.browser('/generate-metadata-resume/nested', { let browser = await next.browser('/generate-metadata-resume/nested', {
......
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