Unverified Commit 5e144103 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

docs: add updateTag example to cacheTag page (#94508)

### What?

Adds an `updateTag` example to the `cacheTag` API reference page.

### Why?

The `cacheTag` docs only showed `revalidateTag` for cache invalidation.
For forms and user-triggered mutations (the dominant use case for
`cacheTag`), `updateTag` is the recommended API because it provides
read-your-own-writes semantics. DX agent friction logs showed agents
defaulting to `revalidateTag` and hitting the deprecated single-arg
error before discovering `updateTag` via web search.

### How?

Added a second example block after the existing `revalidateTag` one,
showing `updateTag('my-data')` in a Server Function.
Co-authored-by: 's avatarJoseph Chamochumbi <joseph.chamochumbi@vercel.com>
parent bd9f0eb4
...@@ -62,30 +62,30 @@ export async function getData() { ...@@ -62,30 +62,30 @@ export async function getData() {
You can then purge the cache on-demand from a [Server Function](/docs/app/getting-started/mutating-data) or [Route Handler](/docs/app/api-reference/file-conventions/route): You can then purge the cache on-demand from a [Server Function](/docs/app/getting-started/mutating-data) or [Route Handler](/docs/app/api-reference/file-conventions/route):
- Use [`updateTag`](/docs/app/api-reference/functions/updateTag) inside a Server Function for read-your-own-writes scenarios, where a user makes a change and the next read should fetch fresh data immediately. - Use [`updateTag`](/docs/app/api-reference/functions/updateTag) for read-your-own-writes scenarios, such as forms and other user-triggered mutations, where a user makes a change and the next read should fetch fresh data immediately. `updateTag` is only available inside Server Functions.
- Use [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) when it is acceptable to serve stale data while revalidation happens in the background, or when revalidating from a route handler. - Use [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) when it is acceptable to serve stale data while revalidation happens in the background, or when revalidating from a [Route Handler](/docs/app/api-reference/file-conventions/route) or other context.
The example below uses `revalidateTag`: For example, this Server Function adds a post and then purges every cache entry tagged `'my-data'` so the next read reflects the change:
```tsx filename="app/action.ts" switcher ```tsx filename="app/action.ts" switcher
'use server' 'use server'
import { revalidateTag } from 'next/cache' import { updateTag } from 'next/cache'
export default async function submit() { export default async function submit() {
await addPost() await addPost()
revalidateTag('my-data', 'max') updateTag('my-data')
} }
``` ```
```jsx filename="app/action.js" switcher ```jsx filename="app/action.js" switcher
'use server' 'use server'
import { revalidateTag } from 'next/cache' import { updateTag } from 'next/cache'
export default async function submit() { export default async function submit() {
await addPost() await addPost()
revalidateTag('my-data', 'max') updateTag('my-data')
} }
``` ```
......
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