Repository navigation
Expand file tree
/
Copy pathmiddleware.js
More file actions
51 lines (44 loc) · 2.18 KB
/
Copy pathmiddleware.js
File metadata and controls
51 lines (44 loc) · 2.18 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
import { next, rewrite } from '@vercel/functions';
// Vercel Routing Middleware: serves the markdown twin of a docs page (built next to it as /page.md)
// to AI agents that send `Accept: text/markdown`. The home page has no twin, so it answers with llms.txt.
export const config = {
// Pages (with or without .html) and .md twins only; skips assets and any other file
matcher: '/((?!assets/)(?!.*\\.(?!(?:md|html)$)[^/.]+$).*)'
};
// Suffix of page URLs on this site: '' with cleanUrls, '.html' without
const PAGE_SUFFIX = '';
const quality = (accept, type) => {
for (const part of accept.split(',')) {
const [media, ...params] = part.split(';').map(value => value.trim().toLowerCase());
if (media !== type) continue;
const q = params.find(param => param.startsWith('q='));
return q ? Number(q.slice(2)) || 0 : 1;
}
return 0;
};
const prefersMarkdown = accept => {
const markdown = quality(accept, 'text/markdown');
return markdown > 0 && markdown >= quality(accept, 'text/html');
};
export default async function middleware(request) {
const url = new URL(request.url);
if (url.pathname.endsWith('.md')) {
return next({ headers: { Link: `<${url.origin}${url.pathname.slice(0, -3)}${PAGE_SUFFIX}>; rel="canonical"`, Vary: 'Accept' } });
}
if (!prefersMarkdown(request.headers.get('accept') || '')) {
return next({ headers: { Vary: 'Accept' } });
}
const page = url.pathname.replace(/(\/index)?(\.html)?\/?$/, '');
const markdown = new URL(page ? `${page}.md` : '/llms.txt', url.origin);
// Not every page has a twin (archived versions, for one). Credentials are forwarded and redirects
// not followed, so a protected preview's login redirect does not pass for an existing file.
const headers = {};
for (const name of ['cookie', 'x-vercel-protection-bypass']) {
if (request.headers.has(name)) headers[name] = request.headers.get(name);
}
const twin = await fetch(markdown, { method: 'HEAD', headers, redirect: 'manual' }).catch(() => null);
if (!twin?.ok) {
return next({ headers: { Vary: 'Accept' } });
}
return rewrite(markdown, { headers: { Vary: 'Accept' } });
}