-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathroute.ts
40 lines (35 loc) · 1.13 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
34
35
36
37
38
39
40
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
// This endpoint simulates an on demand revalidation request
export async function GET(request: NextRequest) {
let manifest: { preview: { previewModeId: string } };
// This fails at build time when next.js tries to evaluate the route
try {
const prerenderManifest = await import(
// @ts-expect-error
/* webpackIgnore: true */ "../../../../prerender-manifest.json",
{ with: { type: "json" } }
);
manifest = prerenderManifest.default;
} catch (e) {
console.error(e);
return new Response(null, { status: 500 });
}
const previewId = manifest.preview.previewModeId;
const host = request.headers.get("host") ?? "localhost:3001";
const result = await fetch(
`http${host?.includes("localhost") ? "" : "s"}://${host}/isr`,
{
headers: { "x-prerender-revalidate": previewId },
method: "HEAD",
},
);
return NextResponse.json({
status: 200,
body: {
result: result.ok,
cacheControl: result.headers.get("cache-control"),
},
});
}