diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index b2632c841ca0ef..35a4fb02a9149a 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -641,14 +641,18 @@ export async function _createServer( } } + const normalizedPublicDir = normalizePath(config.publicDir) + const onFileAddUnlink = async (file: string, isUnlink: boolean) => { file = normalizePath(file) await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' }) - if (publicFiles && config.publicDir && file.startsWith(config.publicDir)) { - publicFiles[isUnlink ? 'delete' : 'add']( - file.slice(config.publicDir.length), - ) + if (config.publicDir && publicFiles) { + if (file.startsWith(normalizedPublicDir)) { + publicFiles[isUnlink ? 'delete' : 'add']( + file.slice(normalizedPublicDir.length), + ) + } } await handleFileAddUnlink(file, server, isUnlink) await onHMRUpdate(file, true)