-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
69 lines (61 loc) · 2.85 KB
/
Copy pathproxy.ts
File metadata and controls
69 lines (61 loc) · 2.85 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
import createMiddleware from "next-intl/middleware";
import { NextRequest, NextResponse } from "next/server";
import { routing } from "./i18n/routing";
import { MEETING_URL_PREFIXES } from "./lib/meeting-slug";
const intl = createMiddleware(routing);
// "/{locale}/{...slug}.{json|txt}" — the public data-API URL grammar. Append
// `.json` or `.txt` to any meeting page URL to get the same content in that
// format. See app/api/data/[locale]/[...path]/route.ts for the actual handler.
const DATA_PATH = /^\/(ar|zh|en|fr|ru|es)\/(.+)\.(json|txt)$/;
// Locale-prefixed paths whose first sub-segment names a meeting. Used to
// decide whether to emit per-meeting `.txt`/`.json` Link headers.
const MEETING_PATH = new RegExp(
`^\\/(?:ar|zh|en|fr|ru|es)\\/(?:${MEETING_URL_PREFIXES.join("|")})\\/`,
);
export default function middleware(req: NextRequest) {
const m = req.nextUrl.pathname.match(DATA_PATH);
if (m) {
const [, locale, path, ext] = m;
// Format is encoded as the FIRST segment after the locale (text | json),
// not as a query param. `NextResponse.rewrite` doesn't reliably propagate
// added query params to the destination handler's `request.nextUrl`, but
// path segments survive intact via the route's params.
const fmt = ext === "txt" ? "text" : "json";
const url = req.nextUrl.clone();
url.pathname = `/api/data/${locale}/${fmt}/${path}`;
return NextResponse.rewrite(url);
}
const res = intl(req);
// Discovery hints for LLM crawlers. The llms.txt spec doesn't standardize
// a discovery mechanism; the de-facto convention (Mintlify-hosted docs
// like docs.anthropic.com, docs.cursor.com) is HTTP `Link` headers with
// `rel="llms-txt"` / `rel="llms-full-txt"`. Always emitted here; per-
// meeting `.txt` / `.json` alternates are appended only on meeting URLs.
//
// We set this in proxy.ts (not next.config.ts `headers()`) because next-
// intl's middleware response replaces the default Next response, so
// routing-layer `headers()` does not propagate to locale-routed pages.
res.headers.append(
"Link",
'</llms.txt>; rel="llms-txt", </llms-full.txt>; rel="llms-full-txt"',
);
const pathname = req.nextUrl.pathname;
if (MEETING_PATH.test(pathname)) {
res.headers.append(
"Link",
`<${pathname}.txt>; rel="alternate"; type="text/plain", <${pathname}.json>; rel="alternate"; type="application/json"`,
);
}
return res;
}
export const config = {
// Two distinct matchers:
// 1. The next-intl matcher: HTML pages without file extensions and
// excluding internal namespaces (api routes, _next, Sentry tunnel).
// 2. A second matcher for `.json` / `.txt` URLs under a locale prefix,
// so this middleware can rewrite them to the data handler.
matcher: [
"/((?!api|_next|_vercel|monitoring|.*\\..*).*)",
"/(ar|zh|en|fr|ru|es)/(.*\\.(?:json|txt))",
],
};