Unverified Commit 5d7c41a4 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[Cache Components] Ensure cache misses always cause a restart in dev (#86583)

This PR works around a problem in the restart-on-cache-miss rendering
flow where short-lived caches wouldn't be considered dynamic if they
managed to fill microtaskily.

The problem is that:

1. If a cache fills microtaskily, it won't register as a cache miss, so
we won't restart the render
2. The logic for omitting short-lived caches only runs for cache hits
(because it expects to run in the final render), so it won't be applied
to a cache we just filled

The fix implemented here isn't ideal, but fixes the problem -- we just
make it so that all caches take at least a task to fill, and thus all
cache misses will be "noticed" and cause a restart.

This sacrifices the nice property we had before where a microtasky cache
miss wouldn't trigger a restart, but we can live with that for now for
the sake of correctness.
parent 4a8eeab2
...@@ -691,9 +691,23 @@ async function generateCacheEntryImpl( ...@@ -691,9 +691,23 @@ async function generateCacheEntryImpl(
stream = prelude stream = prelude
} }
break break
case 'request':
// 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.
//
// TODO(restart-on-cache-miss): This is suboptimal.
// Ideally we wouldn't need to restart for microtasky caches,
// but the current logic for omitting short-lived caches only works correctly
// if we do a second render, so that's the best we can do until we refactor that.
if (
process.env.NODE_ENV === 'development' &&
outerWorkUnitStore.cacheSignal
) {
await new Promise((resolve) => setTimeout(resolve))
}
// fallthrough
case 'prerender-ppr': case 'prerender-ppr':
case 'prerender-legacy': case 'prerender-legacy':
case 'request':
case 'cache': case 'cache':
case 'private-cache': case 'private-cache':
case 'unstable-cache': case 'unstable-cache':
......
import { cacheLife } from 'next/cache'
export default async function Page() {
'use cache: remote'
cacheLife({ expire: 299 }) // 1 second below the threshold of 5 minutes
// This cache can produce an entry microtaskily.
// We explicitly test it separately from "slow" caches,
// because of a dev bug where "fast" caches like this didn't register as a cache miss
// and thus sidestepped the usual logic for omitting short-lived caches.
return (
<>
<p>
This page is cached with a low expire time. Such a short-lived cache is
excluded from prerenders, and creates a dynamic hole. Without a parent
suspense boundary, this will cause an error during prerendering.
</p>
</>
)
}
...@@ -5,6 +5,9 @@ export default async function Page() { ...@@ -5,6 +5,9 @@ export default async function Page() {
cacheLife({ expire: 299 }) // 1 second below the threshold of 5 minutes cacheLife({ expire: 299 }) // 1 second below the threshold of 5 minutes
// This cache takes >1 task to fill, so it'll always show up as a cache miss in dev
await new Promise((resolve) => setTimeout(resolve, 5))
return ( return (
<> <>
<p> <p>
......
import { cacheLife } from 'next/cache'
export default async function Page() {
'use cache: remote'
cacheLife({ revalidate: 0 })
// This cache can produce an entry microtaskily.
// We explicitly test it separately from "slow" caches,
// because of a dev bug where "fast" caches like this didn't register as a cache miss
// and thus sidestepped the usual logic for omitting short-lived caches.
return (
<>
<p>
This page is cached with a zero revalidate time. Such a short-lived
cache is excluded from prerenders, and creates a dynamic hole. Without a
parent suspense boundary, this will cause an error during prerendering.
</p>
</>
)
}
...@@ -5,6 +5,9 @@ export default async function Page() { ...@@ -5,6 +5,9 @@ export default async function Page() {
cacheLife({ revalidate: 0 }) cacheLife({ revalidate: 0 })
// This cache takes >1 task to fill, so it'll always show up as a cache miss in dev
await new Promise((resolve) => setTimeout(resolve, 5))
return ( return (
<> <>
<p> <p>
......
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