32 lines
915 B
TypeScript
32 lines
915 B
TypeScript
|
|
import { NextResponse } from 'next/server';
|
||
|
|
import type { NextRequest } from 'next/server';
|
||
|
|
|
||
|
|
export function middleware(request: NextRequest) {
|
||
|
|
const pathname = request.nextUrl.pathname;
|
||
|
|
|
||
|
|
// Check if the pathname already has a locale
|
||
|
|
const pathnameHasLocale = /^\/(?:en|zh-TW|zh-CN)(?:\/|$)/.test(pathname);
|
||
|
|
|
||
|
|
// If it doesn't have a locale and it's not a static file or API route
|
||
|
|
if (
|
||
|
|
!pathnameHasLocale &&
|
||
|
|
!pathname.startsWith('/_next') &&
|
||
|
|
!pathname.startsWith('/api') &&
|
||
|
|
!pathname.includes('.')
|
||
|
|
) {
|
||
|
|
// Default to zh-CN locale
|
||
|
|
const url = request.nextUrl.clone();
|
||
|
|
url.pathname = `/zh-CN${pathname}`;
|
||
|
|
return NextResponse.redirect(url);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
export const config = {
|
||
|
|
matcher: [
|
||
|
|
// Skip all internal paths (_next)
|
||
|
|
'/((?!_next/static|_next/image|favicon.ico).*)',
|
||
|
|
],
|
||
|
|
};
|