30 lines
795 B
TypeScript
30 lines
795 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
const LOCALES = ["zh", "en"];
|
|
const DEFAULT_LOCALE = "zh";
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
|
|
const hasLocale = LOCALES.some(
|
|
(locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)
|
|
);
|
|
|
|
if (hasLocale) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
if (pathname.startsWith("/_next") || pathname.startsWith("/api") || pathname.includes(".")) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const locale = DEFAULT_LOCALE;
|
|
const newUrl = new URL(`/${locale}${pathname === "/" ? "" : pathname}`, request.url);
|
|
return NextResponse.redirect(newUrl);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!_next|api|favicon.ico|.*\\..*).*)"],
|
|
};
|