Skip to content

fix(vite): do not crash serving public assets from the dev worker - #5121

Merged
pi0 merged 1 commit into
mainfrom
fix/vite-deleted-public-asset
Nov 11, 2026
Merged

pi0 merged 1 commit into
mainfrom
fix/vite-deleted-public-asset

Conversation

@pi0x

@pi0x pi0x commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

With nitro/vite in dev, the dev worker's static handler returns a 500 for public assets in two cases:

  • a file in public/ is deleted while the dev server runs, and its URL is requested again
  • any public asset is requested through serverFetch() (internal fetch), even one that exists

Root cause

The node public assets reader (#nitro/virtual/public-assets-node) resolves each manifest path against globalThis.__nitro_main__. The rollup, rolldown and rspack builds set that global in the entry chunk, but the Vite dev runner does not, so fileURLToPath(undefined) throws.

In dev, the manifest paths are already absolute (resolve(asset.dir, file)), so __nitro_main__ is not needed there.

Fix

The reader template uses the manifest path as is in dev. The production output does the same thing as before, so built servers are unchanged.

After the fix, Vite matches the other builders: a deleted file gets an uncached 404, and internal fetch of an existing asset works.

Not changed here: in dev, the worker's manifest is only rendered at build time. So a deleted file still gets a 404 from the static handler instead of falling through to a route on the same path, until the next rebuild. This is the same on all builders on main, and has a different cause.

Related to #5120 (same 500 when a whole public/ dir is removed).

Tests

New tests in the dev reload suite, for Vite (test/vite/scan-reload.test.ts) and the builders (test/unit/dev-scan-reload.test.ts): a public asset that exists at startup is fetched directly and through serverFetch(), then deleted.

  • Vite fails on main ([ 'asset', 500 ], and [ 500, 500 ] after the delete) and passes with the fix, 5 runs each.
  • rolldown, rollup and rspack already passed on main and still pass, 5 runs each.
  • test/vite + test/unit (rolldown), test/presets/nitro-dev and test/presets/node pass, also after a full pnpm build.

🤖 Generated with AI assistant

The node public assets reader resolved paths against `__nitro_main__`, which the Vite dev runner does not set. Dev manifest paths are already absolute.
@pi0x
pi0x requested a review from pi0 as a code owner November 11, 2026 22:41
@vercel

vercel Bot commented Nov 11, 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 Nov 11, 2026 10:42pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The generated Node public asset handler now uses direct asset paths in dev mode and resolves paths relative to the server entry point otherwise. New dev-server and Vite tests check public asset responses before and after the asset file is deleted.

Changes: Public asset path handling

Layer / File(s) Summary
Select asset paths by mode
src/build/virtual/public-assets.ts
The generated Node handler uses the asset path directly in dev mode. In non-dev mode, it continues to resolve the path relative to the server entry point.
Test public asset removal
test/unit/_scan-dirs.ts, test/unit/dev-scan-reload.test.ts, test/vite/scan-reload.test.ts
The shared fixture defines a public asset and an internal route that fetches it. Dev-server and Vite tests check the direct and internal responses before and after deletion. The dev-server test skips on Windows with Rspack.

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

Change: Bug fix

Possibly related PRs

Merge Risk: Minimal · up to fb85e

This change stops the dev worker from returning 500 when serving public assets and should be low risk to merge. Production output is unchanged, and the new tests cover the fixed behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title uses the conventional commits form "fix(vite): ..." and the scope and summary match the changeset. The PR fixes a 500 response from the Vite dev worker's public asset handling, which the tit…
Description check Passed The description states the root cause, the fix, the affected tests, and the verification runs. It matches the changes to the public assets template and the new scan-reload tests. It also notes a known…
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.
Full details: Title check

Explanation

The title uses the conventional commits form "fix(vite): ..." and the scope and summary match the changeset. The PR fixes a 500 response from the Vite dev worker's public asset handling, which the title states. The title meets the stated requirement that it should follow conventional commits.

Full details: Description check

Explanation

The description states the root cause, the fix, the affected tests, and the verification runs. It matches the changes to the public assets template and the new scan-reload tests. It also notes a known remaining limitation, so it is related to the changeset.

Finishing Touches

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: db2e0032-72bd-4d90-8b56-f5c29ea74813
Commits

Reviewing files that changed from the base of the PR and between f79d7a2 and fb85ebb.

Files selected for processing: 4

  • src/build/virtual/public-assets.ts
  • test/unit/_scan-dirs.ts
  • test/unit/dev-scan-reload.test.ts
  • test/vite/scan-reload.test.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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: fb85ebb

@pi0
pi0 merged commit fddb916 into main Nov 11, 2026
17 checks passed
@pi0
pi0 deleted the fix/vite-deleted-public-asset branch November 11, 2026 23:14

This branch was successfully deployed

1 active deployment
Preview — fb85ebb2 Deployed Nov 11, 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