-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
38 lines (34 loc) · 1.89 KB
/
Copy pathproxy.ts
File metadata and controls
38 lines (34 loc) · 1.89 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
import { NextResponse, type NextFetchEvent, type NextRequest } from 'next/server'
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware'
import { isMarkdownPreferred } from 'fumadocs-core/negotiation'
import { i18n } from '@/lib/i18n'
const localize = createI18nMiddleware(i18n)
const prefixed = i18n.languages.filter((l) => l !== i18n.defaultLanguage).join('|')
// Guides sit at the site root, beside the API Reference at /api; paths here exclude the base path.
const GUIDE = new RegExp(`^(?:/(${prefixed}))?/(?!api(?:/|$))(.+?)(\\.md)?/?$`)
// A guide as Markdown: its URL with .md, or requested with `Accept: text/markdown`.
function markdownPath(request: NextRequest): string | undefined {
const m = GUIDE.exec(request.nextUrl.pathname)
if (!m || (!m[3] && !isMarkdownPreferred(request))) return undefined
return `/llms.mdx/${m[1] ?? i18n.defaultLanguage}/${m[2]}/content.md`
}
export default function proxy(request: NextRequest, event: NextFetchEvent) {
const markdown = markdownPath(request)
if (markdown) {
// A clone of nextUrl keeps the base path, which a plain URL would drop.
const url = request.nextUrl.clone()
url.pathname = markdown
return NextResponse.rewrite(url, { headers: { Vary: 'Accept' } })
}
return localize(request, event)
}
export const config = {
// Route handlers (search, the playground proxy, chat), build assets, images, icons, Markdown and the OpenAPI document are
// language-neutral; the API Reference beside them under /api is not. Directory names end in a slash so guide paths
// that merely start with them (/api-mcp/…) still get a language.
// Matchers get the base path prepended, and the second one needs a character after it, so the root is listed too.
matcher: [
'/',
'/((?!api/search|api/proxy|api/chat|_next/static/|_next/image|images/|llms\\.mdx/|llms\\.txt|openapi\\.json|favicon\\.ico|icon\\.svg|apple-icon\\.png).*)'
]
}