Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
100 lines (91 loc) · 3.22 KB
/
Copy pathproxy.ts
File metadata and controls
100 lines (91 loc) · 3.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {
type NextFetchEvent,
type NextRequest,
NextResponse,
} from "next/server";
import { negotiate, notAcceptableBody } from "@/lib/accept";
import {
DOCS_HOME,
FETCHER_USER_AGENT,
type MarkdownVia,
markdownLinkFor,
markdownPathFor,
markdownUrlPage,
VARIANT_HEADER,
} from "@/lib/agent-routing";
import { trackMarkdownRead } from "@/lib/server/markdown-reads";
const VARY = "Accept, User-Agent";
/**
* Markdown for agents at the same URLs people use. The home page and docs
* pages negotiate on Accept (q-values included) and answer with Markdown,
* HTML or 406. Elsewhere the negotiated variant is passed on as a request
* header, so a 404 comes back as Markdown to a client that asked for it (see
* the fallback rewrite in lib/agent-routing.ts).
*
* Every Markdown read is counted here (`markdown_read`), whichever way it came.
*/
export function proxy(request: NextRequest, event?: NextFetchEvent) {
// Only reads negotiate. Server Actions POST with their own Accept.
if (request.method !== "GET" && request.method !== "HEAD") {
return NextResponse.next();
}
const accept = request.headers.get("accept");
const userAgent = request.headers.get("user-agent") ?? "";
const variant = negotiate(accept, {
preferMarkdown: FETCHER_USER_AGENT.test(userAgent),
});
const { pathname } = request.nextUrl;
const markdownPath = markdownPathFor(pathname);
const countRead = (page: string, via: MarkdownVia) => {
const sent = trackMarkdownRead({ page, path: pathname, via, userAgent });
event?.waitUntil(sent);
};
if (!markdownPath) {
const page = markdownUrlPage(pathname);
if (page) countRead(page, "url");
return tagVariant(request, variant);
}
if (variant === "none") {
return new NextResponse(notAcceptableBody(accept ?? ""), {
status: 406,
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "no-store",
Vary: VARY,
},
});
}
if (variant === "markdown") {
// Markdown without the fetcher preference means the Accept header chose it.
countRead(
pathname.replace(/(.)\/+$/, "$1"),
negotiate(accept) === "markdown" ? "accept" : "user_agent",
);
const response = NextResponse.rewrite(new URL(markdownPath, request.url));
response.headers.set("Vary", VARY);
return response;
}
// `/docs` has no HTML page; people land on the introduction.
const response =
pathname === "/docs"
? NextResponse.redirect(new URL(DOCS_HOME, request.url), 308)
: NextResponse.next();
response.headers.set("Vary", VARY);
const link = markdownLinkFor(pathname);
if (link) response.headers.set("Link", link);
return response;
}
/** Pass the variant on, dropping any value the client sent itself. */
function tagVariant(request: NextRequest, variant: string) {
const headers = new Headers(request.headers);
if (variant === "markdown") headers.set(VARIANT_HEADER, "markdown");
else headers.delete(VARIANT_HEADER);
return NextResponse.next({ request: { headers } });
}
export const config = {
matcher: [
// Everything but build assets, the preview bundle, the JSON API, MCP,
// registry and images.
"/((?!_next/|_previews/|api/|mcp|r/|og/|embed/|favicon\\.ico).*)",
],
};