File size: 1,272 Bytes
f4102c2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import { NextResponse, type NextRequest } from "next/server";
import { defaultLocale, locales } from "@/i18n/config";
const PUBLIC_FILE = /\.(.*)$/;
function pickLocale(request: NextRequest): string {
const cookie = request.cookies.get("locale")?.value;
if (cookie && (locales as readonly string[]).includes(cookie)) return cookie;
const header = request.headers.get("accept-language") ?? "";
for (const part of header.split(",")) {
const code = part.split(";")[0].trim().toLowerCase().split("-")[0];
if ((locales as readonly string[]).includes(code)) return code;
}
return defaultLocale;
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/api") ||
pathname === "/favicon.ico" ||
PUBLIC_FILE.test(pathname)
) {
return;
}
const hasLocale = (locales as readonly string[]).some(
(l) => pathname === `/${l}` || pathname.startsWith(`/${l}/`)
);
if (hasLocale) return;
const locale = pickLocale(request);
const url = request.nextUrl.clone();
url.pathname = `/${locale}${pathname === "/" ? "" : pathname}`;
return NextResponse.redirect(url);
}
export const config = {
matcher: ["/((?!_next|api|.*\\..*).*)"],
};
|