Files
netrunner-landing/proxy.ts
T
2026-04-19 16:05:05 +10:00

42 lines
1.2 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { match as matchLocale } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
const locales = ["en", "ru"];
const defaultLocale = "en";
function getLocale(request: NextRequest): string {
const negotiatorHeaders: Record<string, string> = {};
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
const languages = new Negotiator({ headers: negotiatorHeaders }).languages();
try {
return matchLocale(languages, locales, defaultLocale);
} catch (e) {
return defaultLocale;
}
}
// Изменили название с middleware на proxy и добавили export
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
);
if (pathnameHasLocale) return;
const locale = getLocale(request);
request.nextUrl.pathname = `/${locale}${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
// Конфиг остается прежним
export const config = {
matcher: ["/((?!_next|api|favicon.ico|logo.svg|.*\\..*).*)"],
};