-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
51 lines (44 loc) · 1.59 KB
/
Copy pathproxy.ts
File metadata and controls
51 lines (44 loc) · 1.59 KB
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
41
42
43
44
45
46
47
48
49
50
51
import { isMarkdownPreferred, rewritePath } from "fumadocs-core/negotiation";
import { type NextRequest, NextResponse } from "next/server";
import { agentLinkHeader } from "@/lib/agent-links";
import { docsContentRoute, docsRoute, homeContentRoute } from "@/lib/shared";
const { rewrite: rewriteDocs } = rewritePath(
`${docsRoute}{/*path}`,
`${docsContentRoute}{/*path}/content.md`,
);
const { rewrite: rewriteSuffix } = rewritePath(
`${docsRoute}{/*path}.mdx`,
`${docsContentRoute}{/*path}/content.md`,
);
export default function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const suffixResult = rewriteSuffix(pathname);
if (suffixResult) {
const response = NextResponse.rewrite(
new URL(suffixResult, request.nextUrl),
);
response.headers.set(
"Link",
`<${pathname.slice(0, -".mdx".length)}>; rel="alternate"; type="text/html"`,
);
return response;
}
if (isMarkdownPreferred(request)) {
const target = pathname === "/" ? homeContentRoute : rewriteDocs(pathname);
if (target) {
const response = NextResponse.rewrite(new URL(target, request.nextUrl));
response.headers.append("Vary", "Accept");
return response;
}
}
// agentLinkHeader returns a value exactly for the negotiable HTML paths
// (/ and /docs/*), which therefore also need Vary: Accept.
const linkHeader = agentLinkHeader(pathname);
if (linkHeader) {
const response = NextResponse.next();
response.headers.set("Link", linkHeader);
response.headers.append("Vary", "Accept");
return response;
}
return NextResponse.next();
}