Skip to content

revalidateTag

revalidateTag allows you to purge cached data on-demand for a specific cache tag.

Usage

revalidateTag can be called in Server Functions and Route Handlers.

revalidateTag cannot be called in Client Components or Middleware, as it only works in server environments.

Good to know: revalidateTag marks tagged data as stale, but fresh data is only fetched when pages using that tag are next visited. This means calling revalidateTag will not immediately trigger many revalidations at once. The invalidation only happens when any page using that tag is next visited.

Parameters

revalidateTag(tag: string): void;
  • tag: A string representing the cache tag associated with the data you want to revalidate. Must not exceed 256 characters. This value is case-sensitive.

You can add tags to fetch as follows:

fetch(url, { next: { tags: [...] } });

Returns

revalidateTag does not return a value.

Relationship with revalidatePath

revalidateTag revalidates data with specific tags across all pages that use those tags, while revalidatePath revalidates specific page or layout paths.

Good to know: These functions serve different purposes and may need to be used together for comprehensive data consistency. For detailed examples and considerations, see Relationship with revalidateTag.

Examples

Server Action

app/actions.ts
'use server'
 
import { revalidateTag } from 'next/cache'
 
export default async function submit() {
  await addPost()
  revalidateTag('posts')
}

Route Handler

app/api/revalidate/route.ts
import type { NextRequest } from 'next/server'
import { revalidateTag } from 'next/cache'
 
export async function GET(request: NextRequest) {
  const tag = request.nextUrl.searchParams.get('tag')
 
  if (tag) {
    revalidateTag(tag)
    return Response.json({ revalidated: true, now: Date.now() })
  }
 
  return Response.json({
    revalidated: false,
    now: Date.now(),
    message: 'Missing tag to revalidate',
  })
}