Openstatus www.openstatus.dev

trying redirect

+35 -1
+35 -1
apps/status-page/src/middleware.ts
··· 17 17 const hostnames = host?.split(/[.:]/) ?? url.host.split(/[.:]/); 18 18 const pathnames = url.pathname.split("/"); 19 19 20 - console.log({ hostnames, pathnames, host, urlHost: url.host }); 20 + const subdomain = getValidSubdomain(url.host); 21 + console.log({ 22 + hostnames, 23 + pathnames, 24 + host, 25 + urlHost: url.host, 26 + subdomain, 27 + }); 21 28 22 29 if ( 23 30 hostnames.length > 2 && ··· 29 36 } else { 30 37 prefix = pathnames[1].toLowerCase(); 31 38 type = "pathname"; 39 + } 40 + 41 + if (subdomain !== null) { 42 + prefix = subdomain.toLowerCase(); 32 43 } 33 44 34 45 console.log({ pathname: url.pathname, type, prefix }); ··· 131 142 "/((?!api|assets|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)", 132 143 ], 133 144 }; 145 + 146 + export const getValidSubdomain = (host?: string | null) => { 147 + let subdomain: string | null = null; 148 + if (!host && typeof window !== "undefined") { 149 + // On client side, get the host from window 150 + // biome-ignore lint: to fix later 151 + host = window.location.host; 152 + } 153 + // we should improve here for custom vercel deploy page 154 + if (host?.includes(".") && !host.includes(".vercel.app")) { 155 + const candidate = host.split(".")[0]; 156 + if (candidate && !candidate.includes("www")) { 157 + // Valid candidate 158 + subdomain = candidate; 159 + } 160 + } 161 + 162 + // In case the host is a custom domain 163 + if (host && !(host?.includes("stpg.dev") || host?.endsWith(".vercel.app"))) { 164 + subdomain = host; 165 + } 166 + return subdomain; 167 + };