Skip to content

fix(web): cache api answers at the edge to stop billed worker hops - #125

Merged
Glory42 merged 1 commit into
masterfrom
fix/api-request-budget
Nov 17, 2026
Merged

Glory42 merged 1 commit into
masterfrom
fix/api-request-budget

Conversation

@Glory42

@Glory42 Glory42 commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Summary

On 2026-11-17 the account used 84.5k of Workers Free's 100k daily requests by 19:41 UTC. f1-api alone took 41k, and most of those were this site's own server fetches to the API's public URL, about 2.7 per page render. Each one is a billed Worker request, even when the API answers from its edge cache.

 get(path)                                   apps/web/src/lib/api.ts
-  fetch(PUBLIC_API_URL + path)              # 1 billed Worker request, every time
+  fetchApiCached(PUBLIC_API_URL + path)     apps/web/src/lib/apiCache.ts
+    hit in the site's caches.default  → return it   # no Worker request
+    miss → fetch, and if 200 + public:
+      store for min(API max-age, page lifetime)     # 5 min Fri–Mon noon UTC, 1 h between

An answer is never held longer than a page is, so a backfill or a SNAPSHOT_VERSION bump shows up as fast as before. Errors, 404s, no-store and answers without a max-age are never stored. The cache is off in astro dev and in the browser (search and compare). A Service Binding would remove the hop entirely, but Cloudflare's docs don't say whether binding calls count toward the Free daily limit, or whether the API's Cache API still works when it's reached through one.

docs/deployment.md also gains the dashboard WAF rule that blocks crawlers on f1-api: Meta's meta-webindexer ignores the API's robots.txt and calls API URLs directly.

Evidence

  • Before: f1-api handled 41,183 requests on 11-17, against 14,420 Pages Functions runs. From 11:00 UTC the two moved together at about 2.7 API calls per render.
  • After: tests/unit/lib/apiCache.test.ts, 10 tests:
    • a repeat read doesn't call the API again (1 load for 2 reads)
    • the API's max-age=86400 is capped to 3600 on a Tuesday and 300 on a Saturday; a shorter API max-age is kept
    • 504s, 404s, no-store and answers without a max-age are never stored
    • with no cache available, or when the cache throws, the site still calls the API
  • The web app's 361 unit tests pass; astro check reports 0 errors; ESLint and bun run build pass.

Merge Danger

Door: two-way

Reverting the commit puts back the direct fetch. Entries already stored expire within an hour.

Blast Radius: site-wide

Every server-rendered page reads the API through this cache. A misbehaving cache would show data up to 1 h old (5 min on race weekends), the same staleness the page cache already allows.

Every fetch from the site's Pages Functions to the API's public URL is a
billed Worker request on Workers Free, even when the API answers from its
own edge cache. On 2026-11-17 that made f1-api 41k of the account's 100k
daily requests. Answers are held no longer than a page is, so backfills
and SNAPSHOT_VERSION bumps show up as fast as before.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

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

Latest commit: 7b401a7
Status: ✅  Deploy successful!
Preview URL: https://b389972e.f1-prediction-platform-web.pages.dev
Branch Preview URL: https://fix-api-request-budget.f1-prediction-platform-web.pages.dev

View logs

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
f1-prediction-platform-api 7b401a7 Nov 17 2026, 09:39 PM

@Glory42
Glory42 merged commit 6c65ffd into master Nov 17, 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