Skip to content

perf(static): avoid filesystem lookups for missing public files - #11

Open
RomainLanz wants to merge 2 commits into
2.xfrom
perf/index-static-files
Open

RomainLanz wants to merge 2 commits into
2.xfrom
perf/index-static-files

Conversation

@RomainLanz

Copy link
Copy Markdown
Member

The static middleware checks the filesystem for every GET and HEAD request, including application routes that have no matching public file.

This adds an opt-in indexFiles option. The middleware indexes public/ once when constructed and passes missing paths directly to the next middleware. A public directory index also covers files outside the Vite manifest, such as robots.txt.

Existing files still use serve-static for metadata, caching, HEAD, ranges, directory handling, and dotfile rules. Symlink descendants and case variants also delegate to serve-static.

The default remains unchanged. Indexing is intended for immutable deployments. Adding files requires a restart; leave it disabled for development and uploads. The index requires a synchronous initial scan and memory proportional to the public directory size.

Verification:

  • npm test: 19 passed, including lint.
  • npm run typecheck and npm run build: passed.
  • A real Inertia SSR build using the compiled middleware passed HTML, Inertia JSON, and asset checks. On 1,000 warm route requests, native statx probes of public/dashboard fell from 1,000 to zero.

This removes negative filesystem probes, not existing asset reads. Warm read_bytes remained zero with and without indexing, so these tests do not establish physical disk savings or a fixed throughput gain.

Static middleware probes public paths for application routes on every
GET and HEAD request. Add an opt-in public directory index to skip
known misses while preserving serve-static handling for possible hits.

Index the whole directory instead of the Vite manifest so other public
files remain accessible. Keep indexing disabled by default because new
files require a process restart.
Skip the filesystem for paths ending with a slash when the directory has
no index.html, so application routes such as "/" no longer probe the
public directory.

Fold index keys through lowercase before uppercase. Uppercasing alone
reports existing files as missing for "ẞ" and "ϴ" on case-insensitive
filesystems.

Construct the middleware while booting the web environment so the scan
does not delay the first request, and store keys relative to the public
directory to reduce memory.
@RomainLanz
RomainLanz requested a review from thetutlage October 5, 2026 14:10
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.

1 participant