Merge dev into master: resolve middleware conflict (use dev version with full locale detection)
Some checks failed
Build and Deploy / build (push) Has been cancelled

This commit is contained in:
Unchained
2026-03-24 14:08:17 +02:00
31 changed files with 587 additions and 162 deletions

View File

@@ -1,40 +1,45 @@
import createMiddleware from "next-intl/middleware";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { routing } from "./src/i18n/routing";
import { SUPPORTED_LOCALES, DEFAULT_LOCALE, LOCALE_COOKIE, getPathWithoutLocale, buildLocalePath, isValidLocale } from "@/lib/i18n/locales";
import type { Locale } from "@/lib/i18n/locales";
const oldSerbianPaths = ["", "products", "about", "contact", "checkout"];
const OLD_SERBIAN_PATHS = ["products", "about", "contact", "checkout"];
function detectLocale(cookieLocale: string | undefined, acceptLanguage: string): Locale {
if (cookieLocale && isValidLocale(cookieLocale)) {
return cookieLocale;
}
if (acceptLanguage.includes("en")) {
return "en";
}
return DEFAULT_LOCALE;
}
export default function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const cookieLocale = request.cookies.get(LOCALE_COOKIE)?.value;
const acceptLanguage = request.headers.get("accept-language") || "";
const isOldSerbianPath = oldSerbianPaths.some((path) => {
if (path === "") {
return pathname === "/";
}
return pathname === `/${path}` || pathname.startsWith(`/${path}/`);
});
const hasLocalePrefix = routing.locales.some(
(locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)
);
if (isOldSerbianPath && !hasLocalePrefix) {
const newPathname = pathname === "/"
? "/sr"
: `/sr${pathname}`;
if (pathname === "/" || pathname === "") {
const locale = detectLocale(cookieLocale, acceptLanguage);
const url = request.nextUrl.clone();
url.pathname = newPathname;
url.pathname = buildLocalePath(locale, "/");
return NextResponse.redirect(url, 301);
}
const intlMiddleware = createMiddleware({
...routing,
});
const isOldSerbianPath = OLD_SERBIAN_PATHS.some(
(path) => pathname === `/${path}` || pathname.startsWith(`/${path}/`)
);
return intlMiddleware(request);
if (isOldSerbianPath) {
const locale = detectLocale(cookieLocale, acceptLanguage);
const newPath = buildLocalePath(locale, pathname);
const url = request.nextUrl.clone();
url.pathname = newPath;
return NextResponse.redirect(url, 301);
}
return NextResponse.next();
}
export const config = {
@@ -43,4 +48,4 @@ export const config = {
"/(sr|en|de|fr)/:path*",
"/((?!api|_next|_vercel|.*\\..*).*)",
],
};
};