Serve the web's www host as a redirect to its bare host
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Prod's web Worker takes www.<domain> as a second Custom Domain, and the web answers any www. host with a 301 to the bare host (a 308 for a write), path and query kept.
authored by oscabriel · committed by oscabriel · parent 1ece295 · Browse files · 6 files·+60 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+60 added−8 removed
- tests:+29 added−1 removed
- other:+31 added−7 removed
Provenance
- Commit
05cecd1- Tree
feb5bf1- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
1ece295
6 files changed+60 added−8 removed
modified:apps/web/src/server/addresses.ts+21 added-2 removed
80 unmodified lines81828384858685868788899091929394959697989910010110210310410510610710810911011111211335 unmodified lines80 unmodified lines });
const withoutGit = (spelled: string) => spelled.replace(/\.git$/iu, "");
export const canonicalAddress = (request: Request): Option.Option<Response> => { if (request.method !== "GET" && request.method !== "HEAD") {// The web answers at its bare host only: a `www.` host is the same page there, and a write keeps its method with a 308.const bareHost = (url: URL, isRead: boolean): Option.Option<Response> => { if (!url.hostname.startsWith("www.")) { return Option.none(); }
const bare = new URL(url); bare.hostname = url.hostname.slice("www.".length);
return Option.some( isRead ? movedTo(bare.href) : new Response(null, { headers: { location: bare.href }, status: 308 }) );};
export const canonicalAddress = (request: Request): Option.Option<Response> => { const url = new URL(request.url); const isRead = request.method === "GET" || request.method === "HEAD"; const onBareHost = bareHost(url, isRead);
if (Option.isSome(onBareHost) || !isRead) { return onBareHost; }
const segments = segmentsOf(url.pathname); const [first] = segments;
if (first === undefined || isReserved(first)) {35 unmodified linesmodified:apps/web/test/legacy-redirect.test.ts+28 added
78 unmodified lines79 expect(edge.headers.get("cache-control")).toContain("public");80 });81 });82 83+const onWww = (path: string, method = "GET") =>84+ Option.map(85+ canonicalAddress(86+ new Request(`https://www.web-test.example${path}`, { method })87+ ),88+ (response) => [response.status, response.headers.get("location")]89+ );90+91+describe("an address on a www. host", () => {92+ it("is a 301 to the bare host, the path and the query kept", () => {93+ expect(onWww("/")).toStrictEqual(94+ Option.some([301, "https://web-test.example/"])95+ );96+ expect(onWww("/oscabriel/datekit/tree/main?x=1")).toStrictEqual(97+ Option.some([98+ 301,99+ "https://web-test.example/oscabriel/datekit/tree/main?x=1",100+ ])101+ );102+ });103+104+ it("is a 308 for a request that is not a read, so it keeps its method", () => {105+ expect(onWww("/oscabriel/datekit", "POST")).toStrictEqual(106+ Option.some([308, "https://web-test.example/oscabriel/datekit"])107+ );108+ });109+});110+111 describe("an address that needs no redirect", () => {112 it("goes on to the pages: a canonical name, a source path, and a word no project has", () => {113 expect(movedTo("/oscabriel/datekit/tree/main")).toStrictEqual(114 Option.none()31 unmodified linesmodified:apps/web/wrangler.jsonc+1 added-1 removed
51 unmodified lines52 "PUBLIC_PLATFORM_URL": "https://platform-demo.oscargabriel.workers.dev",53 },54 },55 // Served on the brand's own domain, never workers.dev: infra/deploy.ts56- // passes its Custom Domain and PUBLIC_PLATFORM_URL, which name the brand,56+ // passes its Custom Domains and PUBLIC_PLATFORM_URL, which name the brand,57 // so they live in infra/stages.ts. The URL here is a stand-in the58 // deploy's --var replaces, so the generated Env still types it a string.59 "prod": {60 "workers_dev": false,8 unmodified linesmodified:infra/deploy.ts+2 added-3 removed
65 unmodified lines66 // name them here, never from `wrangler.jsonc` (see `customHosts`).67 const hostArgs = (stage: Stage) =>68 Option.match(customHosts(stage), {69 onNone: () => ({ platform: [], web: [] }),70- onSome: ({ platform, web }) => ({70+ onSome: ({ platform, web, webAliases }) => ({71 platform: [72 "--domain",73 platform,74 "--var",75 `PUBLIC_WEB_URL:https://${web}`,76 ],77 web: [78- "--domain",79- web,78+ ...[web, ...webAliases].flatMap((host) => ["--domain", host]),79 "--var",80 `PUBLIC_PLATFORM_URL:https://${platform}`,81 ],82 }),211 unmodified linesmodified:infra/stages.ts+7 added-1 removed
30 unmodified lines31 32 export interface Hosts {33 readonly platform: string;34 readonly web: string;35+ // More Custom Domains on the web's Worker, each answered with a 301 to `web`.36+ readonly webAliases: readonly string[];37 }38 39 // A stage served on the brand's own domain, as Workers Custom Domains. The40 // names live here, not in `wrangler.jsonc`, so the brand stays in4 unmodified lines45 export const URL_FROM_DEPLOY = "https://from-infra-deploy.invalid";46 47 export const customHosts = (stage: Stage): Option.Option<Hosts> =>48 stage === "prod"47- ? Option.some({ platform: `git.${Brand.domain}`, web: Brand.domain })49+ ? Option.some({50+ platform: `git.${Brand.domain}`,51+ web: Brand.domain,52+ webAliases: [`www.${Brand.domain}`],53+ })54 : Option.none();55 56 export interface Resources {57 readonly platform: string;114 unmodified linesmodified:packages/config/test/deploy.test.ts+1 added-1 removed
462 unmodified lines463 ".$ bun run verify",464 "apps/sandbox$ wrangler deploy --env prod",465 `apps/platform$ varlock-wrangler deploy --env prod --domain ${git} --var PUBLIC_WEB_URL:https://${Brand.domain}`,466 "apps/web$ bun run build",467- `apps/web$ wrangler deploy --env prod --domain ${Brand.domain} --var PUBLIC_PLATFORM_URL:https://${git}`,467+ `apps/web$ wrangler deploy --env prod --domain ${Brand.domain} --domain www.${Brand.domain} --var PUBLIC_PLATFORM_URL:https://${git}`,468 ".$ bun run bootstrap --stage prod --check-triggers",469 ".$ bun scripts/smoke.ts --stage prod",470 ]);471 })24 unmodified lines