Skip to content

fix: serve index.html from public subdirectories#21870

Open
patil-amrut wants to merge 3 commits intovitejs:mainfrom
patil-amrut:fix/serve-index-html-public-subdirs
Open

fix: serve index.html from public subdirectories#21870
patil-amrut wants to merge 3 commits intovitejs:mainfrom
patil-amrut:fix/serve-index-html-public-subdirs

Conversation

@patil-amrut
Copy link

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 the index.html file from public/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 sirv configuration 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) had extensions: [] set, which prevented sirv from properly handling directory index requests.

Solution

Changed extensions: [] to extensions: ['.html'] in both files to allow sirv to serve index.html files from subdirectories when accessing directory paths.

Changes

  • packages/vite/src/node/server/middlewares/static.ts: Updated sirv options
  • packages/vite/src/node/preview.ts: Updated sirv options

Testing

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.html works (already worked)
  • /foo/bar/ now also works (previously didn't)

patil-amrut and others added 2 commits March 14, 2026 17:58
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
@patil-amrut patil-amrut changed the title Fix/serve index html public subdirs fix: serve index.html from public subdirectories Mar 15, 2026
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.
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the unrelated changes. Also add a test for the new functionality.

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.

Serve index.html inside subdirectory of public from subdirectory path

2 participants