Skip to content

Commit 4b70311

Browse files
Copilothuangyiirene
andcommitted
Implement homepage auto-redirect based on browser language
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 41ff746 commit 4b70311

2 files changed

Lines changed: 28 additions & 65 deletions

File tree

packages/site/app/page.tsx

Lines changed: 0 additions & 63 deletions
This file was deleted.

packages/site/proxy.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
22
import { i18n } from '@/lib/i18n';
3+
import { NextResponse, NextRequest, NextFetchEvent } from 'next/server';
34

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+
}
532

633
export const config = {
734
// Matcher ignoring `/_next/` and `/api/`
8-
// You may need to adjust it to ignore static assets in `/public` folder
935
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|logo.svg).*)'],
1036
};

0 commit comments

Comments
 (0)