Skip to content

perf(runtime): skip static middleware work for non-asset requests - #4854

Merged
pi0 merged 2 commits into
mainfrom
perf/static-assets
Oct 19, 2026
Merged

pi0 merged 2 commits into
mainfrom
perf/static-assets

Conversation

@pi0x

@pi0x pi0x commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

The static middleware runs on every request when serveStatic is enabled (the default for server presets). For requests that are not public assets, it still parsed and sorted accept-encoding, then made up to 6 getAsset lookups with joinURL, plus decodePath on every path.

Now it returns early unless the path is an asset (also one that only exists compressed, e.g. x.js.br), a directory with an index.html, or under a public asset base (which still returns 404 as before). #nitro/virtual/public-assets gets a hasAsset(id) export that looks up id and its .gz/.br/.zst variants directly in the manifest (no startup cost or extra memory). decodePath only runs for paths containing %.

Hello-world GET /api/hello, bundled node-server build with a public/ dir, Node 24, oha with 100 connections, server pinned to 4 cores (median of 3×8s):

RPS
before 23.4k
after 27.3k (+17%)
serveStatic: false (upper bound) 28.9k

Found while benchmarking nitro start (#4849).

🤖 Generated with AI assistant

Check the public asset manifest (also compressed-only assets and public asset bases) before parsing `accept-encoding`, and only decode paths containing `%`.
@pi0x
pi0x requested a review from pi0 as a code owner October 18, 2026 23:52
@vercel

vercel Bot commented Oct 18, 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 18, 2026 11:59pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 11cc1b03-14d7-4fe2-9d22-f6c01ad4c514
📥 Commits

Reviewing files that changed from the base of the PR and between a062b9d and 9c7b6b5.

📒 Files selected for processing: 1
  • src/build/virtual/public-assets.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The public-assets modules now export hasAsset. The static handler checks asset presence before encoding negotiation and decodes the normalized request path only when it contains %. Tests cover non-public paths and compressed-only assets.

Changes: Static asset lookup

Layer / File(s) Summary
Public asset presence checks
src/build/virtual/public-assets.ts, src/runtime/virtual/public-assets.ts
The build module checks asset IDs and their .gz, .br, and .zst variants. The runtime module provides a hasAsset stub that returns false.
Static handler fast path and validation
src/runtime/internal/static.ts, test/unit/static-middleware.test.ts
The handler checks the normalized path and its index.html candidate before encoding negotiation. Tests check that non-public paths return early and that a compressed-only asset is served with its encoding.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 9c7b6

The change skips unnecessary static handling for non-assets while preserving the inspected asset-serving paths. No concrete user-facing regression remains to resolve before merging.

### Architecture Summary

Architecture risk: 🔵 Low · up to 9c7b6

The change affects 2 systems.

Changed systems: src, test

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — src (service) was modified; 3 changed files map to changed impact.
  • observed — test (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in src/runtime/internal/static.ts: The handler adds hasAsset to its public-assets imports.
  • observed — Modified behavior in src/runtime/internal/static.ts: The handler now normalizes the URL path and decodes it only when it contains %; previously it always decoded the path. It returns before encoding negotiation when neither the path nor its index.html candidate is an asset and the path is not a public-asset URL.
  • observed — Modified behavior in src/runtime/virtual/public-assets.ts: Added the exported hasAsset function; it returns false for all asset IDs.
  • observed — Modified behavior in test/unit/static-middleware.test.ts: The hasAsset mock now checks the requested ID and its .gz, .br, and .zst variants using getAsset.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title follows the Conventional Commits format with the type perf, scope runtime, and a concise summary of the static middleware optimization.
Description check Passed The description clearly explains the static middleware optimization, the new hasAsset behavior, early-return conditions, and benchmark results.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 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 18, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 9c7b6b5

pi0x pushed a commit that referenced this pull request Oct 19, 2026
…ing paths

Same approach as #4854: keep the raw file list and check the `.html` and `index.html` candidates per request.
@pi0
pi0 merged commit 93e55ee into main Oct 19, 2026
15 checks passed
@pi0
pi0 deleted the perf/static-assets branch October 19, 2026 03:10

This branch was successfully deployed

1 active deployment
Preview — 9c7b6b53 Deployed Oct 18, 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.

2 participants