-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
44 lines (37 loc) · 1.55 KB
/
middleware.ts
File metadata and controls
44 lines (37 loc) · 1.55 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
/**
* ObjectDocs
* Copyright (c) 2026-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
import { i18n } from '@/lib/i18n';
import { NextResponse, NextRequest, NextFetchEvent } from 'next/server';
// Create fumadocs middleware
const fumadocsMiddleware = createI18nMiddleware(i18n);
export default function middleware(request: NextRequest, event: NextFetchEvent) {
const path = request.nextUrl.pathname;
// Handle root path separately with custom language detection
if (path === '/') {
const acceptLanguage = request.headers.get('accept-language') || '';
// Simple language detection: check if zh is in Accept-Language
if (acceptLanguage.toLowerCase().includes('zh')) {
// Redirect to Chinese docs
const url = request.nextUrl.clone();
url.pathname = '/cn/docs';
return NextResponse.redirect(url);
} else {
// Redirect to default language (English)
const url = request.nextUrl.clone();
url.pathname = '/en/docs';
return NextResponse.redirect(url);
}
}
// For all other paths, pass through to fumadocs middleware
return fumadocsMiddleware(request, event);
}
export const config = {
// Matcher ignoring `/_next/`, `/api/`, LLM routes, OG image routes, and SEO files
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|logo.svg|llms\\.txt|llms-full\\.txt|llms\\.mdx|og/|sitemap\\.xml|robots\\.txt).*)'],
};