Skip to content

perf(web): hold pages until their data can next change - #127

Merged
Glory42 merged 2 commits into
masterfrom
fix/page-cpu-budget
Nov 18, 2026
Merged

Glory42 merged 2 commits into
masterfrom
fix/page-cpu-budget

Conversation

@Glory42

@Glory42 Glory42 commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Summary

Workers Free stops a request at 10 ms of CPU. A page rendered from scratch takes 15–100 ms, so only edge-cache hits stay under it. On 2026-11-17 Meta's meta-webindexer walked ~11k distinct URLs and about 1,100 renders were cut off. That crawler is now blocked by a zone WAF rule (already live, documented in docs/deployment.md); this PR makes the cache hold far more of what's left.

 middleware
-  servePageCached(key = path + params + sidebar cookie)
+  trackRenderHealth                     # AsyncLocalStorage per render
+    servePageCached(key = path + params + build id)
       hit  → serve (2–9 ms CPU)
       miss → render
-        store if 200, for 5 min (race weekend) / 1 h
+        store if 200 or 404 and no API outage seen,
+          for 10 min inside a write window,
+          else until the next window or 00:00 UTC
  • Sidebar: a head script applies the sidebar cookie before first paint and on each client-side navigation, so one copy serves everyone.
  • API answers (apiCache.ts) are held as long as pages and keyed by build too, so a deploy starts fresh.
  • Missing records: unknown ids answer 404 (and are kept). A missing record after an API outage answers 503 and is never kept, where a "Couldn't load" page used to stick for an hour.
  • api.ts reports failures through apiFailure.ts, so browser islands never import node:async_hooks.

Evidence

  • Before (production, 11-17): 1,086 exceededResources. Locally, uncached renders take 15–100 ms of CPU, against 2–9 ms for a cache hit.
    After (production build under local workerd, real API):
    • /drivers/999991 → 404, and the repeat comes from cache in 9 ms.
    • /drivers with a collapsed-sidebar cookie is served the expanded visitor's copy in 9 ms.
    • Built against an unreachable API: /drivers → 503 on every request and the home page shows its fallback. 0 cache entries written, against 18 in the healthy run (all keyed __build=).
  • bun test/vitest: 376 passed. Playwright smoke (sidebar, mobile, drivers, races): 26 passed, including "a collapsed sidebar stays collapsed across navigation and reloads".

Merge Danger

Door: two-way

Revert and redeploy. The build id in every key means old copies are never served by the next build.

Blast Radius: freshness

A manual Neon write outside a write window now shows on the site only after a redeploy or at 00:00 UTC (was ≤ 1 h). Scheduled ETL writes are unaffected.

The edge page cache kept a second copy of every page per sidebar state.
A head script now reads the cookie before first paint and on each
client-side navigation, so the server renders one cookie-free page.
Workers Free stops a request at 10 ms of CPU and an uncached page render
takes 15-100 ms, so only edge-cache hits stay under it. On 2026-11-17 a
crawler walking distinct URLs made nearly every request a fresh render
and about 1,100 hit the limit.

Pages and the API answers behind them now hold 10 minutes inside a write
window and otherwise until the next window or 00:00 UTC, keyed by build
so a deploy starts fresh. Unknown ids answer 404 and are kept; a render
that saw an API outage answers 503 or shows its fallback and is never
kept, where a "Couldn't load" page used to stick for an hour.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying f1-prediction-platform-web with  Cloudflare Pages  Cloudflare Pages

Latest commit: b6183ab
Status: ✅  Deploy successful!
Preview URL: https://c8de82fd.f1-prediction-platform-web.pages.dev
Branch Preview URL: https://fix-page-cpu-budget.f1-prediction-platform-web.pages.dev

View logs

@Glory42
Glory42 merged commit 6c453c2 into master Nov 18, 2026
6 checks passed
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