Add admin site settings sections and locale defaults
This commit is contained in:
+50
-1
@@ -14,9 +14,37 @@ import {
|
||||
isLegacyAdminPath,
|
||||
toInternalAdminPath,
|
||||
} from "./lib/admin-routing";
|
||||
import { getLocalizedPathWithDefault } from "./lib/locale";
|
||||
|
||||
const intlMiddleware = createMiddleware(routing);
|
||||
|
||||
async function getConfiguredDefaultLocale(request: NextRequest) {
|
||||
try {
|
||||
const response = await fetch(new URL("/api/site/default-locale", request.url), {
|
||||
headers: {
|
||||
"x-middleware-request": "1",
|
||||
},
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return routing.defaultLocale;
|
||||
}
|
||||
|
||||
const data = await response.json() as { defaultLocale?: string };
|
||||
|
||||
return data.defaultLocale === "ar" || data.defaultLocale === "en" || data.defaultLocale === "de"
|
||||
? data.defaultLocale
|
||||
: routing.defaultLocale;
|
||||
} catch {
|
||||
return routing.defaultLocale;
|
||||
}
|
||||
}
|
||||
|
||||
function hasLocalePrefix(pathname: string) {
|
||||
return routing.locales.some((locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`));
|
||||
}
|
||||
|
||||
function getAdminBasicAuthUser(): string {
|
||||
return process.env.ADMIN_BASIC_AUTH_USER ?? "";
|
||||
}
|
||||
@@ -111,7 +139,28 @@ export default async function middleware(request: NextRequest) {
|
||||
return response;
|
||||
}
|
||||
|
||||
if (pathname === "/de" || pathname.startsWith("/de/")) {
|
||||
const configuredDefaultLocale = await getConfiguredDefaultLocale(request);
|
||||
|
||||
if (
|
||||
configuredDefaultLocale !== routing.defaultLocale &&
|
||||
(pathname === "/de" || pathname.startsWith("/de/"))
|
||||
) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
if (
|
||||
configuredDefaultLocale !== routing.defaultLocale &&
|
||||
!hasLocalePrefix(pathname)
|
||||
) {
|
||||
const redirectUrl = request.nextUrl.clone();
|
||||
redirectUrl.pathname = getLocalizedPathWithDefault(configuredDefaultLocale, pathname, routing.defaultLocale);
|
||||
return NextResponse.redirect(redirectUrl, 307);
|
||||
}
|
||||
|
||||
if (
|
||||
configuredDefaultLocale === routing.defaultLocale &&
|
||||
(pathname === "/de" || pathname.startsWith("/de/"))
|
||||
) {
|
||||
const redirectUrl = request.nextUrl.clone();
|
||||
const nextPath = pathname.slice(3) || "/";
|
||||
redirectUrl.pathname = nextPath;
|
||||
|
||||
Reference in New Issue
Block a user