ADR-003 — Media on an R2 Custom Domain in the Site's Zone

By Engineering & Architecture Team•October 12, 2026•4 min read

Status: Accepted Date: 2026-12-05 Authors: Engineering & Architecture Team

Context

ADR-002 keeps media out of Git, in a Cloudflare R2 bucket with zero egress cost. This record decides the address the site’s pages use to load that media, alongside <domain> for Cloudflare Pages: how many certificates and security perimeters it needs, whether cross-origin (CORS) configuration is required, and what it costs per request. See BRD-003 and SR-003.

Decision Drivers (FRs & NFRs)

  • BR-1.1 / SR-003 — one perimeter and one certificate for pages and media.
  • BR-1.1 / SR-003, BRD-002 BR-1.3 — media is served from R2, never from Git or the build.
  • BR-2.1 / SR-003 — one firewall and rate-limit set covers pages and media.
  • BRD-001 BR-1.1 / SR-003 SR-F-03 — media is cached at the edge with no per-request cost.

Decision — Where media is addressed

Option 1 — A media domain in a separate zone

Separate endpoints for Pages and R2, with the media host outside the site’s zone.

Pros:

  • Simple edge setup: R2 custom domain with no routing code.

Cons:

  • Two zones, two certificates and two sets of security rules to keep in step.
  • Markdown image links are absolute URLs to another domain.

Option 2 — Path routing on the main domain (<domain>/assets/*)

Route media requests to R2 on the primary domain with an edge route.

Pros:

  • One hostname: root-relative image paths and one origin for pages and media.

Cons:

  • Transform Rules and Bulk Redirects cannot serve an R2 bucket on the same hostname, so a Worker (or Pages Function) with an R2 binding sits in the media path; every image request counts against the free daily Worker request allowance.
  • /assets/ is reserved for media and must never be produced by the site build.

Option 3 — assets.<domain> as an R2 custom domain in the site’s zone (CHOSEN)

The media bucket is attached to the site’s own zone as the custom domain assets.<domain>, with its public r2.dev address turned off, and pages load images from https://assets.<domain>/images/....

Pros:

  • No Worker and no per-request cost: R2 serves the object directly from the edge cache.
  • One zone, one perimeter: the host is in the same zone, so the same TLS settings, firewall rules and rate limit apply to pages and media.
  • One certificate: the zone’s universal certificate covers the apex and one subdomain level.
  • No CORS for plain images: <img> loads do not need CORS; nothing on the site reads image pixels from script.

Cons:

  • The media address is in the HTML. Each environment’s build holds its own assets.<domain>, so one build cannot serve every environment; ADR-005 checks the production build against the tested non-production build instead.
  • The Content-Security-Policy names the media host, computed per build by scripts/write-headers.mjs.
  • Markdown keeps /images/... paths; the build rewrites them to the media address, so links in the repository stay root-relative.

Final Decision

Serve media from assets.<domain> as an R2 custom domain in the site’s own zone (Option 3). It keeps one zone, one certificate and one set of security rules for pages and media, needs no CORS for images, and puts no Worker and no per-request cost in the media path. A media domain in another zone is rejected for its duplicated certificates and rules; path routing on the main domain for the Worker it needs and that Worker’s request cost. The accepted cost is a per-environment media address in each build and in its Content-Security-Policy.