fix: serve index.html from public subdirectories#21870
Open
patil-amrut wants to merge 3 commits intovitejs:mainfrom
Open
fix: serve index.html from public subdirectories#21870patil-amrut wants to merge 3 commits intovitejs:mainfrom
patil-amrut wants to merge 3 commits intovitejs:mainfrom
Conversation
When publicDir is configured but the directory does not exist, initPublicFiles returns an empty Set (truthy), causing the non-existent path to be added to chokidar's watch list. This silently breaks HMR as chokidar fails to detect file changes. Check publicFiles.size instead of just publicFiles to ensure only existing, non-empty public directories are watched. Fixes vitejs#19864
Allow sirv to serve index.html files from subdirectories in the public folder when accessing the directory path (e.g., /foo/bar/ should serve /public/foo/bar/index.html). This fixes the inconsistency between dev and preview modes where preview already supported this behavior. Closes vitejs#6714
This fixes the HMR regression introduced by adding extensions to sirvOptions. The extensions should only apply to the public directory middleware for serving index.html from subdirectories, not to the dev server's static middleware.
sapphi-red
requested changes
Mar 16, 2026
Member
sapphi-red
left a comment
There was a problem hiding this comment.
Please remove the unrelated changes. Also add a test for the new functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #6714 - Serve index.html files from subdirectories in the public folder when accessing the directory path (e.g.,
/foo/bar/should serve/public/foo/bar/index.html).Problem
Currently, in Vite's dev server, accessing a path like
/foo/bar/does not serve theindex.htmlfile frompublic/foo/bar/index.html, even though this works correctly in preview mode. This creates an inconsistency between development and production builds.Root Cause
The issue was in the
sirvconfiguration used for serving static files. Both the dev server (packages/vite/src/node/server/middlewares/static.ts) and preview server (packages/vite/src/node/preview.ts) hadextensions: []set, which preventedsirvfrom properly handling directory index requests.Solution
Changed
extensions: []toextensions: ['.html']in both files to allowsirvto serveindex.htmlfiles from subdirectories when accessing directory paths.Changes
packages/vite/src/node/server/middlewares/static.ts: Updated sirv optionspackages/vite/src/node/preview.ts: Updated sirv optionsTesting
The fix aligns dev and preview behavior. Preview mode already worked correctly, and now dev mode matches this behavior.
Reproduction
The original issue includes a reproduction case: https://stackblitz.com/edit/vitejs-vite-944tsu?file=public%2Ffoo%2Fbar%2Findex.html&terminal=dev
After this fix:
/foo/bar/index.htmlworks (already worked)/foo/bar/now also works (previously didn't)