Repository navigation
Expand file tree
/
Copy pathproxy.js
More file actions
82 lines (67 loc) · 2.71 KB
/
Copy pathproxy.js
File metadata and controls
82 lines (67 loc) · 2.71 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
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
import { isMarkdownPreferred } from 'fumadocs-core/negotiation';
import { NextResponse } from 'next/server';
import { i18n } from './lib/i18n';
const handleI18n = createI18nMiddleware(i18n);
const INTERNAL_PREFIXES = ['/_next', '/api'];
const MARKDOWN_SUFFIX_RE = /\.(md|mdx)$/i;
const STATIC_FILE_RE = /\.[^/]+$/;
function getLocaleFromPath(pathname) {
const segment = pathname.split('/')[1];
return i18n.languages.includes(segment) ? segment : null;
}
function isStaticAsset(pathname) {
if (pathname === '/llms.txt' || pathname === '/llms-full.txt') {
return false;
}
return STATIC_FILE_RE.test(pathname) && !MARKDOWN_SUFFIX_RE.test(pathname);
}
function stripLocale(pathname, lang) {
const prefix = `/${lang}`;
const stripped = pathname === prefix ? '/' : pathname.slice(prefix.length) || '/';
return stripped.startsWith('/') ? stripped : `/${stripped}`;
}
function toMarkdownPath(pathname, lang) {
const localizedPath = stripLocale(pathname, lang).replace(/\/$/, '');
return localizedPath && localizedPath !== '/'
? `/${lang}/llms.mdx${localizedPath}/content.md`
: `/${lang}/llms.mdx/content.md`;
}
function isInternalRoute(pathname) {
return INTERNAL_PREFIXES.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`));
}
// Build the options object for NextResponse.next() that injects x-current-path
// into the request headers, so server components (e.g. app/[lang]/(docs)/layout.jsx)
// can read the request pathname and filter the sidebar page tree by active edition.
// Next.js does not expose pathname to server components without this kind of header
// injection.
function withPathHeader(request) {
const headers = new Headers(request.headers);
headers.set('x-current-path', request.nextUrl.pathname);
return { request: { headers } };
}
export default function proxy(request) {
const { pathname } = request.nextUrl;
if (isInternalRoute(pathname) || isStaticAsset(pathname)) {
return NextResponse.next();
}
const lang = getLocaleFromPath(pathname);
if (!lang) {
return handleI18n(request);
}
if (
pathname === `/${lang}/llms.txt`
|| pathname === `/${lang}/llms-full.txt`
|| pathname.startsWith(`/${lang}/llms.mdx/`)
) {
return NextResponse.next(withPathHeader(request));
}
if (MARKDOWN_SUFFIX_RE.test(pathname)) {
const cleanPath = pathname.replace(MARKDOWN_SUFFIX_RE, '') || `/${lang}`;
return NextResponse.rewrite(new URL(toMarkdownPath(cleanPath, lang), request.nextUrl));
}
if (isMarkdownPreferred(request)) {
return NextResponse.rewrite(new URL(toMarkdownPath(pathname, lang), request.nextUrl));
}
return NextResponse.next(withPathHeader(request));
}