|
1 | 1 | import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware'; |
2 | 2 | import { i18n } from '@/lib/i18n'; |
| 3 | +import { NextResponse, NextRequest, NextFetchEvent } from 'next/server'; |
3 | 4 |
|
4 | | -export default createI18nMiddleware(i18n); |
| 5 | +// Create fumadocs middleware |
| 6 | +const fumadocsMiddleware = createI18nMiddleware(i18n); |
| 7 | + |
| 8 | +export default function proxy(request: NextRequest, event: NextFetchEvent) { |
| 9 | + const path = request.nextUrl.pathname; |
| 10 | + |
| 11 | + // Handle root path separately with custom language detection |
| 12 | + if (path === '/') { |
| 13 | + const acceptLanguage = request.headers.get('accept-language') || ''; |
| 14 | + |
| 15 | + // Simple language detection: check if zh is in Accept-Language |
| 16 | + if (acceptLanguage.toLowerCase().includes('zh')) { |
| 17 | + // Redirect to Chinese docs |
| 18 | + const url = request.nextUrl.clone(); |
| 19 | + url.pathname = '/cn/docs'; |
| 20 | + return NextResponse.redirect(url); |
| 21 | + } else { |
| 22 | + // Redirect to default language (English) |
| 23 | + const url = request.nextUrl.clone(); |
| 24 | + url.pathname = '/en/docs'; |
| 25 | + return NextResponse.redirect(url); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // For all other paths, pass through to fumadocs middleware |
| 30 | + return fumadocsMiddleware(request, event); |
| 31 | +} |
5 | 32 |
|
6 | 33 | export const config = { |
7 | 34 | // Matcher ignoring `/_next/` and `/api/` |
8 | | - // You may need to adjust it to ignore static assets in `/public` folder |
9 | 35 | matcher: ['/((?!api|_next/static|_next/image|favicon.ico|logo.svg).*)'], |
10 | 36 | }; |
0 commit comments