Skip to content

feat(static): add opt-in Cache-Control via maxAge/immutable - #273

Merged
pi0 merged 2 commits into
mainfrom
feat/statis-cache-control
Jul 17, 2026
Merged

feat(static): add opt-in Cache-Control via maxAge/immutable#273
pi0 merged 2 commits into
mainfrom
feat/statis-cache-control

Conversation

@pi0x

@pi0x pi0x commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds opt-in Cache-Control support to serveStatic(), complementing the ETag/Last-Modified validators added in #269. Both new options are off by default, so behavior is unchanged unless a lifetime is set.

  • maxAge — freshness lifetime in seconds, emitted as Cache-Control: max-age=<n>. Lets a client reuse a response without a request until it goes stale.
  • immutable — appends , immutable (only takes effect alongside maxAge). Skips revalidation even on an explicit reload — appropriate for fingerprinted, content-hashed assets.

Details

  • The header rides along on 304 Not Modified responses too, so a revalidation refreshes the client's stored freshness (RFC 9110 §15.4.5). It is omitted on 412 Precondition Failed and, like the validators, on renderHTML routes.
  • A fractional or negative maxAge is floored and clamped to a valid non-negative delta-seconds.
  • The value depends only on the options, so it is precomputed once at middleware setup.

Tests

Added a Cache-Control describe block covering: default omission, max-age emission, immutable, floor/clamp, immutable-without-maxAge no-op, presence on 304/HEAD and absence on 412/renderHTML. Full suite: 137 passed, typecheck clean.

Docs updated in docs/1.guide/4.middleware.md.

🤖 Generated with Claude Code

`serveStatic()` gains two options, both off by default:

- `maxAge` (seconds) emits `Cache-Control: max-age=<n>`
- `immutable` appends `, immutable` (only alongside `maxAge`)

The header rides along on `304 Not Modified` responses too, so a
revalidation refreshes the client's stored freshness (RFC 9110 §15.4.5),
but is omitted on `412` and on `renderHTML` routes. A fractional or
negative `maxAge` is floored and clamped to a valid delta-seconds.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@pi0x
pi0x requested a review from pi0 as a code owner July 17, 2026 16:19
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 23 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: CHILL

Plan: Pro

Run ID: 7bdaf924-f463-4264-a540-1b10d75e1c91

📥 Commits

Reviewing files that changed from the base of the PR and between 99ca81a and 948d9d2.

📒 Files selected for processing (3)
  • docs/1.guide/4.middleware.md
  • src/static.ts
  • test/static.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/statis-cache-control

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.

@pkg-pr-new

pkg-pr-new Bot commented Jul 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/srvx@273

commit: 948d9d2

`buildCacheControl` floored and clamped negative/fractional `maxAge` but
passed `NaN`, `Infinity`, and very large numbers straight through, emitting
malformed headers like `max-age=NaN` or `max-age=1e+21`. Fall back to 0 for
non-finite input and cap at the RFC 9111 recommended ceiling of 2^31 seconds.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@pi0
pi0 merged commit 0bfdef3 into main Jul 17, 2026
15 of 16 checks passed
@pi0
pi0 deleted the feat/statis-cache-control branch July 17, 2026 16:25
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