Skip to content
Merged
Changes from 1 commit
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
Next Next commit
fix(hmr): wait for event handlers correctly
  • Loading branch information
sapphi-red committed Apr 4, 2025
commit d26a770db2679ea97e59c659c949a0ea096ac42b
21 changes: 6 additions & 15 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ErrorPayload, HotPayload } from 'types/hmrPayload'
import type { ViteHotContext } from 'types/hot'
import type { InferCustomEventPayload } from 'types/customEvent'
import { HMRClient, HMRContext } from '../shared/hmr'
import {
createWebSocketModuleRunnerTransport,
Expand Down Expand Up @@ -174,7 +173,7 @@ async function handleMessage(payload: HotPayload) {
console.debug(`[vite] connected.`)
break
case 'update':
notifyListeners('vite:beforeUpdate', payload)
await hmrClient.notifyListeners('vite:beforeUpdate', payload)
if (hasDocument) {
// if this is the first update and there's already an error overlay, it
// means the page opened with existing server compile error and the whole
Expand Down Expand Up @@ -238,10 +237,10 @@ async function handleMessage(payload: HotPayload) {
})
}),
)
notifyListeners('vite:afterUpdate', payload)
await hmrClient.notifyListeners('vite:afterUpdate', payload)
break
case 'custom': {
notifyListeners(payload.event, payload.data)
await hmrClient.notifyListeners(payload.event, payload.data)
if (payload.event === 'vite:ws:disconnect') {
if (hasDocument && !willUnload) {
console.log(`[vite] server connection lost. Polling for restart...`)
Expand All @@ -255,7 +254,7 @@ async function handleMessage(payload: HotPayload) {
break
}
case 'full-reload':
notifyListeners('vite:beforeFullReload', payload)
await hmrClient.notifyListeners('vite:beforeFullReload', payload)
if (hasDocument) {
if (payload.path && payload.path.endsWith('.html')) {
// if html file is edited, only reload the page if the browser is
Expand All @@ -276,11 +275,11 @@ async function handleMessage(payload: HotPayload) {
}
break
case 'prune':
notifyListeners('vite:beforePrune', payload)
await hmrClient.notifyListeners('vite:beforePrune', payload)
await hmrClient.prunePaths(payload.paths)
break
case 'error': {
notifyListeners('vite:error', payload)
await hmrClient.notifyListeners('vite:error', payload)
if (hasDocument) {
const err = payload.err
if (enableOverlay) {
Expand All @@ -302,14 +301,6 @@ async function handleMessage(payload: HotPayload) {
}
}

function notifyListeners<T extends string>(
event: T,
data: InferCustomEventPayload<T>,
): void
function notifyListeners(event: string, data: any): void {
hmrClient.notifyListeners(event, data)
}

const enableOverlay = __HMR_ENABLE_OVERLAY__
const hasDocument = 'document' in globalThis

Expand Down