Files
Jimmy LaiandGitHub 451a54cb2e cache: add unstable_noStore API (#56930)
This PR introduces a new API, `unstable_noStore`, which will allow users to declaratively opt out of caching anywhere during static generation in the same way that you can specify `cache: 'no-store'` on a fetch call in Next.js.

An important caveat and difference from just calling `cookies()` to opt-out of static generation is that this won't opt you out when called from within `unstable_cache` and instead defers to the cache configuration to it.

```
import {unstable_noStore as noStore} from 'next/cache';

export default async function Component() {
  noStore();
  const result = await db.query(...);
}
```
2023-10-17 14:52:46 +00:00

29 lines
650 B
TypeScript

import { revalidateTag, unstable_cache } from 'next/cache'
import { getUncachedRandomData } from '../no-store-fn'
import { RevalidateButton } from '../revalidate-button'
export default async function Page() {
async function revalidate() {
'use server'
await revalidateTag('no-store-fn')
}
const cachedData = await unstable_cache(
async () => {
return getUncachedRandomData()
},
['random'],
{
tags: ['no-store-fn'],
}
)()
return (
<div>
<p>random: {Math.random()}</p>
<p id="data">cachedData: {cachedData.random}</p>
<RevalidateButton onClick={revalidate} />
</div>
)
}