Skip to content

Commit 566da10

Browse files
committed
content in doc, strapi integration, custom comps supported, isr tested, 404 handling todo
1 parent f9466d7 commit 566da10

File tree

7 files changed

+8345
-6942
lines changed

7 files changed

+8345
-6942
lines changed

app/api/revalidate/route.ts

Lines changed: 176 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,196 @@
1-
import { revalidateTag } from 'next/cache'
1+
22
import { NextRequest, NextResponse } from 'next/server'
3+
import { revalidatePath, revalidateTag } from 'next/cache'
4+
import { clearPathsCache } from '@/utils/strapi'
35

4-
const REVALIDATE_SECRET = process.env.REVALIDATE_SECRET
6+
interface RevalidationResult {
7+
path?: string
8+
tag?: string
9+
revalidated: boolean
10+
type: 'route' | 'path' | 'tag'
11+
timestamp: string
12+
}
513

614
export async function POST(request: NextRequest) {
715
try {
8-
const requestData = await request.json()
9-
const { tag, secret } = requestData
16+
const body = await request.json()
17+
const {
18+
paths,
19+
path,
20+
tags,
21+
tag,
22+
secret,
23+
revalidateAll = false,
24+
clearCache = false
25+
} = body
26+
27+
if (secret !== process.env.REVALIDATE_SECRET) {
28+
return NextResponse.json(
29+
{ message: 'Invalid secret' },
30+
{ status: 401 }
31+
)
32+
}
33+
34+
const results: RevalidationResult[] = []
35+
36+
if (clearCache) {
37+
clearPathsCache()
38+
console.log('Cleared paths cache')
39+
}
40+
41+
if (revalidateAll) {
42+
revalidatePath('/', 'layout')
43+
revalidateTag('mdx-content-list')
44+
revalidateTag('mdx-paths')
45+
46+
results.push({
47+
path: '/',
48+
revalidated: true,
49+
type: 'route',
50+
timestamp: new Date().toISOString(),
51+
})
52+
}
53+
54+
if (path) {
55+
revalidatePath(path)
56+
revalidateTag(`mdx-content-${path}`)
57+
58+
results.push({
59+
path,
60+
revalidated: true,
61+
type: 'path',
62+
timestamp: new Date().toISOString(),
63+
})
64+
}
65+
66+
if (paths && Array.isArray(paths)) {
67+
for (const p of paths) {
68+
revalidatePath(p)
69+
revalidateTag(`mdx-content-${p}`)
70+
71+
results.push({
72+
path: p,
73+
revalidated: true,
74+
type: 'path',
75+
timestamp: new Date().toISOString(),
76+
})
77+
}
78+
}
79+
80+
if (tag) {
81+
revalidateTag(tag)
1082

11-
// Check for secret if configured
12-
if (REVALIDATE_SECRET && secret !== REVALIDATE_SECRET) {
13-
return NextResponse.json({ message: 'Invalid revalidation token' }, { status: 401 })
83+
results.push({
84+
tag,
85+
revalidated: true,
86+
type: 'tag',
87+
timestamp: new Date().toISOString(),
88+
})
1489
}
1590

16-
// Verify tag is provided
17-
if (!tag) {
18-
return NextResponse.json({ message: 'Missing tag parameter' }, { status: 400 })
91+
if (tags && Array.isArray(tags)) {
92+
for (const t of tags) {
93+
revalidateTag(t)
94+
95+
results.push({
96+
tag: t,
97+
revalidated: true,
98+
type: 'tag',
99+
timestamp: new Date().toISOString(),
100+
})
101+
}
19102
}
20103

21-
// Revalidate the tag
22-
revalidateTag(tag)
104+
console.log('Revalidation completed:', results)
105+
106+
return NextResponse.json({
107+
revalidated: true,
108+
results,
109+
timestamp: new Date().toISOString(),
110+
})
111+
} catch (error) {
112+
console.error('Revalidation error:', error)
113+
return NextResponse.json(
114+
{
115+
message: 'Error revalidating paths',
116+
error: error instanceof Error ? error.message : 'Unknown error'
117+
},
118+
{ status: 500 }
119+
)
120+
}
121+
}
122+
123+
export async function GET(request: NextRequest) {
124+
const { searchParams } = new URL(request.url)
125+
const path = searchParams.get('path')
126+
const tag = searchParams.get('tag')
127+
const secret = searchParams.get('secret')
128+
const revalidateAll = searchParams.get('revalidateAll') === 'true'
129+
const clearCache = searchParams.get('clearCache') === 'true'
130+
131+
if (secret !== process.env.REVALIDATE_SECRET) {
132+
return NextResponse.json(
133+
{ message: 'Invalid secret' },
134+
{ status: 401 }
135+
)
136+
}
137+
138+
try {
139+
const results: RevalidationResult[] = []
140+
141+
if (clearCache) {
142+
clearPathsCache()
143+
console.log('Cleared paths cache')
144+
}
145+
146+
if (revalidateAll) {
147+
revalidatePath('/', 'layout')
148+
revalidateTag('mdx-content-list')
149+
revalidateTag('mdx-paths')
150+
151+
results.push({
152+
path: '/',
153+
revalidated: true,
154+
type: 'route',
155+
timestamp: new Date().toISOString(),
156+
})
157+
}
158+
159+
if (path) {
160+
revalidatePath(path)
161+
revalidateTag(`mdx-content-${path}`)
162+
163+
results.push({
164+
path,
165+
revalidated: true,
166+
type: 'path',
167+
timestamp: new Date().toISOString(),
168+
})
169+
}
170+
171+
if (tag) {
172+
revalidateTag(tag)
173+
174+
results.push({
175+
tag,
176+
revalidated: true,
177+
type: 'tag',
178+
timestamp: new Date().toISOString(),
179+
})
180+
}
23181

24182
return NextResponse.json({
25183
revalidated: true,
26-
message: `Tag "${tag}" revalidated successfully`,
27-
timestamp: Date.now(),
184+
results,
185+
timestamp: new Date().toISOString(),
28186
})
29187
} catch (error) {
30188
console.error('Revalidation error:', error)
31189
return NextResponse.json(
32-
{ message: 'Error processing revalidation request', error: String(error) },
190+
{
191+
message: 'Error revalidating paths',
192+
error: error instanceof Error ? error.message : 'Unknown error'
193+
},
33194
{ status: 500 }
34195
)
35196
}

0 commit comments

Comments
 (0)