Skip to content

Commit 760c625

Browse files
authored
Merge pull request #47 from objectstack-ai/copilot/add-auto-redirect-for-language
2 parents 53ee013 + b9548d0 commit 760c625

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/site/app/page.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { redirect } from 'next/navigation';
2+
3+
/**
4+
* Root page - redirects are handled by proxy.ts middleware
5+
* This page should never actually render as the middleware intercepts and redirects
6+
* But Next.js requires a page component for the route to be recognized
7+
*/
8+
export default function RootPage() {
9+
// Fallback redirect if middleware didn't handle it
10+
redirect('/en/docs');
11+
}

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)