Add dedicated local admin domain routing
This commit is contained in:
+30
-5
@@ -4,8 +4,12 @@ import type { NextRequest } from "next/server";
|
||||
|
||||
import { routing } from "./i18n/routing";
|
||||
import {
|
||||
fromDevelopmentAdminPath,
|
||||
getAdminBaseUrl,
|
||||
getRequestHostname,
|
||||
isDevelopmentAdminPath,
|
||||
isAdminHost,
|
||||
hasDedicatedAdminHost,
|
||||
isInternalAdminPath,
|
||||
isLegacyAdminPath,
|
||||
toInternalAdminPath,
|
||||
@@ -53,15 +57,35 @@ function isAdminBasicAuthValid(request: NextRequest): boolean {
|
||||
|
||||
export default async function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
const isDevelopmentAdminRequest =
|
||||
process.env.NODE_ENV !== "production" && isDevelopmentAdminPath(pathname);
|
||||
const hostname = getRequestHostname(
|
||||
request.headers.get("x-forwarded-host") ?? request.headers.get("host") ?? request.nextUrl.hostname,
|
||||
request.headers.get("host") ?? request.headers.get("x-forwarded-host") ?? request.nextUrl.hostname,
|
||||
);
|
||||
const isAdminRequest = isAdminHost(hostname);
|
||||
const isAdminRequest = isDevelopmentAdminRequest || isAdminHost(hostname);
|
||||
const hasDedicatedAdminHostname = hasDedicatedAdminHost();
|
||||
const adminRobotsHeaders = {
|
||||
"X-Robots-Tag": "noindex, nofollow, noarchive, nosnippet, noimageindex",
|
||||
};
|
||||
|
||||
if (isInternalAdminPath(pathname) || isLegacyAdminPath(pathname)) {
|
||||
if (
|
||||
isDevelopmentAdminRequest &&
|
||||
hasDedicatedAdminHostname &&
|
||||
!isAdminHost(hostname)
|
||||
) {
|
||||
const redirectUrl = new URL(getAdminBaseUrl());
|
||||
redirectUrl.pathname = fromDevelopmentAdminPath(pathname);
|
||||
redirectUrl.search = request.nextUrl.search;
|
||||
return NextResponse.redirect(redirectUrl, 308);
|
||||
}
|
||||
|
||||
if (isLegacyAdminPath(pathname) && process.env.NODE_ENV === "production") {
|
||||
return new NextResponse("Not Found", {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
|
||||
if (isInternalAdminPath(pathname) && process.env.NODE_ENV === "production" && !isAdminRequest) {
|
||||
return new NextResponse("Not Found", {
|
||||
status: 404,
|
||||
});
|
||||
@@ -79,8 +103,9 @@ export default async function middleware(request: NextRequest) {
|
||||
}
|
||||
|
||||
const rewriteUrl = request.nextUrl.clone();
|
||||
rewriteUrl.pathname = toInternalAdminPath(pathname);
|
||||
|
||||
rewriteUrl.pathname = toInternalAdminPath(
|
||||
isDevelopmentAdminRequest ? fromDevelopmentAdminPath(pathname) : pathname,
|
||||
);
|
||||
const response = NextResponse.rewrite(rewriteUrl);
|
||||
response.headers.set("X-Robots-Tag", adminRobotsHeaders["X-Robots-Tag"]);
|
||||
return response;
|
||||
|
||||
Reference in New Issue
Block a user