Skip to content

feat: inline site.notFound on the Cloudflare Worker (refs #309) - #384

Merged
jimhoyd merged 1 commit into
mainfrom
feat/cloudflare-not-found
Sep 21, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
feat/cloudflare-not-found

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

Closes the remaining gap from #309: build --target cloudflare refused site.notFound outright because that target has no static-asset binding. That page is small, singular and static, so the build now reads it and carries it inline in the Worker artifact as a respond route at /404.html, answering the same status, headers and method rules as self-hosted, Vercel and AWS.

Every other page/static/download route stays refused — those are open-ended, not one bounded page, and would need a real Workers Static Assets binding.

What changed

  • src/site.ts — inlineNotFound() reads the configured page, enforces a 64 KiB size cap and valid UTF-8, and rewrites the generated /404.html route into an ordinary respond route (no template, no request data, no per-request file read).
  • src/build-cloudflare.ts — calls inlineNotFound() after applySite(); the artifact records notFound: true when the fallback is present.
  • src/cloudflare.ts — an unmatched GET/HEAD is now answered from the inlined /404.html route with status 404 when artifact.notFound is set; every other method still gets the plain-text Not found.
  • test/site.test.ts — replaces the old "Cloudflare refuses site.notFound" assertion with coverage of the inline path (GET/HEAD/POST, direct /404.html access, shadowing by a declared route) and its bounds (64 KiB cap, invalid UTF-8 refused, other page routes still refused).
  • docs/CLOUDFLARE.md, docs/SITE.md, docs/OPEN-DECISIONS.md — document the decision, the size/encoding bounds, and the workerd verification. llms-full.txt regenerated (npm run docs:llms).

Options considered (recorded in docs/OPEN-DECISIONS.md)

Option Verdict
1. Inline the page as a string Chosen. Bounded (64 KiB, UTF-8), no new dependency, a small change confined to src/build-cloudflare.ts + src/site.ts.
2. Workers Static Assets binding (assets in wrangler config) Would need a new build-output shape and validation for a whole class of files — oversized for deciding one bounded page. Left as a future design if a route type ever needs to serve more than one small file on this target.
3. Keep refusing, document the respond/function-route alternative Left the issue's Cloudflare acceptance item unmet.

Verified on real workerd

Built examples/not-found for the cloudflare target and ran the artifact under wrangler dev --local in a scratch directory (no login/deploy/credentials), following the pattern in scripts/workerd-parity.ts (#381):

  • GET /nope?q=1 → 404, content-type: text/html; charset=utf-8, cache-control: no-store, x-content-type-options: nosniff, and the rest of the configured security headers; body is the configured page.
  • HEAD /nope → 404, empty body, correct content-length.
  • POST /nope → 404, plain-text Not found (unchanged from before this change).
  • GET /404.html directly → 200 with the page.
  • GET / → unaffected (200).

Checks run

  • npm ci
  • npm run check:docs (trust-model prose, version statements, local links, guidance claims, yaml-reference, llms-full, cookbook index, plugin distribution)
  • npm run verify (lint, typecheck, check, build, npm test — 676 passing, verify:workspaces)
  • Core package entry count: 430 of the 440-entry budget in scripts/package-audit.ts (not raised)

Open decisions

None new. docs/OPEN-DECISIONS.md records this as an accepted decision (inline over an assets binding) and notes that the assets-binding design should only be revisited if a future site convention or route type needs to serve more than one small file on Cloudflare.

Refs #309. Does not close it (leaving that to the reporter/maintainer to confirm). Siblings #383 and #330 are untouched — no changes to llms.txt/context/recipes/packages/ui.

🤖 Generated with Claude Code

build --target cloudflare no longer refuses site.notFound. The one
configured page is small, singular and static, so the build reads it
(64 KiB cap, must decode as UTF-8) and carries it inline in the
artifact as a `respond` route at /404.html, answering the same
status, headers and method rules as the self-hosted, Vercel and AWS
targets: 404 with the page for an unmatched GET/HEAD, security
headers, the plain-text `Not found` preserved for other methods, and
/404.html itself still reachable directly. Every other page/static/
download route stays refused, since those are open-ended rather than
one bounded page.

Verified against real workerd via `wrangler dev --local` in a scratch
directory: status 404, content-type, cache-control: no-store,
x-content-type-options: nosniff and the rest of the security headers
on GET, correct content-length with an empty body on HEAD, and POST
still answering the plain-text Not found.

- src/site.ts: inlineNotFound() reads and validates the page, rewrites
  the generated /404.html route to a respond route
- src/build-cloudflare.ts: calls inlineNotFound() after applySite();
  artifact.notFound: true records that the fallback is available
- src/cloudflare.ts: an unmatched GET/HEAD with artifact.notFound set
  is answered from the inlined /404.html route with status 404
- test/site.test.ts: replaces the old "refused" assertion with
  coverage of the inline path (GET/HEAD/POST, direct /404.html access,
  shadowing) and its bounds (64 KiB cap, invalid UTF-8, other page
  routes still refused)
- docs/CLOUDFLARE.md, docs/SITE.md, docs/OPEN-DECISIONS.md: document
  the decision and the workerd verification

Options considered (docs/OPEN-DECISIONS.md has the full writeup):
1. Inline the page as a string (chosen) — bounded, no new dependency,
   small change confined to src/build-cloudflare.ts + src/site.ts.
2. Workers Static Assets binding (`assets` in wrangler config) — would
   need new build output shape and validation for a whole class of
   files; oversized for one bounded page.
3. Keep refusing and document the respond/function-route alternative —
   left the issue's Cloudflare acceptance item unmet.

npm run check:docs, npm ci and npm run verify all pass. Core package
entry count is 430 of the 440-entry budget in scripts/package-audit.ts
(not raised).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jimhoyd
jimhoyd merged commit f48d6b5 into main Sep 21, 2026
20 checks passed
@jimhoyd
jimhoyd deleted the feat/cloudflare-not-found branch September 21, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant