|
1
|
import { addExtraToMd, getCleanMd, workerCompatibleFetch } from "docs-utils" |
|
2
|
import { unstable_cache } from "next/cache" |
|
3
|
import { notFound } from "next/navigation" |
|
4
|
import { NextRequest, NextResponse } from "next/server" |
|
5
|
import path from "path" |
|
6
|
import { PostHog } from "posthog-node" |
|
7
|
import { |
|
8
|
addUrlToRelativeLink, |
|
9
|
crossProjectLinksPlugin, |
|
10
|
localLinksRehypePlugin, |
|
11
|
} from "remark-rehype-plugins" |
|
12
|
import type { Plugin } from "unified" |
|
13
|
import { fetchFromAssetsBinding } from "../../../utils/fetch-from-assets-binding" |
|
14
|
|
|
15
|
type Params = { |
|
16
|
params: Promise<{ slug?: string[] }> |
|
17
|
} |
|
18
|
|
|
19
|
export async function GET(req: NextRequest, { params }: Params) { |
|
20
|
const { slug: rawSlug } = await params |
|
21
|
const slug = rawSlug?.filter(Boolean) ?? [] |
|
22
|
const origin = process.env.NEXT_PUBLIC_BASE_URL || new URL(req.url).origin |
|
23
|
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "" |
|
24
|
|
|
25
|
const rawMdxUrl = `${origin}${basePath}/raw-mdx/${[...slug, "page.mdx"].join("/")}` |
|
26
|
const fileContent = |
|
27
|
(await fetchFromAssetsBinding(rawMdxUrl)) ?? |
|
28
|
(await workerCompatibleFetch<string | null>({ |
|
29
|
url: rawMdxUrl, |
|
30
|
responseTransformer: async (res) => { |
|
31
|
return res.ok ? res.text() : null |
|
32
|
}, |
|
33
|
fallbackAction: async () => { |
|
34
|
try { |
|
35
|
const { promises: fs } = await import("fs") |
|
36
|
return await fs.readFile( |
|
37
|
path.join(process.cwd(), "app", ...slug, "page.mdx"), |
|
38
|
"utf-8" |
|
39
|
) |
|
40
|
} catch { |
|
41
|
return null |
|
42
|
} |
|
43
|
}, |
|
44
|
useRemote: !!process.env.CLOUDFLARE_ENV, |
|
45
|
})) |
|
46
|
|
|
47
|
if (!fileContent) { |
|
48
|
return notFound() |
|
49
|
} |
|
50
|
|
|
51
|
const cleanMdContent = await getCleanMd_(fileContent, { |
|
52
|
before: [ |
|
53
|
[ |
|
54
|
crossProjectLinksPlugin, |
|
55
|
{ |
|
56
|
baseUrl: process.env.NEXT_PUBLIC_BASE_URL, |
|
57
|
projectUrls: { |
|
58
|
docs: { |
|
59
|
url: process.env.NEXT_PUBLIC_DOCS_URL, |
|
60
|
path: "", |
|
61
|
}, |
|
62
|
resources: { |
|
63
|
url: process.env.NEXT_PUBLIC_RESOURCES_URL, |
|
64
|
}, |
|
65
|
ui: { |
|
66
|
url: process.env.NEXT_PUBLIC_UI_URL, |
|
67
|
}, |
|
68
|
api: { |
|
69
|
url: process.env.NEXT_PUBLIC_API_URL, |
|
70
|
}, |
|
71
|
}, |
|
72
|
useBaseUrl: |
|
73
|
process.env.NODE_ENV === "production" || |
|
74
|
process.env.VERCEL_ENV === "production" || |
|
75
|
!!process.env.CLOUDFLARE_ENV, |
|
76
|
}, |
|
77
|
], |
|
78
|
[localLinksRehypePlugin], |
|
79
|
] as unknown as Plugin[], |
|
80
|
after: [ |
|
81
|
[addUrlToRelativeLink, { url: process.env.NEXT_PUBLIC_BASE_URL }], |
|
82
|
] as unknown as Plugin[], |
|
83
|
}) |
|
84
|
|
|
85
|
const acceptHeader = req.headers.get("accept") || "" |
|
86
|
if ( |
|
87
|
acceptHeader.includes("text/plain") || |
|
88
|
acceptHeader.includes("text/markdown") |
|
89
|
) { |
|
90
|
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { |
|
91
|
host: process.env.NEXT_PUBLIC_POSTHOG_HOST, |
|
92
|
}) |
|
93
|
|
|
94
|
const urlObj = new URL(req.url) |
|
95
|
const url = `${process.env.NEXT_PUBLIC_BASE_URL || ""}${process.env.NEXT_PUBLIC_BASE_PATH || ""}${urlObj.pathname}` |
|
96
|
|
|
97
|
client.capture({ |
|
98
|
distinctId: "anonymous", |
|
99
|
event: "md_content_requested_agents", |
|
100
|
properties: { |
|
101
|
$current_url: url, |
|
102
|
$raw_user_agent: req.headers.get("user-agent") || undefined, |
|
103
|
$ip: |
|
104
|
req.headers.get("cf-connecting-ip") || |
|
105
|
req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || |
|
106
|
undefined, |
|
107
|
}, |
|
108
|
}) |
|
109
|
|
|
110
|
await client.shutdown() |
|
111
|
} |
|
112
|
|
|
113
|
return new NextResponse( |
|
114
|
addExtraToMd(cleanMdContent, { |
|
115
|
baseUrl: process.env.NEXT_PUBLIC_BASE_URL || "", |
|
116
|
basePath: process.env.NEXT_PUBLIC_BASE_PATH || "", |
|
117
|
}), |
|
118
|
{ |
|
119
|
headers: { |
|
120
|
"Content-Type": "text/markdown", |
|
121
|
"Cache-Control": "public, max-age=3600, must-revalidate", |
|
122
|
}, |
|
123
|
status: 200, |
|
124
|
} |
|
125
|
) |
|
126
|
} |
|
127
|
|
|
128
|
const getCleanMd_ = unstable_cache( |
|
129
|
async (content: string, plugins?: { before?: Plugin[]; after?: Plugin[] }) => |
|
130
|
getCleanMd({ file: content, type: "content", plugins }), |
|
131
|
["clean-md"], |
|
132
|
{ |
|
133
|
revalidate: 3600, |
|
134
|
} |
|
135
|
) |