Skip to content

Commit

Permalink
fix: 400 errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ar Rakin <rakinar2@gmail.com>
  • Loading branch information
virtual-designer authored Aug 28, 2024
1 parent d708252 commit 3f8cd2f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const httpStatusText: Record<number, string> = {
500: "Internal Server Error",
};

const FRONTEND_DOMAIN = process.env.NEXT_PUBLIC_FRONTEND_ROOT_DOMAIN?.replace(
const FRONTEND_DOMAIN = process.env.NEXT_PUBLIC_FRONTEND_DOMAIN?.replace(
/:\d+$/,
"",
);

const FRONTEND_ROOT_DOMAIN = process.env.NEXT_PUBLIC_FRONTEND_ROOT_DOMAIN?.replace(
/:\d+$/,
"",
);
Expand Down Expand Up @@ -82,14 +87,14 @@ export function middleware(request: NextRequest) {

if (
!hostname ||
(!hostname.endsWith("." + FRONTEND_DOMAIN) &&
hostname !== FRONTEND_DOMAIN)
(!hostname.endsWith("." + FRONTEND_ROOT_DOMAIN) &&
hostname !== FRONTEND_DOMAIN && hostname !== FRONTEND_ROOT_DOMAIN)
) {
return error(400, "The request hostname is not valid.");
}

if (hostname === FRONTEND_DOMAIN) {
requestHeaders.set("x-domain", FRONTEND_DOMAIN);
if (hostname === FRONTEND_DOMAIN || hostname === FRONTEND_ROOT_DOMAIN) {
requestHeaders.set("x-domain", FRONTEND_ROOT_DOMAIN);

return NextResponse.next({
request: {
Expand All @@ -98,8 +103,7 @@ export function middleware(request: NextRequest) {
});
}

const subdomain = hostname.replace("." + FRONTEND_DOMAIN, "").trim();

const subdomain = hostname.replace("." + FRONTEND_ROOT_DOMAIN, "").trim();
const subdomainConfig = subdomains[subdomain];

if (!subdomainConfig) {
Expand Down

0 comments on commit 3f8cd2f

Please sign in to comment.