Unverified Commit 00d3c830 authored by conico974's avatar conico974 Committed by GitHub

Revalidate `"use cache"` caches during on-demand revalidation (#76100)

### What?
When doing an On Demand revalidation (with `res.revalidate` or using
`x-prerender-revalidate` header ), `use cache` entry will never get
revalidated

### Why?
It should behave the same as the fetch cache or `unstable_cache`

### How?

We force revalidation of `use cache` entry when doing an on demand
revalidation

---------
Co-authored-by: 's avatarHendrik Liebau <mail@hendrik-liebau.de>
parent 4d940d35
...@@ -910,6 +910,10 @@ function shouldForceRevalidate( ...@@ -910,6 +910,10 @@ function shouldForceRevalidate(
workStore: WorkStore, workStore: WorkStore,
workUnitStore: WorkUnitStore | undefined workUnitStore: WorkUnitStore | undefined
): boolean { ): boolean {
if (workStore.isOnDemandRevalidate) {
return true
}
if (workStore.dev && workUnitStore) { if (workStore.dev && workUnitStore) {
if (workUnitStore.type === 'request') { if (workUnitStore.type === 'request') {
return workUnitStore.headers.get('cache-control') === 'no-cache' return workUnitStore.headers.get('cache-control') === 'no-cache'
......
import { revalidatePath } from 'next/cache'
import { RevalidateButtons } from './revalidate-buttons'
async function cachedValue() {
'use cache'
return Math.random()
}
export default async function Page() {
const value = await cachedValue()
return (
<div>
<p id="value">{value}</p>
<RevalidateButtons
revalidatePath={async () => {
'use server'
revalidatePath('/on-demand-revalidate')
}}
/>
</div>
)
}
'use client'
export function RevalidateButtons({
revalidatePath,
}: {
revalidatePath: () => Promise<void>
}) {
return (
<form>
<button id="revalidate-path" formAction={revalidatePath}>
revalidate with revalidatePath()
</button>{' '}
<button
id="revalidate-api-route"
formAction={async () => {
await fetch('/api/revalidate?path=/on-demand-revalidate', {
method: 'POST',
})
}}
>
revalidate with API route
</button>
</form>
)
}
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const pathParam = req.query['path']
if (!pathParam) {
return res.status(400).send(`Missing required query param "path"`)
}
const paths = Array.isArray(pathParam) ? pathParam : [pathParam]
try {
await Promise.all(paths.map((path) => res.revalidate(path)))
return res.json({ revalidated: true })
} catch (err) {
console.error(err)
return res.status(500).send(`Error revalidating ${paths}`)
}
}
...@@ -304,6 +304,30 @@ describe('use-cache', () => { ...@@ -304,6 +304,30 @@ describe('use-cache', () => {
} }
if (isNextStart) { if (isNextStart) {
// res.revalidate does not work in dev mode
it('should revalidate caches during on-demand revalidation', async () => {
const browser = await next.browser('/on-demand-revalidate')
const initial = await browser.elementById('value').text()
// Bust the ISR cache first to populate the "use cache" in-memory cache for
// the subsequent on-demand revalidation.
await browser.elementById('revalidate-path').click()
await retry(async () => {
expect(await browser.elementById('value').text()).not.toBe(initial)
})
const value = await browser.elementById('value').text()
await browser.elementById('revalidate-api-route').click()
await browser.waitForIdleNetwork()
await retry(async () => {
await browser.refresh()
expect(await browser.elementById('value').text()).not.toBe(value)
})
})
it('should prerender fully cacheable pages as static HTML', async () => { it('should prerender fully cacheable pages as static HTML', async () => {
const prerenderManifest = JSON.parse( const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json') await next.readFile('.next/prerender-manifest.json')
...@@ -336,6 +360,7 @@ describe('use-cache', () => { ...@@ -336,6 +360,7 @@ describe('use-cache', () => {
'/logs', '/logs',
'/method-props', '/method-props',
'/not-found', '/not-found',
'/on-demand-revalidate',
'/passed-to-client', '/passed-to-client',
'/react-cache', '/react-cache',
'/referential-equality', '/referential-equality',
......
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