Skip to content

Commit 46b1b0a

Browse files
committed
fix: buffer full reload messages
1 parent adc78dc commit 46b1b0a

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import colors from 'picocolors'
1010
import type { WebSocket as WebSocketRaw } from 'ws'
1111
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1212
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
13-
import type { ErrorPayload, HotPayload } from 'types/hmrPayload'
13+
import type {
14+
ErrorPayload,
15+
FullReloadPayload,
16+
HotPayload,
17+
} from 'types/hmrPayload'
1418
import type { InferCustomEventPayload } from 'types/customEvent'
1519
import type { ResolvedConfig } from '..'
1620
import { isObject } from '../utils'
@@ -297,9 +301,9 @@ export function createWebSocketServer(
297301
})
298302
})
299303
socket.send(JSON.stringify({ type: 'connected' }))
300-
if (bufferedError) {
301-
socket.send(JSON.stringify(bufferedError))
302-
bufferedError = null
304+
if (bufferedMessage) {
305+
socket.send(JSON.stringify(bufferedMessage))
306+
bufferedMessage = null
303307
}
304308
})
305309

@@ -345,13 +349,18 @@ export function createWebSocketServer(
345349
// sends the error payload before the client connection is established.
346350
// If we have no open clients, buffer the error and send it to the next
347351
// connected client.
348-
let bufferedError: ErrorPayload | null = null
352+
// The same thing may happen when the optimizer runs fast enough to
353+
// finish the bundling before the client connects.
354+
let bufferedMessage: ErrorPayload | FullReloadPayload | null = null
349355

350356
const normalizedHotChannel = normalizeHotChannel(
351357
{
352358
send(payload) {
353-
if (payload.type === 'error' && !wss.clients.size) {
354-
bufferedError = payload
359+
if (
360+
(payload.type === 'error' || payload.type === 'full-reload') &&
361+
!wss.clients.size
362+
) {
363+
bufferedMessage = payload
355364
return
356365
}
357366

0 commit comments

Comments
 (0)