-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmiddleware.mjs
More file actions
88 lines (77 loc) · 2.34 KB
/
middleware.mjs
File metadata and controls
88 lines (77 loc) · 2.34 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
/* global URL, Response */
import { next } from "@vercel/edge";
const STATIC_EXTENSIONS =
/\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot|json|xml|txt|md|map)$/i;
// Known AI agent and bot User-Agent patterns
const AGENT_UA_PATTERNS = [
/\bClaudeBot\b/i,
/\bChatGPT-User\b/i,
/\bGPTBot\b/i,
/\bGoogle-Extended\b/i,
/\bPerplexityBot\b/i,
/\bCohere-AI\b/i,
/\bOAI-SearchBot\b/i,
/\bYouBot\b/i,
/\bAI2Bot\b/i,
/\bApplebot-Extended\b/i,
/\bMeta-ExternalAgent\b/i,
/\bMeta-ExternalFetcher\b/i,
/\bFirecrawl\b/i,
/\bJinaBot\b/i,
];
export const config = {
matcher: "/docs/:path*",
};
function isAgent(request) {
const accept = request.headers.get("accept") || "";
if (accept.includes("text/markdown")) {
return true;
}
const ua = request.headers.get("user-agent") || "";
return AGENT_UA_PATTERNS.some((pattern) => pattern.test(ua));
}
function rewriteToMarkdown(pathname, requestUrl) {
// The @signalwire/docusaurus-plugin-llms-txt generates .md files at the
// route path (e.g., build/overview.md). With baseUrl "/docs/", these are
// served at /docs/<path>.md on the deployed site.
//
// Rewrite HTML URL to its .md equivalent:
// /docs/overview/ -> /docs/overview.md
// /docs/cli/cp/ -> /docs/cli/cp.md
// /docs/ -> /docs/index.md
const subpath = pathname.replace(/^\/docs\/?/, "").replace(/\/+$/, "");
let mdPath;
if (!subpath) {
mdPath = "/docs/index.md";
} else {
mdPath = `/docs/${subpath}.md`;
}
return new Response(null, {
status: 307,
headers: {
Location: new URL(mdPath, requestUrl).toString(),
Vary: "Accept, User-Agent",
},
});
}
export default function middleware(request) {
const url = new URL(request.url);
const pathname = url.pathname;
// Skip static assets
if (STATIC_EXTENSIONS.test(pathname)) {
return next();
}
if (isAgent(request)) {
return rewriteToMarkdown(pathname, request.url);
}
// For normal HTML requests, add Link header pointing to llms.txt for discovery.
// Intentionally omit Vary: User-Agent here to avoid CDN cache fragmentation —
// Vercel Edge Middleware runs before the cache, so the redirect already handles
// agent requests before this branch is reached.
return next({
headers: {
Link: '</docs/llms.txt>; rel="alternate"; type="text/plain"',
Vary: "Accept",
},
});
}