Skip to content

Commit 3a7f2ae

Browse files
authored
fix(html): avoid adding filesystem root to watcher (#23689)
1 parent facc2fb commit 3a7f2ae

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<div>Outside root</div>
5+
</body>
6+
</html>

‎packages/vite/src/node/server/middlewares/__tests__/indexHtml.spec.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3-
import { describe, expect, onTestFinished, test } from 'vitest'
3+
import { describe, expect, onTestFinished, test, vi } from 'vitest'
44
import { FS_PREFIX } from '../../../constants'
55
import { createServer } from '../../../server'
66

77
const FIXTURE_DIR = path.resolve(import.meta.dirname, 'fixtures')
88
const HTML_PATH = path.resolve(FIXTURE_DIR, 'root/index.html')
99
const HTML_CONTENT = fs.readFileSync(HTML_PATH, 'utf-8')
10+
const OUTSIDE_HTML_PATH = path.resolve(FIXTURE_DIR, 'outside.html')
11+
const OUTSIDE_HTML_CONTENT = fs.readFileSync(OUTSIDE_HTML_PATH, 'utf-8')
1012
const VITE_PACKAGE_DIR = path.resolve(import.meta.dirname, '../../../../..')
1113

1214
async function createTestServer(rootDir?: string) {
@@ -107,6 +109,32 @@ describe('indexHtml middleware — inline script proxy cache', () => {
107109
}
108110
})
109111

112+
describe('indexHtml middleware — file watching', () => {
113+
test('watches an /@fs/ HTML file outside root', async () => {
114+
const server = await createTestServer()
115+
const addSpy = vi
116+
.spyOn(server.watcher, 'add')
117+
.mockImplementation(() => server.watcher)
118+
const fsUrl = path.posix.join(FS_PREFIX, OUTSIDE_HTML_PATH)
119+
120+
await server.transformIndexHtml(fsUrl, OUTSIDE_HTML_CONTENT)
121+
122+
expect(addSpy).toHaveBeenCalledWith(OUTSIDE_HTML_PATH)
123+
})
124+
125+
test('does not watch a directory for a root URL with a query', async () => {
126+
const server = await createTestServer()
127+
const addSpy = vi
128+
.spyOn(server.watcher, 'add')
129+
.mockImplementation(() => server.watcher)
130+
const html = '<style>body { color: red; }</style>'
131+
132+
await server.transformIndexHtml('/?foo=bar', html)
133+
134+
expect(addSpy).not.toHaveBeenCalledWith('/')
135+
})
136+
})
137+
110138
describe('indexHtml middleware — HMR timestamp injection with non-root base', () => {
111139
test('entry script URL gets the lastHMRTimestamp query when base is not root', async () => {
112140
const root = path.resolve(import.meta.dirname, 'fixtures/base-root')

‎packages/vite/src/node/server/middlewares/indexHtml.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
normalizePath,
4444
processSrcSetSync,
4545
stripBase,
46+
tryStatSync,
4647
} from '../../utils'
4748
import {
4849
BasicMinimalPluginContext,
@@ -223,7 +224,8 @@ const devHtmlHook: IndexHtmlTransformHook = async (
223224
let proxyModuleUrl: string
224225

225226
const trailingSlash = htmlPath.endsWith('/')
226-
if (!trailingSlash && fs.existsSync(filename)) {
227+
if (!trailingSlash && tryStatSync(filename)?.isFile()) {
228+
ensureWatchedFile(watcher, filename, config.root)
227229
// If htmlPath is a /@fs/ URL (e.g. vitest-browser always uses this form
228230
// for testerHtmlPath), normalise to an absolute FS path so proxyCacheUrl
229231
// is always root-relative.
@@ -404,7 +406,6 @@ const devHtmlHook: IndexHtmlTransformHook = async (
404406
url,
405407
false,
406408
)
407-
ensureWatchedFile(watcher, mod.file, config.root)
408409

409410
const result =
410411
await server!.environments.client.pluginContainer.transform(
@@ -431,7 +432,6 @@ const devHtmlHook: IndexHtmlTransformHook = async (
431432
url,
432433
false,
433434
)
434-
ensureWatchedFile(watcher, mod.file, config.root)
435435

436436
await server?.environments.client.pluginContainer.transform(code, mod.id!)
437437

0 commit comments

Comments
 (0)