-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
134 lines (121 loc) · 3.8 KB
/
route.ts
File metadata and controls
134 lines (121 loc) · 3.8 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
import { NextResponse } from 'next/server';
import { nonEmpty } from '../../utils/config';
import {
revalidateFullSite,
revalidateAllFeeds,
revalidateAllGbfsFeeds,
revalidateAllGtfsFeeds,
revalidateAllGtfsRtFeeds,
revalidateSpecificFeeds,
} from '../../utils/revalidate-feeds';
type RevalidateTypes =
| 'full'
| 'all-feeds'
| 'all-gbfs-feeds'
| 'all-gtfs-rt-feeds'
| 'all-gtfs-feeds'
| 'specific-feeds';
interface RevalidateBody {
feedIds: string[]; // only for 'specific-feeds' revalidation type
type: RevalidateTypes;
}
const defaultRevalidateOptions: RevalidateBody = {
// By default it will revalidate nothing
type: 'specific-feeds',
feedIds: [],
};
/**
* GET handler for the Vercel cron job that revalidates all GBFS feed pages.
* Vercel automatically passes Authorization: Bearer <CRON_SECRET> with each invocation.
* Configured in vercel.json under "crons" for 4am UTC Monday-Saturday and 7am UTC Sunday.
*/
export async function GET(req: Request): Promise<NextResponse> {
const authHeader = req.headers.get('authorization');
const cronSecret = process.env.CRON_SECRET;
if (cronSecret == null) {
return NextResponse.json(
{ ok: false, error: 'Server misconfigured: CRON_SECRET missing' },
{ status: 500 },
);
}
if (authHeader !== `Bearer ${cronSecret}`) {
return NextResponse.json(
{ ok: false, error: 'Unauthorized' },
{ status: 401 },
);
}
try {
revalidateAllGbfsFeeds();
console.log(
'[cron] revalidate /api/revalidate: all-gbfs-feeds revalidation triggered',
);
return NextResponse.json({
ok: true,
message: 'All GBFS feeds revalidated successfully',
});
} catch (error) {
console.error(
'[cron] revalidate /api/revalidate: revalidation failed:',
error,
);
return NextResponse.json(
{ ok: false, error: 'Revalidation failed' },
{ status: 500 },
);
}
}
export async function POST(req: Request): Promise<NextResponse> {
const expectedSecret = nonEmpty(process.env.REVALIDATE_SECRET);
if (expectedSecret == null) {
return NextResponse.json(
{ ok: false, error: 'Server misconfigured: REVALIDATE_SECRET missing' },
{ status: 500 },
);
}
const providedSecret = req.headers.get('x-revalidate-secret');
if (providedSecret == null || providedSecret !== expectedSecret) {
return NextResponse.json(
{ ok: false, error: 'Unauthorized' },
{ status: 401 },
);
}
let payload: RevalidateBody = { ...defaultRevalidateOptions }; // default to full revalidation if body is missing/invalid
try {
const body = (await req.json()) as RevalidateBody;
payload = {
...defaultRevalidateOptions,
...body,
};
} catch (parseError) {
console.error(
'Failed to parse request body, falling back to defaults:',
parseError,
);
payload = { ...defaultRevalidateOptions };
}
// NOTE
// revalidatePath = triggers revalidation for entire page cache
// revalidateTag = triggers revalidation for API calls using `unstable_cache` with matching tags (e.g., feed-123, guest-feeds)
try {
if (payload.type === 'full') revalidateFullSite();
if (payload.type === 'all-feeds') revalidateAllFeeds();
if (payload.type === 'all-gbfs-feeds') revalidateAllGbfsFeeds();
if (payload.type === 'all-gtfs-feeds') revalidateAllGtfsFeeds();
if (payload.type === 'all-gtfs-rt-feeds') revalidateAllGtfsRtFeeds();
if (payload.type === 'specific-feeds')
revalidateSpecificFeeds(payload.feedIds);
return NextResponse.json({
ok: true,
message: 'Revalidation triggered successfully',
});
} catch (error) {
console.error('Revalidation failed:', error);
return NextResponse.json(
{
ok: false,
error: 'Failed to revalidate',
},
{ status: 500 },
);
}
}