|
1
|
import { unstable_cache } from "next/cache" |
|
2
|
import path from "path" |
|
3
|
import { workerCompatibleFetch } from "docs-utils" |
|
4
|
import { loadReferenceFromBinding } from "../../../../utils/load-reference-from-binding" |
|
5
|
|
|
6
|
type GetRouteProps = { |
|
7
|
params: Promise<{ |
|
8
|
slug: string[] |
|
9
|
}> |
|
10
|
} |
|
11
|
|
|
12
|
export async function GET(request: Request, { params }: GetRouteProps) { |
|
13
|
const { slug } = await params |
|
14
|
const docPage = await loadReferencesFile(slug) |
|
15
|
|
|
16
|
if (!docPage) { |
|
17
|
return new Response( |
|
18
|
JSON.stringify({ |
|
19
|
error: { |
|
20
|
name: "NotFound", |
|
21
|
message: "Reference file not found", |
|
22
|
}, |
|
23
|
}), |
|
24
|
{ |
|
25
|
status: 404, |
|
26
|
headers: { |
|
27
|
"Content-Type": "application/json", |
|
28
|
}, |
|
29
|
} |
|
30
|
) |
|
31
|
} |
|
32
|
|
|
33
|
return new Response(docPage, { |
|
34
|
status: 200, |
|
35
|
headers: { |
|
36
|
"Content-Type": "application/json", |
|
37
|
// References only change on releases, so let browsers reuse the |
|
38
|
// payload briefly and CDNs cache it for an hour, serving stale |
|
39
|
// while revalidating in the background afterwards. |
|
40
|
"Cache-Control": |
|
41
|
"public, max-age=300, s-maxage=3600, stale-while-revalidate=86400", |
|
42
|
}, |
|
43
|
}) |
|
44
|
} |
|
45
|
|
|
46
|
/** |
|
47
|
* Reads a reference doc-model file (`page.json`) from R2 — via the bucket |
|
48
|
* binding, the public bucket URL, or the local filesystem in dev. |
|
49
|
*/ |
|
50
|
async function loadReferenceSource(filePath: string): Promise<string | null> { |
|
51
|
const r2Base = process.env.NEXT_PUBLIC_REFERENCES_R2_BASE_URL |
|
52
|
const monoRepoPath = path.resolve("..", "..", "..") |
|
53
|
const relPath = filePath.replace(/^.*\/references\//, "") |
|
54
|
const localPath = path.join(monoRepoPath, filePath) |
|
55
|
|
|
56
|
return ( |
|
57
|
(await loadReferenceFromBinding(`resources/references/${relPath}`)) ?? |
|
58
|
(await workerCompatibleFetch<string | null>({ |
|
59
|
url: `${r2Base}/references/${relPath}`, |
|
60
|
responseTransformer: async (res) => { |
|
61
|
return res.ok ? res.text() : null |
|
62
|
}, |
|
63
|
fallbackAction: async () => { |
|
64
|
try { |
|
65
|
const { promises: fs } = await import("fs") |
|
66
|
return await fs.readFile(localPath, "utf-8") |
|
67
|
} catch (e) { |
|
68
|
console.error(e) |
|
69
|
return null |
|
70
|
} |
|
71
|
}, |
|
72
|
useRemote: !!r2Base, |
|
73
|
})) |
|
74
|
) |
|
75
|
} |
|
76
|
|
|
77
|
/** |
|
78
|
* Loads a reference page as its serialized `DocPage` JSON string. The doc-model |
|
79
|
* is returned as-is: no MDX serialization, and no runtime link-fixing (links |
|
80
|
* are already resolved to final slugs at generation time). |
|
81
|
*/ |
|
82
|
async function loadReferencesFileUncached( |
|
83
|
slug: string[] |
|
84
|
): Promise<string | undefined> { |
|
85
|
const pathname = `/references/${slug.join("/")}` |
|
86
|
const filesMap = (await import("@/generated/files-map.mjs")).filesMap |
|
87
|
const fileDetails = filesMap.find((f) => f.pathname === pathname) |
|
88
|
if (!fileDetails) { |
|
89
|
return undefined |
|
90
|
} |
|
91
|
|
|
92
|
const content = await loadReferenceSource(fileDetails.filePath) |
|
93
|
return content ?? undefined |
|
94
|
} |
|
95
|
|
|
96
|
// The revalidation window bounds how long a stale entry is served when |
|
97
|
// references are re-uploaded to R2 without a redeploy (deploys already |
|
98
|
// invalidate the cache through the build ID in cache keys). |
|
99
|
const loadReferencesFile = unstable_cache( |
|
100
|
loadReferencesFileUncached, |
|
101
|
["references-file"], |
|
102
|
{ |
|
103
|
revalidate: 3600, |
|
104
|
} |
|
105
|
) |
|
106
|
|
|
107
|
export const dynamic = "force-static" |
|
108
|
export const revalidate = 3600 |