carlos / platform Public

spec: static sites set response headers by path with _headers (#195 item 2, #450), rev 1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev b0928a186288a3c4e9fc55924ee946e9bfdab625 parent a8b5260
1 file changed, +282 −0
  • docs/superpowers/specs/2026-10-29-static-headers-design.md +282 −0

CI cancelled — superseded by a newer push — run details

diff --git a/docs/superpowers/specs/2026-10-29-static-headers-design.md b/docs/superpowers/specs/2026-10-29-static-headers-design.md
new file mode 100644
index 000000000..e012fba8c
--- /dev/null
+++ b/docs/superpowers/specs/2026-10-29-static-headers-design.md
@@ -0,0 +1,282 @@
+# Static sites: response headers by path (`_headers`)
+
+Status: draft rev 1, 2026-10-29. Discussions #195 (item 2) and #450.
+Branch `static-headers`. Review budget: at most three Astra rounds; the
+review stops at the first round with no P1 (AGENTS.md, "Stopping rules").
+
+## 1. Why
+
+A static site (`carlos deploy --kind static`) cannot set a response header.
+Since #195 item 1 (merged b58e1275, in v0.23.0) every file the tree serves
+carries `Cache-Control: no-cache` and a content `ETag`
+(`internal/edge/static.go`, `ServeHTTP` and `serveFile`). That is right for
+pages. It is wrong for two kinds of file, and two sites have asked:
+
+- **Files that never change.** fiesta.rastrillo.org, the CDN for Rastrillo's
+ Fiesta design system, publishes each release at an exact path
+ (`/1.0.0/fiesta.css`) that never changes, and floats `/1/*` onto the newest
+ 1.x with a `_redirects` rewrite (`/1/* /1.0.0/:splat 200`). It wants
+ `public, max-age=31536000, immutable` on `/1.0.0/*` and a short `max-age`
+ on `/1/*`. Today every page view on every site that links Fiesta spends a
+ conditional request per file on bytes that cannot change. Dineraya asked
+ for the same for fingerprinted assets (#195 item 2).
+- **Files read from other origins.** Fiesta's `fetch()` of
+ `/1/i18n/<lang>.json`, `<script type="module">` and subresource integrity
+ (`integrity=` plus `crossorigin`) all need `Access-Control-Allow-Origin`.
+ Nothing in `internal/edge` sets it (#450). Fiesta 1.0.0 ships a `.js`
+ wrapper per language as a stopgap.
+
+Neither site is working around the gap, and neither should have to.
+
+## 2. What we build
+
+A file named `_headers` at the root of a static site's tree, in the format
+Netlify and Cloudflare Pages use, sets response headers by path:
+
+```text
+# Exact releases never change.
+/1.0.0/*
+ Cache-Control: public, max-age=31536000, immutable
+ Access-Control-Allow-Origin: *
+
+# The floating major moves to each new 1.x.
+/1/*
+ Cache-Control: public, max-age=300
+ Access-Control-Allow-Origin: *
+```
+
+One feature answers both discussions: #195 item 2 is a `Cache-Control` rule,
+#450 an `Access-Control-Allow-Origin` rule.
+
+### Goals
+
+- A site sets `Cache-Control` and a short list of cross-origin headers on the
+ paths it chooses.
+- A site that ships no `_headers` file is served exactly as today, header for
+ header.
+- A mistake is refused when the site is shipped, naming the line, as
+ `_redirects` mistakes are.
+
+### Non-goals
+
+- Headers on backend (instance) apps. An app sets its own.
+- CORS preflight (`OPTIONS`) answers. Fiesta's three uses are simple GETs:
+ `fetch()` with no custom headers, module scripts, and SRI with
+ `crossorigin="anonymous"`. None sends a preflight.
+- Netlify's `! Header` detach lines and Cloudflare's `:placeholder`
+ substitution in header values.
+- Changing `Content-Type`. The ETag is computed over the type
+ (`tagCache.hash`), so overriding it would need the tag to cover the
+ override; it waits for an asker.
+
+### Anti-goals
+
+- A `_headers` rule must never let one visitor's response reach another
+ through the edge's shared response cache (section 4.4).
+- No header the platform owns (`X-Carlos-*`, `Set-Cookie`, `Content-*`,
+ hop-by-hop headers, `ETag`, `Location`, `Vary`) can be set.
+
+## 3. The file
+
+### 3.1 Syntax
+
+As Netlify and Cloudflare Pages define it:
+
+- A line that starts at column 0 is a **path line**: a source pattern, and
+ nothing else on the line.
+- An indented line (one or more spaces or tabs) is a **header line**,
+ `Name: value`, and belongs to the nearest path line above it. A header
+ line before any path line is an error.
+- A blank line, or a line whose first non-space character is `#`, is
+ ignored. (`#` inside a header value is part of the value: CSP sources and
+ `Link` targets can carry one.)
+- A path line with no header lines under it is an error: it is almost
+ certainly an indentation mistake.
+
+The source pattern is exactly a `_redirects` source (`parseRedirectLine`'s
+source half in `internal/edge/redirects.go`): a path, or an http(s) URL whose
+host must match the request's; `:name` captures one segment; a final `*`
+matches the rest, `""` included, so `/1.0.0/*` also matches `/1.0.0`;
+case-sensitive; percent-decoding and dot-segment refusal as there. The
+builder factors the source parser and `redirectRule.match` out so both files
+share one implementation.
+
+The name is case-insensitive and stored canonical
+(`http.CanonicalHeaderKey`). The value is trimmed, must be non-empty, and
+must be visible ASCII and spaces only (no control characters).
+
+### 3.2 Which headers
+
+Version 1 allows these, and refuses every other name at ship:
+
+| Header | Value accepted |
+| --- | --- |
+| `Cache-Control` | a comma-separated directive list (`token` or `token=value`) |
+| `Access-Control-Allow-Origin` | `*` only |
+| `Access-Control-Expose-Headers` | a comma-separated list of header names |
+| `Cross-Origin-Resource-Policy` | `same-origin`, `same-site` or `cross-origin` |
+| `Timing-Allow-Origin` | `*` |
+
+`Access-Control-Allow-Origin` is `*` only because any other value is either
+one fixed origin (rarely what a CDN wants) or a list, which needs the edge to
+echo the request's `Origin` and send `Vary: Origin`. `*` also keeps the
+response sharable (`assetAdmit`, `internal/edge/assetadmit.go`, already
+treats `*` as shareable and anything else as not). `Cross-Origin-Resource-Policy`
+and `Timing-Allow-Origin` are on the list because a CDN's users need them: a
+page with `Cross-Origin-Embedder-Policy: require-corp` cannot load a
+cross-origin file without CORP. Widening the list later is additive.
+
+The refusal names the header and says it is not supported yet, so the next
+asker files a discussion rather than shipping around it.
+
+### 3.3 Limits
+
+At most 100 path lines, 2,048 characters a line and 256 KiB a file, as
+Cloudflare Pages bounds `_headers`. Unlike `_redirects`, which is scanned only
+when no file answers a path, these rules are matched on every file served,
+so the count is kept small.
+
+### 3.4 Where it is checked
+
+- **Ship.** `PackDir` (`internal/edge/static.go`) parses `_headers` the way it
+ parses `_redirects` and refuses the tree on any error, naming every bad
+ line. `carlos ship` and `carlos deploy` both pack through it.
+- **Edge.** The edge parses `_headers` once per version swap, beside
+ `loadRedirects` in `currentDir`, and carries the result in `servedTree`
+ with the redirect rules, so one request never pairs one version's files
+ with another's headers. A line it cannot use (only a tree packed by
+ something else can hold one) is logged and skipped; the rest apply.
+- The edge never serves the `_headers` file itself: it is configuration, as
+ `_redirects` is (`serveTree`'s `found`).
+
+## 4. How it serves
+
+### 4.1 Matching
+
+Rules match the **cleaned request path and host**, before any `_redirects`
+rewrite: `p` and `requestHost(r.Host)` as `serveTree` holds them on entry.
+The headers describe the URL the browser holds. With Fiesta's file, a request
+for `/1/fiesta.css` is rewritten to the file at `/1.0.0/fiesta.css` but takes
+`/1/*`'s `max-age=300`; matching after the rewrite would give the floating
+URL `immutable` and freeze it in browsers for a year. Netlify matches the
+same way.
+
+### 4.2 Several matching rules
+
+Every rule whose pattern matches applies. When more than one matching rule
+names the same header, the **last** one in the file wins, so a site can write
+a general rule first and a specific one under it:
+
+```text
+/*
+ Access-Control-Allow-Origin: *
+/1.0.0/*
+ Cache-Control: public, max-age=31536000, immutable
+```
+
+Netlify and Cloudflare join repeated values with a comma instead. That turns
+`no-cache` plus `max-age=31536000` into a contradiction and `*` plus `*` into
+an invalid `Access-Control-Allow-Origin`, so we do not. The docs say so.
+
+### 4.3 Which responses carry them
+
+The headers go on every answer that **serves a file from the tree under
+status 200**: the 200 itself, its 304 and 206, a `HEAD`, and a `_redirects`
+200 rewrite. They are set on the writer before `serveFile` calls
+`http.ServeContent`, so its 304 keeps them (net/http drops only
+`Content-Type`, `Content-Length` and `Content-Encoding` from a 304) and its
+errors are handled as net/http handles them.
+
+They do not go on redirects (a `_redirects` 3xx, the folder slash 301, the
+`/index.html` 301), on 404s (the tree's own and a `_redirects` 404 page), on
+the guard's refusals, or on 400/403/500. A 404 under `/1.0.0/*` marked
+`immutable` would pin a typo in every browser for a year.
+
+A matching `Cache-Control` rule **replaces** the default `no-cache` that
+`ServeHTTP` sets. Everything else about the answer is unchanged: the ETag,
+`X-Carlos-Version`, compression.
+
+### 4.4 The edge's shared response cache
+
+Today no static response is stored in the edge's shared LRU (`e.cache`,
+`internal/edge/cache.go`): `maybeStore` (`internal/edge/edge.go`) stores only
+a response with a positive `max-age`, and every static answer carries
+`no-cache`. A `_headers` `max-age` would change that, and the cache is the
+wrong place for these files:
+
+- a hit (`writeCached`) replays the stored 200 with no `Age` and no
+ conditional handling, so a browser revalidating with `If-None-Match`
+ would get the whole file instead of a 304, and a browser's freshness would
+ restart on a copy already up to five minutes old;
+- a hit replays the `X-Carlos-Version` frozen at store time, which
+ `carlos deploy -kind static` reads to know a deploy landed;
+- a static deploy does not purge it;
+- the tree is on this box's disk already, so a hit saves nothing worth
+ having.
+
+So static routes are taken out of the shared cache structurally: the
+`cacheable` computation in `Edge.ServeHTTP` gains `up.route.Kind != "static"`
+beside its existing `Kind != "status"` refusal. No static response is stored
+today, so this changes nothing for any site that exists.
+
+The other edge cache, the declared-asset cache (`assetcatch.go`), is already
+closed to static sites: the console refuses an `--assets` block on a static
+release (`internal/console/apiship_assets.go`, `apiShipAssetsStatic`).
+
+### 4.5 Compression
+
+Unchanged. The compressor (`internal/edge/compress.go`) sits above the
+handler, adds `Vary: Accept-Encoding` to what it compresses and honours a
+`no-transform` directive, which a site may now set.
+
+## 5. Compatibility
+
+- A tree with no `_headers` file: no change, header for header. A guard test
+ pins it.
+- A tree already deployed with a file named `_headers` (a site moved from
+ Netlify, say): after the roll its valid rules apply, its invalid lines are
+ logged and skipped, and the file is no longer served. That is the format's
+ meaning; we do not know of such a site.
+- No wire or storage shape changes. The file travels inside `site.tar.gz` as
+ any other file does.
+
+## 6. Docs
+
+`docs/site/cli.md` gains "Response headers for a static site" after
+"Redirects for a static site": the file, the five headers, last-rule-wins,
+which responses carry them, the limits, and what ship refuses. The prose goes
+through copy review before it lands.
+
+## 7. Testing
+
+Through the real `Edge.ServeHTTP` with a static route, not the handler alone
+(the production-wired rule):
+
+- Fiesta's file: `/1.0.0/fiesta.css` gets the immutable `Cache-Control` and
+ `*`; `/1/fiesta.css` (rewritten) gets `max-age=300`, not immutable.
+- A 304 and a `HEAD` carry the same headers as the 200.
+- A 404 and a redirect under a matching pattern carry none of them, and keep
+ `no-cache`.
+- Last-rule-wins on a repeated header.
+- A site with `max-age` is never stored in `e.cache`: a second request
+ reaches the handler.
+- No `_headers`: every header of a 200, 304, 404 and redirect is identical
+ to the same tree served before the change.
+- `/_headers` answers 404.
+- `PackDir` refuses each class of bad line and names it: unknown header,
+ bad value, header line before a path, path line with no headers, limits.
+
+## 8. Rollout
+
+No switch. A site gets this when it ships a `_headers` file to a box running
+the release that carries it, so the switch-on date is that release's roll
+(dev first, then flagship). First users: fiesta.rastrillo.org, then
+dineraya.com. The change touches `serveTree` and the `internal/edge`
+envelope, so the census needs a ruling before merge, as the folder-slash
+change did (a8b52609).
+
+## 9. Left to the builder
+
+Names, file layout (a `headers.go` beside `redirects.go` is the obvious
+place), how the shared source parser is factored, and the exact refusal
+wording (error fragments, outside copy review).