Skip to content

Commit a0c63f8

Browse files
committed
fix: buffer full reload messages
1 parent 1f11e47 commit a0c63f8

File tree

1 file changed

+16
-7
lines changed
  • packages/vite/src/node/server

1 file changed

+16
-7
lines changed

packages/vite/src/node/server/ws.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import colors from 'picocolors'
99
import type { WebSocket as WebSocketRaw } from 'ws'
1010
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1111
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
12-
import type { ErrorPayload, HotPayload } from 'types/hmrPayload'
12+
import type {
13+
ErrorPayload,
14+
FullReloadPayload,
15+
HotPayload,
16+
} from 'types/hmrPayload'
1317
import type { InferCustomEventPayload } from 'types/customEvent'
1418
import type { ResolvedConfig } from '..'
1519
import { isObject } from '../utils'
@@ -231,9 +235,9 @@ export function createWebSocketServer(
231235
})
232236
})
233237
socket.send(JSON.stringify({ type: 'connected' }))
234-
if (bufferedError) {
235-
socket.send(JSON.stringify(bufferedError))
236-
bufferedError = null
238+
if (bufferedMessage) {
239+
socket.send(JSON.stringify(bufferedMessage))
240+
bufferedMessage = null
237241
}
238242
})
239243

@@ -279,13 +283,18 @@ export function createWebSocketServer(
279283
// sends the error payload before the client connection is established.
280284
// If we have no open clients, buffer the error and send it to the next
281285
// connected client.
282-
let bufferedError: ErrorPayload | null = null
286+
// The same thing may happen when the optimizer runs fast enough to
287+
// finish the bundling before the client connects.
288+
let bufferedMessage: ErrorPayload | FullReloadPayload | null = null
283289

284290
const normalizedHotChannel = normalizeHotChannel(
285291
{
286292
send(payload) {
287-
if (payload.type === 'error' && !wss.clients.size) {
288-
bufferedError = payload
293+
if (
294+
(payload.type === 'error' || payload.type === 'full-reload') &&
295+
!wss.clients.size
296+
) {
297+
bufferedMessage = payload
289298
return
290299
}
291300

0 commit comments

Comments
 (0)