-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
25 lines (20 loc) · 934 Bytes
/
Copy pathproxy.ts
File metadata and controls
25 lines (20 loc) · 934 Bytes
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
import { type NextRequest, NextResponse } from "next/server";
/**
* Content negotiation for the docs.
*
* An agent that asks for Markdown gets the raw file; a browser (which always
* sends `text/html`) keeps the rendered page. One URL serves both.
*/
export function proxy(request: NextRequest) {
const accept = request.headers.get("accept")?.toLowerCase() ?? "";
const wantsMarkdown = accept.includes("text/markdown") || accept.includes("text/plain");
if (!wantsMarkdown || accept.includes("text/html")) return NextResponse.next();
const { pathname } = request.nextUrl;
if (pathname.endsWith(".md")) return NextResponse.next();
const slug = pathname === "/docs" ? "index" : pathname.slice("/docs/".length);
if (!slug || slug.includes("/")) return NextResponse.next();
return NextResponse.rewrite(new URL(`/api/docs/${slug}`, request.url));
}
export const config = {
matcher: ["/docs", "/docs/:path*"],
};