|
1
|
import { workerCompatibleFetch } from "docs-utils" |
|
2
|
import path from "path" |
|
3
|
import { fetchFromAssetsBinding } from "./fetch-from-assets-binding" |
|
4
|
|
|
5
|
async function fetchRawMdxFile( |
|
6
|
origin: string, |
|
7
|
basePath: string, |
|
8
|
slug: string[], |
|
9
|
fileName: string |
|
10
|
): Promise<string | null> { |
|
11
|
const isCloudflare = !!process.env.CLOUDFLARE_ENV |
|
12
|
const url = `${origin}${basePath}/raw-mdx/${[...slug, fileName].join("/")}` |
|
13
|
|
|
14
|
return ( |
|
15
|
(await fetchFromAssetsBinding(url)) ?? |
|
16
|
(await workerCompatibleFetch<string | null>({ |
|
17
|
url, |
|
18
|
responseTransformer: async (res) => { |
|
19
|
return res.ok ? res.text() : null |
|
20
|
}, |
|
21
|
fallbackAction: async () => { |
|
22
|
try { |
|
23
|
const { promises: fs } = await import("fs") |
|
24
|
return await fs.readFile( |
|
25
|
path.join(process.cwd(), "app", ...slug, fileName), |
|
26
|
"utf-8" |
|
27
|
) |
|
28
|
} catch { |
|
29
|
return null |
|
30
|
} |
|
31
|
}, |
|
32
|
useRemote: isCloudflare, |
|
33
|
})) |
|
34
|
) |
|
35
|
} |
|
36
|
|
|
37
|
export async function fetchRawMdx( |
|
38
|
origin: string, |
|
39
|
basePath: string, |
|
40
|
slug: string[] |
|
41
|
): Promise<{ content: string; isOverride: boolean } | null> { |
|
42
|
// An `_md-content.mdx` file overrides `page.mdx` if it exists. |
|
43
|
const overrideContent = await fetchRawMdxFile( |
|
44
|
origin, |
|
45
|
basePath, |
|
46
|
slug, |
|
47
|
"_md-content.mdx" |
|
48
|
) |
|
49
|
|
|
50
|
if (overrideContent) { |
|
51
|
return { content: overrideContent, isOverride: true } |
|
52
|
} |
|
53
|
|
|
54
|
const pageContent = await fetchRawMdxFile(origin, basePath, slug, "page.mdx") |
|
55
|
|
|
56
|
return pageContent ? { content: pageContent, isOverride: false } : null |
|
57
|
} |