Fix maintenance redirects and admin host detection

This commit is contained in:
MOH
2026-03-15 04:33:07 +01:00
parent 5d3f77962a
commit 50c3f5cce9
4 changed files with 91 additions and 18 deletions
+54 -10
View File
@@ -14,11 +14,31 @@ import {
isLegacyAdminPath,
toInternalAdminPath,
} from "./lib/admin-routing";
import { getLocalizedPathWithDefault } from "./lib/locale";
import { getLocalizedPathWithDefault, stripLocalePrefix } from "./lib/locale";
const intlMiddleware = createMiddleware(routing);
const ADMIN_SESSION_COOKIE = "moh_admin_session";
async function getConfiguredDefaultLocale(request: NextRequest) {
type SiteRuntimeState = {
defaultLocale: (typeof routing.locales)[number];
maintenanceEnabled: boolean;
};
function isSupportedLocale(locale: string | undefined): locale is (typeof routing.locales)[number] {
return locale === "ar" || locale === "en" || locale === "de";
}
function getPathLocale(pathname: string, fallbackLocale: (typeof routing.locales)[number]) {
const locale = pathname.split("/")[1];
return isSupportedLocale(locale) ? locale : fallbackLocale;
}
function isComingSoonPath(pathname: string) {
return stripLocalePrefix(pathname) === "/coming-soon";
}
async function getSiteRuntimeState(request: NextRequest): Promise<SiteRuntimeState> {
try {
const response = await fetch(new URL("/api/site/default-locale", request.url), {
headers: {
@@ -28,16 +48,26 @@ async function getConfiguredDefaultLocale(request: NextRequest) {
});
if (!response.ok) {
return routing.defaultLocale;
return {
defaultLocale: routing.defaultLocale,
maintenanceEnabled: false,
};
}
const data = await response.json() as { defaultLocale?: string };
const data = await response.json() as {
defaultLocale?: string;
maintenanceEnabled?: boolean;
};
return data.defaultLocale === "ar" || data.defaultLocale === "en" || data.defaultLocale === "de"
? data.defaultLocale
: routing.defaultLocale;
return {
defaultLocale: isSupportedLocale(data.defaultLocale) ? data.defaultLocale : routing.defaultLocale,
maintenanceEnabled: data.maintenanceEnabled === true,
};
} catch {
return routing.defaultLocale;
return {
defaultLocale: routing.defaultLocale,
maintenanceEnabled: false,
};
}
}
@@ -88,7 +118,9 @@ export default async function middleware(request: NextRequest) {
const isDevelopmentAdminRequest =
process.env.NODE_ENV !== "production" && isDevelopmentAdminPath(pathname);
const hostname = getRequestHostname(
request.headers.get("host") ?? request.headers.get("x-forwarded-host") ?? request.nextUrl.hostname,
request.headers.get("x-forwarded-host"),
request.headers.get("host"),
request.nextUrl.hostname,
);
const isAdminRequest = isDevelopmentAdminRequest || isAdminHost(hostname);
const hasDedicatedAdminHostname = hasDedicatedAdminHost();
@@ -139,7 +171,19 @@ export default async function middleware(request: NextRequest) {
return response;
}
const configuredDefaultLocale = await getConfiguredDefaultLocale(request);
const siteRuntimeState = await getSiteRuntimeState(request);
const configuredDefaultLocale = siteRuntimeState.defaultLocale;
if (
siteRuntimeState.maintenanceEnabled &&
!request.cookies.has(ADMIN_SESSION_COOKIE) &&
!isComingSoonPath(pathname)
) {
const locale = getPathLocale(pathname, configuredDefaultLocale);
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = getLocalizedPathWithDefault(locale, "/coming-soon", configuredDefaultLocale);
return NextResponse.redirect(redirectUrl, 307);
}
if (
configuredDefaultLocale !== routing.defaultLocale &&