-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.ts
33 lines (28 loc) · 1.08 KB
/
route.ts
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
import { revalidateTag } from 'next/cache';
/**
* Handle a POST request to revalidate the document cache.
*
* This is a simple implementation that invalidates all document cache entries of the
* fetch cache on any published change in Neos.
*
* If suitable, individual route paths could be invalidated based on the payload sent by Neos
* by using the `revalidateTag` function with the appropriate `document:[routePath]` tag.
*
* Since Next.js will only flush the cache entries the actual re-rendering will be performed
* lazy on the next request to the affected routes.
*/
export async function POST(req: Request) {
// Check for secret to confirm this is a valid request
if (req.headers.get('authorization') !== `Bearer ${process.env.REVALIDATE_TOKEN}`) {
console.warn('Invalid token for revalidate request');
return Response.json(
{ error: 'Invalid token', revalidated: false },
{
status: 401,
}
);
}
console.info('/api/revalidate', 'Revalidating document cache');
revalidateTag('document');
return Response.json({ revalidated: true });
}