Repository navigation
feat: per-call cache status and x-cache response header - #40
Conversation
|
Warning Review limit reached
Next review available in: 41 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: defaults Review profile: CHILL Plan: Pro Run ID: π Files selected for processing (6)
π WalkthroughWalkthroughThis PR introduces a ChangesCacheStatus feature
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related issues
Possibly related PRs
Suggested reviewers: π₯ Pre-merge checks | β 5β Passed checks (5 passed)
β¨ 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 |
x-cache response header
There was a problem hiding this comment.
Actionable comments posted: 1
π€ Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/cache.ts`:
- Around line 222-234: The live cache entry is being mutated in place when
attaching status, which can corrupt shared references from ref-sharing storage
backends. Update the logic in the cache flow around Object.defineProperty(entry,
"status", ...) to clone the entry first, then attach status to the clone and use
that local copy for subsequent value/transform/X-Cache handling. Keep the change
localized to the cache entry update path so concurrent same-key calls cannot
overwrite each otherβs status.
πͺ Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
βΉοΈ Review info
βοΈ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: f181e202-6c74-45e7-b93a-24f85feb1744
π Files selected for processing (6)
README.mdsrc/cache.tssrc/http.tssrc/index.tssrc/types.tstest/index.test.ts
- Expose `entry.status` ("hit" / "stale" / "revalidated" / "miss") to
`transform` for metrics/observability, describing how the value was served on
this call. "revalidated" covers a prior value that was expired/invalid and
re-resolved in the foreground (no stale served), mirroring Vercel's
`x-vercel-cache: REVALIDATED`. Attached as a non-enumerable property so it's
readable by `transform` but never persisted β object spreads (incl.
`expireCache`) and JSON/structuredClone serialization drop it, even for
backends that return entries by reference.
- Add `cacheStatusHeader` to `defineCachedHandler` to emit a CDN-style
`X-Cache: HIT | STALE | REVALIDATED | MISS` header. Defaults to `true`
(`X-Cache`); accepts a custom header name, or `false` to disable. Propagated
on 304 responses.
- Export a new `CacheStatus` type from the package root.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
Adds per-call cache status tracking and a CDN-style
X-Cacheresponse header.Per-call cache status
entry.status("hit"/"stale"/"miss") totransformfor metrics/observability, describing how the value was served on the current call.transformbut never persisted to storage β object spreads (includingexpireCache) and JSON/structuredCloneserialization all drop it, even for backends (like the built-in memory storage) that return entries by reference.X-CacheheadercacheStatusHeadertodefineCachedHandlerto emit a CDN-styleX-Cache: HIT | MISS | STALEheader.true(X-Cache); pass a string for a custom header name, orfalseto disable.304 Not Modifiedresponses.Types
CacheStatustype exported from the package root.Tests
Added coverage for the hit/stale/miss status flow (with and without SWR), non-persistence of
status(on a hit and throughexpireCache), and theX-Cacheheader (default, custom name, disabled, and on 304).All 108 tests pass;
tsgotypecheck clean.π€ Generated with Claude Code
Summary by CodeRabbit
304 Not Modifiedresponses and preserved through cache refresh flows.