vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
65.2k stars 5.78k forks source link

Dev and preview are not aligned in how they serve index.html files contained under subfolders of `public` #16567

Closed ElliotNB closed 2 weeks ago

ElliotNB commented 2 weeks ago

Describe the bug

I have a project with static HTML and CSS assets stored in a subfolder under the public folder (e.g., public/admin-panel/, public/reports/). Each subfolder under the public folder contains an index.html file (e.g., public/admin-panel/index.html and public/reports/index.html).

If I build my application (npm run build -- aka, vite build) and serve up the preview (npm run serve -- aka, vite preview), then the index.html file is served up correctly regardless of whether I access the file via http://localhost:4173/admin-panel/ or http://localhost:4173/admin-panel/index.html

However, if I start up my application using npm run dev (aka, vite), then it does not behave the same. If I visit http://localhost:5173/admin-panel/ then it will instead serve the index.html file contained in the root folder of my project. Only if I visit http://localhost:5173/admin-panel/index.html will it then serve up the correct index.html file.

A similar bug was patched back in October 2023 ( https://github.com/vitejs/vite/pull/14756 ) but it appears to have only been applied to index.html files contained in subfolders under the src folder. Subfolders to the public folder are still impacted.

Reproduction

https://stackblitz.com/edit/vitejs-vite-sepev3?file=main.js

Steps to reproduce

  1. Open the re-pro URL ( https://stackblitz.com/edit/vitejs-vite-sepev3?file=main.js ).
  2. Click on Admin panel link (does not work) and observe that it serves the root index.html.
  3. Click on Admin panel link (does work) and observe that it correctly serves public/admin-panel/index.html -- displaying the "Hello world." mesage.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (4) x64 Intel(R) Core(TM)2 Quad CPU    Q9650  @ 3.00GHz
    Memory: 6.28 GB / 16.00 GB
  Binaries:
    Node: 21.7.3 - C:\Program Files\nodejs\node.EXE
    npm: 10.5.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.6 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.19041.3636

Used Package Manager

npm

Logs

No response

Validations

stackblitz[bot] commented 2 weeks ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

sapphi-red commented 2 weeks ago

Duplicate of #6714