Unify dynamic locale routing
CI / quality (push) Waiting to run

This commit is contained in:
MOH
2026-03-15 05:12:33 +01:00
parent dfed5ec831
commit 27df4ad2fe
3 changed files with 42 additions and 46 deletions
+5 -39
View File
@@ -2,7 +2,7 @@ import createMiddleware from "next-intl/middleware";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { routing } from "./i18n/routing";
import { appLocales, createI18nRouting, routing } from "./i18n/routing";
import {
fromDevelopmentAdminPath,
getAdminBaseUrl,
@@ -16,19 +16,18 @@ import {
} from "./lib/admin-routing";
import { getLocalizedPathWithDefault, stripLocalePrefix } from "./lib/locale";
const intlMiddleware = createMiddleware(routing);
const ADMIN_SESSION_COOKIE = "moh_admin_session";
type SiteRuntimeState = {
defaultLocale: (typeof routing.locales)[number];
defaultLocale: (typeof appLocales)[number];
maintenanceEnabled: boolean;
};
function isSupportedLocale(locale: string | undefined): locale is (typeof routing.locales)[number] {
function isSupportedLocale(locale: string | undefined): locale is (typeof appLocales)[number] {
return locale === "ar" || locale === "en" || locale === "de";
}
function getPathLocale(pathname: string, fallbackLocale: (typeof routing.locales)[number]) {
function getPathLocale(pathname: string, fallbackLocale: (typeof appLocales)[number]) {
const locale = pathname.split("/")[1];
return isSupportedLocale(locale) ? locale : fallbackLocale;
@@ -71,10 +70,6 @@ async function getSiteRuntimeState(request: NextRequest): Promise<SiteRuntimeSta
}
}
function hasLocalePrefix(pathname: string) {
return routing.locales.some((locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`));
}
function getAdminBasicAuthUser(): string {
return process.env.ADMIN_BASIC_AUTH_USER ?? "";
}
@@ -173,6 +168,7 @@ export default async function middleware(request: NextRequest) {
const siteRuntimeState = await getSiteRuntimeState(request);
const configuredDefaultLocale = siteRuntimeState.defaultLocale;
const intlMiddleware = createMiddleware(createI18nRouting(configuredDefaultLocale));
if (
siteRuntimeState.maintenanceEnabled &&
@@ -185,36 +181,6 @@ export default async function middleware(request: NextRequest) {
return NextResponse.redirect(redirectUrl, 307);
}
if (
configuredDefaultLocale !== routing.defaultLocale &&
(pathname === "/de" || pathname.startsWith("/de/"))
) {
return NextResponse.next();
}
if (
configuredDefaultLocale !== routing.defaultLocale &&
!hasLocalePrefix(pathname)
) {
const rewriteUrl = request.nextUrl.clone();
rewriteUrl.pathname = getLocalizedPathWithDefault(
configuredDefaultLocale,
pathname,
routing.defaultLocale,
);
return NextResponse.rewrite(rewriteUrl);
}
if (
configuredDefaultLocale === routing.defaultLocale &&
(pathname === "/de" || pathname.startsWith("/de/"))
) {
const redirectUrl = request.nextUrl.clone();
const nextPath = pathname.slice(3) || "/";
redirectUrl.pathname = nextPath;
return NextResponse.redirect(redirectUrl, 308);
}
return intlMiddleware(request);
}