Skip to content

    revalidateTag

    revalidateTag allows you to revalidate data associated with a specific cache tag. This is useful for scenarios where you want to update your cached data without waiting for a revalidation period to expire.

    app/api/revalidate/route.ts
    import { NextRequest, NextResponse, revalidateTag } from 'next/server';
     
    export async function GET(request: NextRequest) {
      const tag = request.nextUrl.searchParams.get('tag');
      revalidateTag(tag);
      return NextResponse.json({ revalidated: true, now: Date.now() });
    }

    Good to know:

    Parameters

    revalidateTag(tag: string): void;
    • tag: A string representing the cache tag associated with the data you want to revalidate.

    You can add tags to fetch as follows:

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

    Returns

    revalidateTag does not return any value.

    Examples

    Node.js Runtime

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

    Edge Runtime

    app/api/revalidate/route.ts
    import { NextRequest, NextResponse } from 'next/server';
    import { revalidateTag } from 'next/cache';
     
    export const runtime = 'edge';
     
    export async function GET(request: NextRequest) {
      const tag = request.nextUrl.searchParams.get('tag');
      revalidateTag(tag);
      return NextResponse.json({ revalidated: true, now: Date.now() });
    }

    Was this helpful?