Skip to content
File

Blob: src/worker/lib/host-match.ts

typescript25 lines
1export type SiteHostMatch =
2 | { kind: "subdomain"; slug: string; baseDomain: string }
3 | { kind: "apex"; baseDomain: string }
4 | { kind: "none" };
5 
6export function matchSiteHost(url: URL, env: Pick<Env, "PUBLISHED_SITE_DOMAIN">): SiteHostMatch {
7 const baseDomain = env.PUBLISHED_SITE_DOMAIN?.trim().toLowerCase();
8 if (!baseDomain) return { kind: "none" };
9 
10 const host = url.hostname.toLowerCase();
11 if (host === baseDomain) return { kind: "apex", baseDomain };
12 
13 const suffix = "." + baseDomain;
14 if (host.endsWith(suffix)) {
15 const slug = host.slice(0, host.length - suffix.length);
16 // Reject multi-label subdomains (e.g. foo.bar.bland.site). Sites carve out
17 // exactly one label under the base domain in v1.
18 if (slug.length > 0 && !slug.includes(".")) {
19 return { kind: "subdomain", slug, baseDomain };
20 }
21 }
22 
23 return { kind: "none" };
24}