Skip to content

feat: enable Cloudflare Workers Cache for public read-only endpoints#281

Merged
duyet merged 2 commits into
mainfrom
claude/worker-cache-implementation-4wqr71
Jul 7, 2026
Merged

feat: enable Cloudflare Workers Cache for public read-only endpoints#281
duyet merged 2 commits into
mainfrom
claude/worker-cache-implementation-4wqr71

Conversation

@duyet

@duyet duyet commented Jul 6, 2026

Copy link
Copy Markdown
Owner

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 — set Cache-Control: public on the static public GETs only:
    • GET /api (health) → public, max-age=60, stale-while-revalidate=300
    • GET /llms.txt, GET /agents.md, GET /openapi.jsonpublic, max-age=300, stale-while-revalidate=3600

Why

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-revalidate refreshes in the background so no user waits.

Safety

  • Enabling the flag is a no-op until a response sets a public Cache-Control — nothing else is cached implicitly.
  • All Bearer-authed / per-project / mutating routes (/api/v1/conversations, /projects, /states, /leases, /keys, MCP, OAuth, and all POST/PATCH/DELETE) set no public directive and additionally auto-bypass on the Authorization: Bearer as_live_… header, so they can never be cached.
  • /api/v1/analytics/* (routes/analytics-public.ts) is not cached despite its name — it runs apiKeyAuth + a read scope and returns per-project aggregates. Verified and documented.
  • No changes to compatibility_date, bindings, ids, or secrets.

Docs

docs/knowledge/workers-cache.md (new) + pointer in docs/knowledge/core-memory.md and docs/INDEX.md.

Verification

  • bunx biome check packages/api/src/index.ts → exit 0
  • bunx tsc --noEmit -p packages/api/tsconfig.json → exit 0

Note: requires a Wrangler version with Workers Cache support (the cache config key) at deploy time.


Generated by Claude Code

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>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @duyet, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@duyet, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 30 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0ee06cba-7e1a-4ca7-94ce-4856831b6ca8

📥 Commits

Reviewing files that changed from the base of the PR and between 4ed75ef and 98af376.

📒 Files selected for processing (5)
  • docs/INDEX.md
  • docs/knowledge/core-memory.md
  • docs/knowledge/workers-cache.md
  • packages/api/src/index.ts
  • packages/api/wrangler.jsonc
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/worker-cache-implementation-4wqr71

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/api/src/index.ts
Comment on lines +111 to +124
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));
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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>
@duyet
duyet merged commit dcea999 into main Jul 7, 2026
6 checks passed
@duyet
duyet deleted the claude/worker-cache-implementation-4wqr71 branch July 7, 2026 00:23
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.

2 participants