Fix blocking revalidation for `fetch` and `unstable_cache`

The incremental cache now reads the Resume Data Cache (RDC) during
on-demand revalidation before it bypasses persisted entries. Both
`unstable_cache` and cached `fetch` calls use this path, so the final
prerender does not repeat asynchronous work and fail with
`NEXT_STATIC_GEN_BAILOUT`.

This extends the fix proposed in PR #100895 to cached fetches and preserves
the forced-refresh behavior introduced in PR #48516. The incremental cache
continues to apply its tag invalidation checks before it returns an entry.

Fixes #99866
Closes #100895
parent 64b6a09a
...@@ -519,6 +519,9 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -519,6 +519,9 @@ export class IncrementalCache implements IncrementalCacheType {
// Unlike other caches if we have a resume data cache, we use it even if // Unlike other caches if we have a resume data cache, we use it even if
// testmode would normally disable it or if requestHeaders say 'no-cache'. // testmode would normally disable it or if requestHeaders say 'no-cache'.
if (ctx.kind === IncrementalCacheKind.FETCH) { if (ctx.kind === IncrementalCacheKind.FETCH) {
const workStore = workAsyncStorage.getStore()
const isOnDemandRevalidate =
this.isOnDemandRevalidate || workStore?.isOnDemandRevalidate
const workUnitStore = workUnitAsyncStorage.getStore() const workUnitStore = workUnitAsyncStorage.getStore()
const resumeDataCache = workUnitStore const resumeDataCache = workUnitStore
? getResumeDataCache(workUnitStore) ? getResumeDataCache(workUnitStore)
...@@ -529,7 +532,6 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -529,7 +532,6 @@ export class IncrementalCache implements IncrementalCacheType {
// Check if any tags were recently revalidated before returning RDC entry. // Check if any tags were recently revalidated before returning RDC entry.
// When a server action calls updateTag(), the re-render should see fresh // When a server action calls updateTag(), the re-render should see fresh
// data instead of stale RDC data. // data instead of stale RDC data.
const workStore = workAsyncStorage.getStore()
const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])] const combinedTags = [...(ctx.tags || []), ...(ctx.softTags || [])]
const hasRevalidatedTag = combinedTags.some( const hasRevalidatedTag = combinedTags.some(
(tag) => (tag) =>
...@@ -559,6 +561,13 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -559,6 +561,13 @@ export class IncrementalCache implements IncrementalCacheType {
console.log('IncrementalCache: rdc:no-resume-data') console.log('IncrementalCache: rdc:no-resume-data')
} }
} }
if (isOnDemandRevalidate) {
// On-demand revalidation reuses the current render's RDC, but
// recomputes persisted data even when its revalidate interval has not
// elapsed.
return null
}
} }
// we don't leverage the prerender cache in dev mode // we don't leverage the prerender cache in dev mode
......
...@@ -1084,16 +1084,14 @@ export function createPatchedFetcher( ...@@ -1084,16 +1084,14 @@ export function createPatchedFetcher(
if (isCacheableRevalidate && !cachedFetchData) { if (isCacheableRevalidate && !cachedFetchData) {
handleUnlock = await incrementalCache.lock(cacheKey) handleUnlock = await incrementalCache.lock(cacheKey)
const entry = workStore.isOnDemandRevalidate const entry = await incrementalCache.get(cacheKey, {
? null kind: IncrementalCacheKind.FETCH,
: await incrementalCache.get(cacheKey, { revalidate: finalRevalidate,
kind: IncrementalCacheKind.FETCH, fetchUrl,
revalidate: finalRevalidate, fetchIdx,
fetchUrl, tags,
fetchIdx, softTags: implicitTags?.tags,
tags, })
softTags: implicitTags?.tags,
})
if (hasNoExplicitCacheConfig && workUnitStore) { if (hasNoExplicitCacheConfig && workUnitStore) {
switch (workUnitStore.type) { switch (workUnitStore.type) {
......
...@@ -228,8 +228,6 @@ export function unstable_cache<T extends Callback>( ...@@ -228,8 +228,6 @@ export function unstable_cache<T extends Callback>(
// we should bypass cache similar to fetches // we should bypass cache similar to fetches
!isNestedUnstableCache && !isNestedUnstableCache &&
workStore.fetchCache !== 'force-no-store' && workStore.fetchCache !== 'force-no-store' &&
!workStore.isOnDemandRevalidate &&
!incrementalCache.isOnDemandRevalidate &&
!workStore.isDraftMode !workStore.isDraftMode
) { ) {
// We attempt to get the current cache entry from the incremental cache. // We attempt to get the current cache entry from the incremental cache.
...@@ -357,33 +355,30 @@ export function unstable_cache<T extends Callback>( ...@@ -357,33 +355,30 @@ export function unstable_cache<T extends Callback>(
// If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in // If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in
// the background. If the entry is missing or invalid we generate a new entry and return it. // the background. If the entry is missing or invalid we generate a new entry and return it.
if (!incrementalCache.isOnDemandRevalidate) { const cacheEntry = await incrementalCache.get(cacheKey, {
// We aren't doing an on demand revalidation so we check use the cache if valid kind: IncrementalCacheKind.FETCH,
const cacheEntry = await incrementalCache.get(cacheKey, { revalidate,
kind: IncrementalCacheKind.FETCH, tags,
revalidate, fetchIdx,
tags, fetchUrl,
fetchIdx, softTags: implicitTags?.tags,
fetchUrl, })
softTags: implicitTags?.tags,
}) if (cacheEntry && cacheEntry.value) {
// The entry exists and has a value
if (cacheEntry && cacheEntry.value) { if (cacheEntry.value.kind !== CachedRouteKind.FETCH) {
// The entry exists and has a value // The entry is invalid and we need a special warning
if (cacheEntry.value.kind !== CachedRouteKind.FETCH) { // @TODO why do we warn this way? Should this just be an error? How are these errors surfaced
// The entry is invalid and we need a special warning // so bugs can be reported
// @TODO why do we warn this way? Should this just be an error? How are these errors surfaced console.error(
// so bugs can be reported `Invariant invalid cacheEntry returned for ${invocationKey}`
console.error( )
`Invariant invalid cacheEntry returned for ${invocationKey}` // will fall through to generating a new cache entry below
) } else if (!cacheEntry.isStale) {
// will fall through to generating a new cache entry below // We have a valid cache entry and it is fresh so we return it
} else if (!cacheEntry.isStale) { return cacheEntry.value.data.body !== undefined
// We have a valid cache entry and it is fresh so we return it ? JSON.parse(cacheEntry.value.data.body)
return cacheEntry.value.data.body !== undefined : undefined
? JSON.parse(cacheEntry.value.data.body)
: undefined
}
} }
} }
......
import { cacheLife } from 'next/cache'
export default async function Expire() {
'use cache'
cacheLife({ revalidate: 60, expire: 300 })
return null
}
import Expire from '../../expire'
export function generateStaticParams() {
return [{ method: 'get' }, { method: 'post' }]
}
export default async function Page({
params,
}: {
params: Promise<{ method: string }>
}) {
const { method } = await params
const response = await fetch(
`https://next-data-api-endpoint.vercel.app/api/random?data-cache-revalidation=fetch-${method}`,
{ method: method.toUpperCase(), next: { revalidate: 3600 } }
)
const value = await response.text()
return (
<>
<Expire />
<p id="value">{value}</p>
</>
)
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { unstable_cache } from 'next/cache'
import Expire from '../expire'
const getValue = unstable_cache(
async () => {
const response = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?data-cache-revalidation=unstable-cache',
{
cache: 'no-store',
}
)
return response.text()
},
['data-cache-revalidation:unstable-cache'],
{ revalidate: 3600 }
)
export default async function Page() {
return (
<>
<Expire />
<p id="value">{await getValue()}</p>
</>
)
}
import { nextTestSetup } from 'e2e-utils'
import { load } from 'cheerio'
describe('data-cache-revalidation', () => {
const { next } = nextTestSetup({
files: __dirname,
})
async function renderValue(
pathname: string,
headers?: Record<string, string>
) {
const response = await next.fetch(pathname, { headers })
expect(response.status).toBe(200)
const value = load(await response.text())('#value').text()
expect(value).toBeTruthy()
return value
}
async function expectDataRevalidation(pathname: string, mode: string) {
const initial = await renderValue(pathname)
let fresh: string
if (mode === 'past expire') {
fresh = await renderValue(pathname, {
'x-test-cache-age-offset-ms': String(301 * 1000),
})
} else {
const response = await next.fetch(
`/api/revalidate?path=${encodeURIComponent(pathname)}`
)
expect(response.status).toBe(200)
expect(await response.json()).toEqual({ revalidated: true })
fresh = await renderValue(pathname)
}
expect(fresh).not.toBe(initial)
expect(await renderValue(pathname)).toBe(fresh)
}
describe.each(['past expire', 'on demand'])('%s', (mode) => {
it.each(['/unstable-cache', '/fetch/get', '/fetch/post'])(
'should regenerate %s with fresh cached data',
async (pathname) => {
await expectDataRevalidation(pathname, mode)
}
)
})
it('should refresh Pages Router unstable_cache data on demand', async () => {
await expectDataRevalidation('/pages-unstable-cache', 'on demand')
})
})
const {
default: FileSystemCache,
} = require('next/dist/server/lib/incremental-cache/file-system-cache')
// Age route entries without changing the timestamps of their cached data.
module.exports = class IncrementalCacheHandler extends FileSystemCache {
constructor(context) {
super(context)
this.requestHeaders = context._requestHeaders
}
async get(key, context) {
const result = await super.get(key, context)
const offsetHeader = this.requestHeaders?.['x-test-cache-age-offset-ms']
const offsetMs = typeof offsetHeader === 'string' ? Number(offsetHeader) : 0
if (result && offsetMs > 0 && context.kind !== 'FETCH') {
return { ...result, lastModified: result.lastModified - offsetMs }
}
return result
}
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
cacheHandler: require.resolve('./incremental-cache-handler'),
}
export default nextConfig
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
if (typeof request.query.path !== 'string') {
response.status(400).end()
return
}
await response.revalidate(request.query.path)
response.json({ revalidated: true })
}
import { unstable_cache } from 'next/cache'
const getValue = unstable_cache(
async () => {
const response = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?data-cache-revalidation=pages-unstable-cache'
)
return response.text()
},
['data-cache-revalidation:pages-unstable-cache'],
{ revalidate: 3600 }
)
export async function getStaticProps() {
return { props: { value: await getValue() }, revalidate: 60 }
}
export default function Page({ value }: { value: string }) {
return <p id="value">{value}</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