Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.4 kB ยท 90 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import type { NextRequest } from "next/server";
// Custom-domain lookups exact-match page.customDomain, which is stored without a// port; an inbound host like "status.acme.com:8080" must be normalized first.export const stripHostPort = (host?: string | null) => host ? host.replace(/:\d+$/, "") : (host ?? null);
export const getValidSubdomain = (host?: string | null) => { let subdomain: string | null = null; if (!host && typeof window !== "undefined") { // On client side, get the host from window host = window.location.host; }
// Exclude localhost and IP addresses from being treated as subdomains if ( host?.match(/^(localhost|127\\.0\\.0\\.1|::1|\\d+\\.\\d+\\.\\d+\\.\\d+)/) ) { return null; }
// Handle subdomains of localhost (e.g., hello.localhost:3000) if (host?.match(/^([^.]+)\.localhost(:\d+)?$/)) { const match = host.match(/^([^.]+)\.localhost(:\d+)?$/); return match?.[1] || null; }
// we should improve here for custom vercel deploy page if (host?.includes(".") && !host.includes(".vercel.app")) { const candidate = host.split(".")[0]; if (candidate && !candidate.includes("www")) { // Valid candidate subdomain = candidate; } }
// In case the host is a custom domain if ( host && !( host?.includes("stpg.dev") || host?.includes("openstatus.dev") || host?.endsWith(".vercel.app") ) ) { subdomain = host; } return subdomain;};
export const getValidCustomDomain = (req: NextRequest | Request) => { const url = "nextUrl" in req ? req.nextUrl.clone() : new URL(req.url); const headers = req.headers; const host = headers.get("x-forwarded-host");
let prefix = ""; let type: "hostname" | "pathname";
const hostnames = host?.split(/[.:]/) ?? url.host.split(/[.:]/); const pathnames = url.pathname.split("/");
const subdomain = getValidSubdomain(url.host); console.log({ hostnames, pathnames, host, urlHost: url.host, subdomain, });
if ( hostnames.length > 2 && hostnames[0] !== "www" && !url.host.endsWith(".vercel.app") ) { prefix = hostnames[0].toLowerCase(); type = "hostname"; } else { prefix = pathnames[1].toLowerCase(); type = "pathname"; }
if (subdomain !== null) { prefix = subdomain.toLowerCase(); }
console.log({ type, prefix });
return { type, prefix };};