Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export async function preview(
sirv(distDir, {
etag: true,
dev: true,
extensions: [],
extensions: ['.html'],
ignores: false,
setHeaders(res) {
if (headers) {
Expand Down
13 changes: 13 additions & 0 deletions packages/vite/src/node/server/__tests__/watcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ describe('watcher configuration', () => {
)
})
})

it('should not watch a non-existent public directory', async () => {
const root = fileURLToPath(
new URL('./fixtures/watcher/nested-root', import.meta.url),
)
const nonExistentPublicDir = resolve(root, '../non-existent-public')
server = await createServer({ root, publicDir: nonExistentPublicDir })
await new Promise((resolve) => server!.watcher.once('ready', resolve))
await vi.waitFor(() => {
const watchedDirs = Object.keys(server!.watcher.getWatched())
expect(watchedDirs).not.toContain(nonExistentPublicDir)
})
})
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ export async function _createServer(
...getEnvFilesForMode(config.mode, config.envDir),
// Watch the public directory explicitly because it might be outside
// of the root directory.
...(publicDir && publicFiles ? [publicDir] : []),
...(publicDir && publicFiles?.size ? [publicDir] : []),
],

resolvedWatchOptions,
Expand Down
9 changes: 4 additions & 5 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const sirvOptions = ({
return {
dev: true,
etag: true,
extensions: [],
setHeaders(res, pathname) {
// Matches js, jsx, ts, tsx, mts, mjs, cjs, cts, ctx, mtx
// The reason this is done, is that the .ts and .mts file extensions are
Expand Down Expand Up @@ -82,14 +81,14 @@ export function servePublicMiddleware(
publicFiles?: Set<string>,
): Connect.NextHandleFunction {
const dir = server.config.publicDir
const serve = sirv(
dir,
sirvOptions({
const serve = sirv(dir, {
...sirvOptions({
config: server.config,
getHeaders: () => server.config.server.headers,
disableFsServeCheck: true,
}),
)
extensions: ['.html'],
})

const toFilePath = (url: string) => {
let filePath = cleanUrl(url)
Expand Down
Loading