29 lines
736 B
TypeScript
29 lines
736 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
const LOCALES = ["zh", "en"] as const;
|
|
const DEFAULT_LOCALE = "zh";
|
|
|
|
function proxy(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname;
|
|
|
|
// 检查路径是否已有 locale 前缀
|
|
const hasLocale = LOCALES.some(
|
|
(loc) => pathname === `/${loc}` || pathname.startsWith(`/${loc}/`)
|
|
);
|
|
|
|
if (hasLocale) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// 根路径或无效路径,重定向到默认 locale
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = `/${DEFAULT_LOCALE}${pathname === "/" ? "" : pathname}`;
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
export { proxy };
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api|trpc|_next|_vercel|.*\\..*).*)"],
|
|
};
|