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.
3.9 kB ยท 128 lines
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import { type Locale, defaultLocale, locales } from "../i18n/config";import { getValidSubdomain, stripHostPort } from "./domain";
export type RouteType = "hostname" | "pathname";
export interface ResolvedRoute { type: RouteType; prefix: string; locale: Locale; /** Whether the locale was explicitly present in the URL (vs. falling back to default) */ localeExplicit: boolean; /** The internal path to rewrite to: /{prefix}/{locale}/{rest} */ rewritePath: string;}
/** * Resolves the routing type, page slug, and locale from request context. * Pure function โ no side effects, no DB calls, no Next.js APIs. */export function resolveRoute({ host, urlHost, pathname,}: { /** x-forwarded-host header value */ host: string | null; /** req.nextUrl.host */ urlHost: string; /** req.nextUrl.pathname */ pathname: string;}): ResolvedRoute | null { const hostnames = host?.split(/[.:]/) ?? urlHost.split(/[.:]/); const pathnames = pathname.split("/");
// Prefer x-forwarded-host for custom-domain detection (behind reverse proxy/CDN, // req.nextUrl.host may be an internal host, not the real custom domain) const subdomain = getValidSubdomain(host ?? urlHost);
let prefix: string; let type: RouteType;
if ( hostnames.length > 2 && hostnames[0] !== "www" && !urlHost.endsWith(".vercel.app") ) { prefix = hostnames[0].toLowerCase(); type = "hostname"; } else { prefix = (pathnames[1] ?? "").toLowerCase(); type = "pathname"; }
if (subdomain !== null) { prefix = subdomain.toLowerCase(); // Host-keyed: the path carries no slug. Apex (`acme.com`) and `www.` custom // domains fail the label check above, and reading their first segment as the // slug would drop it (`/events/report/1` โ `/{slug}/{locale}/report/1`). type = "hostname"; }
// Root path on non-hostname type โ no page to resolve if (pathname === "/" && type !== "hostname" && subdomain === null) { return null; }
function isLocale(value: string | undefined): value is Locale { return ( typeof value === "string" && (locales as readonly string[]).includes(value) ); }
// Resolve locale based on routing type if (type === "hostname") { // apps/web proxies custom domains as `https://www.stpg.dev/{host}/{rest}` // and forwards the original host, so drop a leading segment that repeats // the prefix โ keeping it duplicates the domain inside the rewrite path. const hostSegment = stripHostPort(host ?? urlHost)?.toLowerCase(); const segments = pathnames[1]?.toLowerCase() === hostSegment ? pathnames.slice(1) : pathnames;
const firstSegment = segments[1]?.toLowerCase(); const locale: Locale = isLocale(firstSegment) ? firstSegment : defaultLocale; const hasLocale = isLocale(firstSegment); const rest = (hasLocale ? segments.slice(2) : segments.slice(1)) .filter(Boolean) .join("/");
return { type, prefix, locale, localeExplicit: hasLocale, rewritePath: `/${prefix}/${locale}${rest ? `/${rest}` : ""}`, }; }
// pathname type: locale is at index 2 (/{slug}/{locale}/...) const localeSegment = pathnames[2]?.toLowerCase(); const hasLocale = isLocale(localeSegment); const locale: Locale = hasLocale ? localeSegment : defaultLocale;
if (hasLocale) { // Already has locale in path โ rewrite path is the pathname as-is return { type, prefix, locale, localeExplicit: true, rewritePath: pathname, }; }
// No locale in path โ insert default locale after slug const rest = pathnames.slice(2).filter(Boolean).join("/"); return { type, prefix, locale, localeExplicit: false, rewritePath: `/${prefix}/${locale}${rest ? `/${rest}` : ""}`, };}