- Root / now uses next-intl locale detection to redirect based on Accept-Language header (English browser → /en, Serbian → /sr, etc.) - Old Serbian URLs (/products, /about, etc.) still redirect to /sr/* with 301 - English URLs (/en/*) remain unchanged
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import createMiddleware from "next-intl/middleware";
|
|
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
import { routing } from "./src/i18n/routing";
|
|
|
|
export default function middleware(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname;
|
|
|
|
const hasLocalePrefix = routing.locales.some(
|
|
(locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)
|
|
);
|
|
|
|
if (hasLocalePrefix) {
|
|
const intlMiddleware = createMiddleware({
|
|
...routing,
|
|
});
|
|
return intlMiddleware(request);
|
|
}
|
|
|
|
if (pathname === "/" || pathname === "") {
|
|
const intlMiddleware = createMiddleware({
|
|
...routing,
|
|
localeDetection: true,
|
|
});
|
|
return intlMiddleware(request);
|
|
}
|
|
|
|
const oldSerbianPaths = ["products", "about", "contact", "checkout"];
|
|
const isOldSerbianPath = oldSerbianPaths.some(
|
|
(path) => pathname === `/${path}` || pathname.startsWith(`/${path}/`)
|
|
);
|
|
|
|
if (isOldSerbianPath) {
|
|
const newPathname = `/sr${pathname}`;
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = newPathname;
|
|
return NextResponse.redirect(url, 301);
|
|
}
|
|
|
|
const intlMiddleware = createMiddleware({
|
|
...routing,
|
|
});
|
|
return intlMiddleware(request);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
"/",
|
|
"/(sr|en|de|fr)/:path*",
|
|
"/((?!api|_next|_vercel|.*\\..*).*)",
|
|
],
|
|
};
|