Charlie Roffe

Principal Architect & Engineering Director · Cloud & AI

Madrid, Spain · LinkedIn · GitHub

ADR-027: Edge Hosting Topology — Firebase Hosting in Front of Cloud Run

Context

ADR-002 shipped everything — SPA assets and API — from one Cloud Run container, explicitly rejecting a split frontend. That aged badly on three fronts: every static asset request paid Cloud Run wake-ups and billed CPU to serve immutable hash-named files a CDN should serve from the edge; the custom domains need managed TLS, CDN caching, and edge security headers; yet two things genuinely need the origin — the HTML document (Express enriches index.html per-request for SEO and it must never be cached stale) and the streaming API (SSE, ADR-015). ADR-012/018/021 all reference Firebase Hosting as established fact; this ADR records the actual topology decision retroactively.

Decision

Put Firebase Hosting in front of each environment's Cloud Run service, splitting traffic by what it needs — edge for immutable assets, origin for everything dynamic.

1. Assets at the edge: each Hosting target (prod, dev) deploys dist minus index.html, with /assets/** cached immutable for a year — hash-named Vite chunks are safe to cache forever.
2. HTML from the origin: a catch-all rewrite forwards anything not matching a deployed static file — including the document request — to Cloud Run, where Express serves index.html dynamically with no stale caching.
3. API traffic deliberately bypasses the CDN: the Hosting CDN buffers responses, which breaks Server-Sent Events, so API calls go cross-origin straight to the environment's run.app origin (this is why ADR-018/019 treat the API as cross-origin). The /api/health status probe is the exception: it is fetched same-origin through the Hosting rewrite.
4. Security headers ship at the edge per target (the dev target adds noindex); the CSP therefore exists in three copies (Express + two Hosting targets) that must change together.
5. Atomicity via ADR-021: promoted Hosting assets are extracted from inside the cosign-verified container image.

Stale-chunk recovery pair: immutable assets plus always-fresh HTML mean a deploy strands open tabs holding old chunk references — the server answers unknown /assets/ paths with a 404 (never SPA-fallback HTML), and the client recovers from failed dynamic imports and preloads (lazyWithReload plus a vite:preloadError listener) by reloading once, with a cooldown against loops. Render errors are not auto-reloaded: they reach the ErrorBoundary, which offers a manual Reload button.

Amended 2026-10-30, DNS hardening: the zone is signed with DNSSEC (Cloudflare is both registrar and DNS host, so it publishes the DS record itself), and CAA records allow only Google Trust Services (pki.goog) and Let's Encrypt, the two authorities Firebase Hosting uses, to issue certificates for the domain. Records stay DNS only.

Consequences

Benefits:
* Edge latency and zero container wake for assets — the container is reserved for dynamic work; asset traffic costs nothing at rest.
* Streaming works: SSE flows unbuffered because the API path never crosses the CDN.
* Headers and caching policy are code: versioned in firebase.json, deployed atomically with the release (ADR-021), rehearsed on the dev target.

Trade-offs:
* Three CSP copies must be kept in sync manually — the known sharp edge of this topology (it silently broke the App Check rollout once, ADR-019).
* Cross-origin API plumbing: CORS, App Check token headers, and the hostname-to-origin map exist solely because of the CDN-buffering constraint.
* Client-side recovery logic is required for the stale-chunk window; small but load-bearing.

Alternatives considered

1. Everything from Cloud Run (ADR-002 topology): Rejected — every asset request pays container wake-up and billed CPU, with no edge caching or managed custom-domain TLS.
2. GCS bucket + Cloud CDN behind a Global Load Balancer: Rejected — the load balancer carries a fixed monthly charge, violating the zero-standby baseline (ADR-001) for a capability Firebase Hosting includes free.
3. Same-origin API through Hosting rewrites: Rejected — the CDN buffers rewritten responses and breaks SSE; the split-origin design is the direct consequence of measuring this.
4. A third-party edge (Cloudflare/Vercel) in front: Rejected — a new vendor and DNS dependency, while Firebase Hosting already integrates natively with Cloud Run rewrites and the App Check stack (ADR-019).

Architecture Decision Records