-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
26 lines (20 loc) · 841 Bytes
/
Copy pathproxy.ts
File metadata and controls
26 lines (20 loc) · 841 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
26
import { type NextRequest, NextResponse } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// If path ends in .md, serve the markdown route
if (pathname.endsWith(".md")) {
return NextResponse.rewrite(new URL("/md", request.url));
}
// Check Accept header — if text/markdown appears before text/html, serve markdown
const accept = request.headers.get("accept") ?? "";
const types = accept.split(",").map((t) => t.trim().split(";")[0].trim());
const mdIndex = types.indexOf("text/markdown");
const htmlIndex = types.indexOf("text/html");
if (mdIndex !== -1 && (htmlIndex === -1 || mdIndex < htmlIndex)) {
return NextResponse.rewrite(new URL("/md", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/", "/.md"],
};