|
1
|
import { cache } from "react" |
|
2
|
import { ReferenceJSON } from "../../../components/ReferenceJSON" |
|
3
|
import { Metadata } from "next" |
|
4
|
import { workerCompatibleFetch } from "docs-utils" |
|
5
|
import path from "path" |
|
6
|
|
|
7
|
type PageProps = { |
|
8
|
params: Promise<{ |
|
9
|
slug: string[] |
|
10
|
}> |
|
11
|
} |
|
12
|
|
|
13
|
export default async function ReferencesPage(props: PageProps) { |
|
14
|
const params = await props.params |
|
15
|
const { slug } = params |
|
16
|
|
|
17
|
return <ReferenceJSON slug={slug} /> |
|
18
|
} |
|
19
|
|
|
20
|
export async function generateMetadata({ |
|
21
|
params, |
|
22
|
}: PageProps): Promise<Metadata> { |
|
23
|
const slug = (await params).slug |
|
24
|
const metadata: Metadata = {} |
|
25
|
|
|
26
|
const content = await loadReferencesFile(slug) |
|
27
|
|
|
28
|
if (!content) { |
|
29
|
return metadata |
|
30
|
} |
|
31
|
|
|
32
|
// The DocPage carries its title / keywords directly. |
|
33
|
try { |
|
34
|
const docPage = JSON.parse(content) |
|
35
|
metadata.title = docPage.title |
|
36
|
metadata.keywords = (docPage.frontmatter?.keywords || []) as string[] |
|
37
|
} catch { |
|
38
|
// ignore malformed JSON |
|
39
|
} |
|
40
|
|
|
41
|
return metadata |
|
42
|
} |
|
43
|
|
|
44
|
const loadReferencesFile = cache( |
|
45
|
async (slug: string[]): Promise<string | undefined> => { |
|
46
|
const monoRepoPath = path.resolve("..", "..", "..") |
|
47
|
|
|
48
|
const pathname = `/references/${slug.join("/")}` |
|
49
|
const filesMap = (await import("@/generated/files-map.mjs")).filesMap |
|
50
|
const fileDetails = filesMap.find((f) => f.pathname === pathname) |
|
51
|
if (!fileDetails) { |
|
52
|
return undefined |
|
53
|
} |
|
54
|
|
|
55
|
const r2Base = process.env.NEXT_PUBLIC_REFERENCES_R2_BASE_URL |
|
56
|
const content = await workerCompatibleFetch<string | null>({ |
|
57
|
url: `${r2Base}/references/${fileDetails.filePath.replace( |
|
58
|
/^.*\/references\//, |
|
59
|
"" |
|
60
|
)}`, |
|
61
|
responseTransformer: async (res) => { |
|
62
|
return res.ok ? res.text() : null |
|
63
|
}, |
|
64
|
fallbackAction: async () => { |
|
65
|
try { |
|
66
|
const { promises: fs } = await import("fs") |
|
67
|
const fullPath = path.join(monoRepoPath, fileDetails.filePath) |
|
68
|
return await fs.readFile(fullPath, "utf-8") |
|
69
|
} catch { |
|
70
|
return null |
|
71
|
} |
|
72
|
}, |
|
73
|
useRemote: !!r2Base, |
|
74
|
}) |
|
75
|
|
|
76
|
return content ?? undefined |
|
77
|
} |
|
78
|
) |