feat: enable Cloudflare Workers Cache for public read-only endpoints#281
Conversation
Enable Workers Cache ("cache": { enabled: true }) on the API Worker and set
Cache-Control: public on the genuinely public, unauthenticated, static GETs
only: /api (health, max-age=60/swr=300) and /llms.txt, /agents.md,
/openapi.json (max-age=300/swr=3600). Authenticated/per-project/mutating
routes set no public directive and auto-bypass on the Authorization header,
so they are never cached. Adds docs/knowledge/workers-cache.md and registers
it in core-memory + docs/INDEX.md.
Co-Authored-By: Duyet Le <me@duyet.net>
Co-Authored-By: duyetbot <bot@duyet.net>
|
Warning Review limit reached
Next review available in: 30 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (5)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request enables Cloudflare Workers Cache for the API and configures public caching headers for static, unauthenticated endpoints such as /api, /llms.txt, /agents.md, and /openapi.json. It also adds documentation detailing the caching strategy and rules. The feedback suggests optimizing the /openapi.json endpoint by parsing the static OpenAPI specification string once at the module level rather than on every request, which reduces CPU overhead on cache misses.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| const STATIC_CACHE_CONTROL = "public, max-age=300, stale-while-revalidate=3600"; | ||
|
|
||
| app.get("/llms.txt", (c) => { | ||
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | ||
| return c.text(LLMS_TXT); | ||
| }); | ||
| app.get("/agents.md", (c) => { | ||
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | ||
| return c.text(AGENTS_MD); | ||
| }); | ||
| app.get("/openapi.json", (c) => { | ||
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | ||
| return c.json(JSON.parse(OPENAPI_SPEC)); | ||
| }); |
There was a problem hiding this comment.
Parsing the large OPENAPI_SPEC JSON string (over 1900 lines) on every request to /openapi.json is highly inefficient and can cause unnecessary CPU overhead and latency, especially on cache misses, bypasses, or during local development. Since the OpenAPI spec is static, we should parse it once at the module level (during Worker startup) and reuse the parsed object.
| const STATIC_CACHE_CONTROL = "public, max-age=300, stale-while-revalidate=3600"; | |
| app.get("/llms.txt", (c) => { | |
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | |
| return c.text(LLMS_TXT); | |
| }); | |
| app.get("/agents.md", (c) => { | |
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | |
| return c.text(AGENTS_MD); | |
| }); | |
| app.get("/openapi.json", (c) => { | |
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | |
| return c.json(JSON.parse(OPENAPI_SPEC)); | |
| }); | |
| const STATIC_CACHE_CONTROL = "public, max-age=300, stale-while-revalidate=3600"; | |
| const PARSED_OPENAPI_SPEC = JSON.parse(OPENAPI_SPEC); | |
| app.get("/llms.txt", (c) => { | |
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | |
| return c.text(LLMS_TXT); | |
| }); | |
| app.get("/agents.md", (c) => { | |
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | |
| return c.text(AGENTS_MD); | |
| }); | |
| app.get("/openapi.json", (c) => { | |
| c.header("Cache-Control", STATIC_CACHE_CONTROL); | |
| return c.json(PARSED_OPENAPI_SPEC); | |
| }); |
Address review: /openapi.json parsed the ~1900-line OPENAPI_SPEC string on every request. It's static, so parse it once at Worker startup and reuse the object — no re-parse cost on cache misses/bypasses or in local dev. Co-Authored-By: Duyet Le <me@duyet.net> Co-Authored-By: duyetbot <bot@duyet.net>
What
Enables Cloudflare Workers Cache (blog, launched 2026-07-06) on the AgentState API Worker and marks the genuinely public, unauthenticated GETs as cacheable.
packages/api/wrangler.jsonc— add top-level"cache": { "enabled": true }.packages/api/src/index.ts— setCache-Control: publicon the static public GETs only:GET /api(health) →public, max-age=60, stale-while-revalidate=300GET /llms.txt,GET /agents.md,GET /openapi.json→public, max-age=300, stale-while-revalidate=3600Why
When the Worker is the origin, every request re-runs the app even when the response is identical. Workers Cache serves a HIT without invoking the Worker (0 CPU) and
stale-while-revalidaterefreshes in the background so no user waits.Safety
Cache-Control— nothing else is cached implicitly./api/v1/conversations,/projects,/states,/leases,/keys, MCP, OAuth, and all POST/PATCH/DELETE) set no public directive and additionally auto-bypass on theAuthorization: Bearer as_live_…header, so they can never be cached./api/v1/analytics/*(routes/analytics-public.ts) is not cached despite its name — it runsapiKeyAuth+ a read scope and returns per-project aggregates. Verified and documented.compatibility_date, bindings, ids, or secrets.Docs
docs/knowledge/workers-cache.md(new) + pointer indocs/knowledge/core-memory.mdanddocs/INDEX.md.Verification
bunx biome check packages/api/src/index.ts→ exit 0bunx tsc --noEmit -p packages/api/tsconfig.json→ exit 0Generated by Claude Code