Skip to content

Missing configured public folder silently breaks HMR #19864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
7 tasks done
Laupetin opened this issue Apr 14, 2025 · 0 comments
Open
7 tasks done

Missing configured public folder silently breaks HMR #19864

Laupetin opened this issue Apr 14, 2025 · 0 comments
Labels
has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@Laupetin
Copy link
Contributor

Describe the bug

If your vite.config.js configures a publicDir, but the specified directory does not exist, the build works fine and the dev server starts fine as well.

However as soon as a file is edited, the file system watcher from chokidar does not detect any changes.
It may depend a bit on the layout of the project, I attached a vite.new link to a similar setup in which i ran into the problem.

The problem can simply be solved but not configuring the publicDir explicitly or creating the specified folder.

Maybe a bit obscure scenario, but it did take me a bit to find out, why HMR was not working 😅 It was not a hard fix but I would have expected something to either error or log rather than silently breaking HMR.

A possible solution to this in Vite could maybe be to only watch the publicDir in case it exists (or contains any files)?
What works for me at least seems to be making the following change:

...(publicDir && publicFiles?.size ? [publicDir] : []),

to here:

...(publicDir && publicFiles ? [publicDir] : []),

Reproduction

https://stackblitz.com/edit/vitejs-vite-l1sytzhy?file=src%2Fmain%2Ffrontend%2Fsrc%2Fcounter.js

Steps to reproduce

  • Root contains a vite.config.js which configures the publicDir to the src/main/frontend/public
  • The directory src/main/frontend should exist, but directory src/main/frontend/public should not exist
  • Root contains an index.html that includes a script in path src/main/frontend/src/main.js
  • Run vite --debug hmr
  • Changing the main.js with not result in a log output that any file was changes, nor will the browser preview be updated
  • Creating the directory specified in publicDir or removing this configuration will fix it

System Info

System:
  OS: Linux 6.14 Arch Linux
  CPU: (16) x64 AMD Ryzen 7 PRO 5850U with Radeon Graphics
  Memory: 7.41 GB / 30.18 GB
  Container: Yes
  Shell: 5.9 - /usr/bin/zsh
Binaries:
  Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
  Yarn: 1.22.22 - /usr/bin/yarn
  npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
  pnpm: 10.8.0 - /usr/bin/pnpm
npmPackages:
  @vitejs/plugin-vue: 5.2.3 => 5.2.3 
  vite: 6.2.6 => 6.2.6

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red sapphi-red added has workaround p2-edge-case Bug, but has workaround or limited in scope (priority) labels Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

2 participants