Skip to content

fix(runtime): don't cache missing static asset 404s - #4896

Merged
pi0 merged 1 commit into
mainfrom
fix/static-asset-cache-control
Oct 22, 2026
Merged

pi0 merged 1 commit into
mainfrom
fix/static-asset-cache-control

Conversation

@pi0x

@pi0x pi0x commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #4891. Since error responses apply event.res.errHeaders, the headers route rule Nitro generates for asset dirs (e.g. /build/**) also reaches 404s. A missing file under /build/ came back with cache-control: public, max-age=..., immutable, so a CDN or browser could cache the 404 for up to a year (e.g. a hashed chunk requested during a deploy).

h3 2.0.8 (h3js/h3#1613, closes h3js/h3#1612) lets an HTTPError that carries a header in its own headers opt out of that rule header. This PR upgrades h3 to ^2.0.8, and the static handler now throws its 404s with cache-control: no-store, matching the Vercel preset's existing no-store 404 route. The old event.res.headers.delete("Cache-Control") calls are removed: event.res.headers is not used for error responses, so they had no effect.

Tests: shows 404 for /build/non-file now checks cache-control: no-store when serveStatic is enabled; it failed on node before the fix. The static middleware unit tests now check the headers on the thrown error.

Not covered: presets with serveStatic: false (e.g. cloudflare-module, cloudflare-pages) don't use the static handler, so a missing asset that reaches the worker still gets h3's no-route 404 with the immutable header. That needs a separate fix.

🤖 Generated with AI assistant

Upgrade h3 to 2.0.8 and throw static 404s with `cache-control: no-store`, which opts them out of the immutable asset-dir `headers` route rule (h3js/h3#1612).
@pi0x
pi0x requested a review from pi0 as a code owner October 22, 2026 05:59
@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Oct 22, 2026 5:59am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 13c9f3f1-86b5-4532-8e3d-1667d22598d3
📥 Commits

Reviewing files that changed from the base of the PR and between 933ee52 and d85b407.

⛔ Files ignored due to path filters: 1
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing: 4
  • package.json
  • src/runtime/internal/static.ts
  • test/tests.ts
  • test/unit/static-middleware.test.ts
+---------------------------------------------------+
| This is not a microservice. This is a macro-mess. |
+---------------------------------------------------+
  \
   \     /)/)
        ( '')
        /  |
      o(___)
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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 Oct 22, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4896

commit: d85b407

This branch was successfully deployed

1 active deployment
Preview — d85b4073 Deployed Oct 22, 2026 by vercel[bot]
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.

headers route rule overrides handler-staged errHeaders on error responses

2 participants