The cacheTag function allows you to tag cached data for on-demand invalidation. By associating tags with cache entries, you can selectively purge or revalidate specific cache entries without affecting other cached data.
Usage
To use cacheTag, enable the cacheComponents flag in your next.config.js file:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfig
const nextConfig = {
cacheComponents: true,
}
export default nextConfig
The cacheTag function takes one or more string values.
import { cacheTag } from 'next/cache'
export async function getData() {
'use cache'
cacheTag('my-data')
const data = await fetch('/api/data')
return data
}
import { cacheTag } from 'next/cache'
export async function getData() {
'use cache'
cacheTag('my-data')
const data = await fetch('/api/data')
return data
}
You can then purge the cache on-demand from a Server Function or Route Handler:
- Use
updateTagfor 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.updateTagis only available inside Server Functions. - Use
revalidateTagwhen it is acceptable to serve stale data while revalidation happens in the background, or when revalidating from a Route Handler or other context.
For example, this Server Function adds a post and then purges every cache entry tagged 'my-data' so the next read reflects the change:
'use server'
import { updateTag } from 'next/cache'
export default async function submit() {
await addPost()
updateTag('my-data')
}
'use server'
import { updateTag } from 'next/cache'
export default async function submit() {
await addPost()
updateTag('my-data')
}
Good to know
- Idempotent Tags: Applying the same tag multiple times has no additional effect.
- Multiple Tags: You can assign multiple tags to a single cache entry by passing multiple string values to
cacheTag.
cacheTag('tag-one', 'tag-two')
- Limits: A single
cacheTag()call accepts up to 128 tags, each with a maximum length of 256 characters. Tags longer than 256 characters are skipped, and any tags past the 128th in one call are dropped. Both cases log a console warning.
Examples
Tagging components or functions
Tag your cached data by calling cacheTag within a cached function or component:
import { cacheTag } from 'next/cache'
interface BookingsProps {
type: string
}
export async function Bookings({ type = 'haircut' }: BookingsProps) {
'use cache'
cacheTag('bookings-data')
async function getBookingsData() {
const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`)
return data
}
return //...
}
import { cacheTag } from 'next/cache'
export async function Bookings({ type = 'haircut' }) {
'use cache'
cacheTag('bookings-data')
async function getBookingsData() {
const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`)
return data
}
return //...
}
Creating tags from external data
You can use the data returned from an async function to tag the cache entry.
import { cacheTag } from 'next/cache'
interface BookingsProps {
type: string
}
export async function Bookings({ type = 'haircut' }: BookingsProps) {
async function getBookingsData() {
'use cache'
const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`)
cacheTag('bookings-data', data.id)
return data
}
return //...
}
import { cacheTag } from 'next/cache'
export async function Bookings({ type = 'haircut' }) {
async function getBookingsData() {
'use cache'
const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`)
cacheTag('bookings-data', data.id)
return data
}
return //...
}
Invalidating tagged cache
Using revalidateTag, you can invalidate the cache for a specific tag when needed:
'use server'
import { revalidateTag } from 'next/cache'
export async function updateBookings() {
await updateBookingData()
revalidateTag('bookings-data', 'max')
}
'use server'
import { revalidateTag } from 'next/cache'
export async function updateBookings() {
await updateBookingData()
revalidateTag('bookings-data', 'max')
}
Source: docs/01-app/03-api-reference/04-functions/cacheTag.mdx