typescriptadvanced
On-Demand ISR Revalidation
Trigger incremental static regeneration via API route with secret token validation for instant cache purge.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { NextRequest, NextResponse } from 'next/server';
import { revalidatePath, revalidateTag } from 'next/cache';
export async function POST(request: NextRequest) {
const secret = request.headers.get('x-revalidation-secret');
if (secret !== process.env.REVALIDATION_SECRET) {
return NextResponse.json({ error: 'Invalid secret' }, { status: 401 });
}
try {
const body = await request.json();
const { type, value } = body as { type: 'path' | 'tag'; value: string };
if (type === 'path') {
revalidatePath(value);
} else if (type === 'tag') {
revalidateTag(value);
} else {
return NextResponse.json({ error: 'Invalid type' }, { status: 400 });
}
return NextResponse.json({
revalidated: true,
type,
value,
timestamp: Date.now(),
});
} catch {
return NextResponse.json(
{ error: 'Failed to revalidate' },
{ status: 500 }
);
}
}
// Usage (from CMS webhook or admin panel):
// curl -X POST https://example.com/api/revalidate \
// -H 'x-revalidation-secret: my-secret' \
// -H 'Content-Type: application/json' \
// -d '{"type": "path", "value": "/blog/my-post"}'Use Cases
- CMS content updates
- Admin panel publish actions
- Webhook-triggered cache busting
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Next.js ISR & On-Demand Revalidation
Configure Incremental Static Regeneration with time-based and on-demand revalidation strategies.
Best for: Caching CMS content with periodic refresh
#nextjs#isr
typescriptadvanced
Caching Strategies in Next.js
Master Next.js caching with fetch cache, unstable_cache, revalidatePath, and revalidateTag patterns.
Best for: ISR page caching
#nextjs#caching
typescriptadvanced
Cache Tags and On-Demand Revalidation
Tag cached data with identifiers and revalidate specific cache entries on demand.
Best for: CMS webhooks
#nextjs#caching
typescriptadvanced
Data Caching with unstable_cache
Cache database queries and expensive computations with unstable_cache for server-side memoization.
Best for: database query caching
#nextjs#cache