-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathproxy.ts
More file actions
54 lines (43 loc) · 1.54 KB
/
Copy pathproxy.ts
File metadata and controls
54 lines (43 loc) · 1.54 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
52
53
54
import { isMarkdownPreferred, rewritePath } from "fumadocs-core/negotiation";
import { type NextRequest, NextResponse } from "next/server";
const { rewrite: rewriteLLM } = rewritePath("/*path", "/llms.mdx/*path");
const rewriteMarkdownPath = (pathname: string) =>
pathname === "/" || pathname === "/docs"
? "/llms.mdx"
: rewriteLLM(pathname);
const MDX_EXTENSION_PATTERN = /\.mdx?$/;
const rewriteInternally = (request: NextRequest, destination: string) =>
NextResponse.rewrite(new URL(destination, request.nextUrl));
const proxy = (request: NextRequest) => {
const pathname = request.nextUrl.pathname;
const isGeneratedMarkdownRoute =
pathname === "/llms.txt" ||
pathname === "/llms.mdx" ||
pathname.startsWith("/llms.mdx/") ||
pathname === "/sitemap.md";
if (isGeneratedMarkdownRoute) {
return NextResponse.next();
}
if (MDX_EXTENSION_PATTERN.test(pathname)) {
const stripped = pathname.replace(MDX_EXTENSION_PATTERN, "");
const result = rewriteMarkdownPath(stripped);
if (result) {
return rewriteInternally(request, result);
}
}
if (isMarkdownPreferred(request)) {
const result = rewriteMarkdownPath(pathname);
if (result) {
return rewriteInternally(request, result);
}
}
return NextResponse.next();
};
export const config = {
// Matcher ignoring `/_next/`, `/api/`, canonical schemas, static assets,
// favicon, sitemap, robots, etc.
matcher: [
"/((?!api|_next/static|_next/image|schemas/|favicon.ico|sitemap.xml|robots.txt).*)",
],
};
export default proxy;