36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
import { locales, fallbackLocale, resolveLocale } from "./src/config/i18n";
|
|
import type { Locale } from "./src/config/i18n";
|
|
|
|
const PUBLIC_FILE = /\.(.*)$/;
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
|
|
// Пропускаем API, статические ресурсы и файлы
|
|
if (pathname.startsWith("/api") || PUBLIC_FILE.test(pathname)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const segments = pathname.split("/").filter(Boolean);
|
|
const firstSegment = segments[0];
|
|
|
|
// Если локаль уже присутствует — пропускаем
|
|
if (locales.includes(firstSegment as Locale)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// Детект по Accept-Language -> fallback
|
|
const detected = resolveLocale(request.headers.get("accept-language"));
|
|
const locale = detected || fallbackLocale;
|
|
|
|
const redirectPath = ["", locale, ...segments].join("/");
|
|
const url = new URL(redirectPath || `/${locale}`, request.url);
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!_next/|favicon.ico).*)"],
|
|
};
|