-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
27 lines (24 loc) · 1.03 KB
/
Copy pathproxy.ts
File metadata and controls
27 lines (24 loc) · 1.03 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
import { NextResponse, type NextRequest } from 'next/server';
// Markdown-for-Agents negotiation. When a GET request carries
// `Accept: text/markdown`, rewrite to /index.md so the caller gets the
// markdown rendering instead of the HTML shell.
//
// Previously attempted via vercel.json `has` rewrites on the static
// export, but Vercel's edge cache served the HTML without re-evaluating
// the header on subsequent requests. The proxy (formerly middleware)
// runs per-request and sidesteps that.
function prefersMarkdown(accept: string | null): boolean {
if (!accept) return false;
return /(^|,)\s*text\/markdown\b/i.test(accept);
}
export function proxy(req: NextRequest) {
if (req.method === 'GET' && prefersMarkdown(req.headers.get('accept'))) {
const target = new URL('/index.md', req.nextUrl.origin);
return NextResponse.rewrite(target);
}
return NextResponse.next();
}
export const config = {
// Skip static asset paths and the markdown file itself to avoid loops.
matcher: ['/((?!_next|index\\.md|.*\\..*).*)'],
};