revalidatePath
revalidatePath
allows you to revalidate data associated with a specific path. 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 } from 'next/server';
import { revalidatePath } from 'next/cache';
export async function GET(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path') || '/';
revalidatePath(path);
return NextResponse.json({ revalidated: true, now: Date.now() });
}
Good to know:
revalidatePath
is available in both Node.js and Edge runtimes.
Parameters
revalidatePath(path: string): void;
path
: A string representing the path associated with the data you want to revalidate.
Returns
revalidatePath
does not return any value.
Examples
Node.js Runtime
app/api/revalidate/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { revalidatePath } from 'next/cache';
export async function GET(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path') || '/';
revalidatePath(path);
return NextResponse.json({ revalidated: true, now: Date.now() });
}
Edge Runtime
app/api/revalidate/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { revalidatePath } from 'next/cache';
export const runtime = 'edge';
export async function GET(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path') || '/';
revalidatePath(path);
return NextResponse.json({ revalidated: true, now: Date.now() });
}