1- import { revalidateTag } from 'next/cache'
21import { NextRequest, NextResponse } from 'next/server'
3-
4- const REVALIDATE_SECRET = process.env.REVALIDATE_SECRET
2+ import { revalidatePath, revalidateTag } from 'next/cache'
53
64export async function POST(request: NextRequest) {
75 try {
8- const requestData = await request.json()
9- const { tag, secret } = requestData
6+ const body = await request.json()
7+ const { path, tag, secret } = body
108
11- // Check for secret if configured
12- if (REVALIDATE_SECRET && secret !== REVALIDATE_SECRET) {
13- return NextResponse.json({ message: 'Invalid revalidation token' }, { status: 401 })
9+ if (secret !== process.env.REVALIDATION_SECRET) {
10+ return NextResponse.json({ message: 'Invalid secret' }, { status: 401 })
1411 }
1512
16- // Verify tag is provided
17- if (!tag) {
18- return NextResponse.json({ message: 'Missing tag parameter' }, { status: 400 })
13+ const timestamp = new Date().toISOString()
14+
15+ if (path) {
16+ // Revalidate specific path
17+ revalidatePath(path)
18+ console.log(`[ISR] Revalidated path: ${path} at ${timestamp}`)
19+ return NextResponse.json({
20+ message: `Path ${path} revalidated successfully`,
21+ timestamp,
22+ type: 'path'
23+ })
1924 }
2025
21- // Revalidate the tag
22- revalidateTag(tag)
26+ if (tag) {
27+ // Revalidate by tag
28+ revalidateTag(tag)
29+ console.log(`[ISR] Revalidated tag: ${tag} at ${timestamp}`)
30+ return NextResponse.json({
31+ message: `Tag ${tag} revalidated successfully`,
32+ timestamp,
33+ type: 'tag'
34+ })
35+ }
2336
24- return NextResponse.json({
25- revalidated: true,
26- message: `Tag "${tag}" revalidated successfully`,
27- timestamp: Date.now(),
28- })
37+ return NextResponse.json({ message: 'No path, tag, or type provided' }, { status: 400 })
2938 } catch (error) {
30- console.error('Revalidation error:', error)
39+ console.error('[ISR] Revalidation error:', error)
3140 return NextResponse.json(
32- { message: 'Error processing revalidation request ', error: String( error) },
41+ { message: 'Error revalidating ', error: error.message },
3342 { status: 500 }
3443 )
3544 }
3645}
46+
47+ // to check revalidation status
48+ export async function GET() {
49+ return NextResponse.json({
50+ message: 'ISR Revalidation API is active',
51+ timestamp: new Date().toISOString(),
52+ endpoints: {
53+ POST: 'Trigger revalidation with { path, tag, secret }'
54+ }
55+ })
56+ }
0 commit comments