-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathroute.ts
More file actions
186 lines (174 loc) · 4.53 KB
/
route.ts
File metadata and controls
186 lines (174 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { revalidatePath, revalidateTag } from 'next/cache'
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
/**
* All revalidation paths for the site
* Includes both base paths and locale-prefixed paths
*/
const ALL_REVALIDATION_PATHS = [
// Root
'/',
// Product categories
'ides',
'clis',
'extensions',
'models',
'model-providers',
'vendors',
// Content
'articles',
'ai-coding-stack',
'docs',
'curated-collections',
'manifesto',
'ai-coding-landscape',
'open-source-rank',
'search',
]
/**
* Category-specific paths
*/
const CATEGORY_PATHS: Record<string, string[]> = {
ides: ['ides'],
clis: ['clis'],
extensions: ['extensions'],
models: ['models'],
providers: ['model-providers'],
vendors: ['vendors'],
tools: ['ides', 'clis', 'extensions'],
content: ['articles', 'ai-coding-stack', 'docs'],
}
/**
* Extract secret from headers or query params
*/
function extractSecret(request: NextRequest): string | null {
// Check Authorization header
const authHeader = request.headers.get('authorization')
if (authHeader?.startsWith('Bearer ')) {
return authHeader.slice(7)
}
// Check query param
const querySecret = request.nextUrl.searchParams.get('secret')
if (querySecret) {
return querySecret
}
// Check x-secret header
const xSecret = request.headers.get('x-secret')
if (xSecret) {
return xSecret
}
return null
}
/**
* Check for secret to confirm this is a valid request
*/
function isValidSecret(secret: string | null): boolean {
const envSecret = process.env.REVALIDATION_SECRET
if (!envSecret) {
return false
}
return secret === envSecret
}
/**
* GET /api/revalidate - Show usage information
*/
export async function GET() {
return NextResponse.json({
message: 'On-Demand Revalidation API',
usage: {
method: 'POST',
url: '/api/revalidate',
authentication:
'Bearer token in Authorization header, OR x-secret header, OR secret query param',
queryParams: {
path: 'Specific path to revalidate (e.g., /ides)',
tag: 'Tag to revalidate (if pages use tags)',
category: 'Category to revalidate (e.g., ide, tools)',
},
examples: [
'POST /api/revalidate?secret=YOUR_SECRET&path=/ides',
'POST /api/revalidate?secret=YOUR_SECRET&tag=manifests',
'POST /api/revalidate?secret=YOUR_SECRET&category=tools',
'curl: curl -X POST "/api/revalidate?secret=xxx&path=/ides"',
],
categories: Object.keys(CATEGORY_PATHS),
},
})
}
export async function POST(request: NextRequest) {
const secret = extractSecret(request)
// Check for secret to confirm this is a valid request
if (!isValidSecret(secret)) {
return NextResponse.json(
{ message: 'Unauthorized - Invalid or missing secret' },
{ status: 401 }
)
}
const path = request.nextUrl.searchParams.get('path')
const tag = request.nextUrl.searchParams.get('tag')
const category = request.nextUrl.searchParams.get('category')
try {
if (path) {
// Revalidate specific path
revalidatePath(path)
return NextResponse.json({
revalidated: true,
type: 'path',
target: path,
now: Date.now(),
})
} else if (tag) {
// Revalidate by tag
revalidateTag(tag, 'layout')
return NextResponse.json({
revalidated: true,
type: 'tag',
target: tag,
now: Date.now(),
})
} else if (category) {
// Revalidate category or group
const paths = CATEGORY_PATHS[category]
if (paths) {
paths.forEach(p => {
revalidatePath(p)
})
return NextResponse.json({
revalidated: true,
type: 'category',
target: category,
paths,
count: paths.length,
now: Date.now(),
})
} else {
return NextResponse.json(
{
message: 'Invalid category',
validCategories: Object.keys(CATEGORY_PATHS),
},
{ status: 400 }
)
}
} else {
// Revalidate all pages
ALL_REVALIDATION_PATHS.forEach(p => {
revalidatePath(p)
})
return NextResponse.json({
revalidated: true,
type: 'all',
count: ALL_REVALIDATION_PATHS.length,
now: Date.now(),
})
}
} catch (err) {
return NextResponse.json(
{
message: 'Error revalidating',
error: err instanceof Error ? err.message : 'Unknown error',
},
{ status: 500 }
)
}
}