@@ -10,7 +10,11 @@ import colors from 'picocolors'
1010import type { WebSocket as WebSocketRaw } from 'ws'
1111import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1212import 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'
1418import type { InferCustomEventPayload } from 'types/customEvent'
1519import type { ResolvedConfig } from '..'
1620import { isObject } from '../utils'
@@ -295,9 +299,9 @@ export function createWebSocketServer(
295299 } )
296300 } )
297301 socket . send ( JSON . stringify ( { type : 'connected' } ) )
298- if ( bufferedError ) {
299- socket . send ( JSON . stringify ( bufferedError ) )
300- bufferedError = null
302+ if ( bufferedMessage ) {
303+ socket . send ( JSON . stringify ( bufferedMessage ) )
304+ bufferedMessage = null
301305 }
302306 } )
303307
@@ -343,13 +347,18 @@ export function createWebSocketServer(
343347 // sends the error payload before the client connection is established.
344348 // If we have no open clients, buffer the error and send it to the next
345349 // connected client.
346- let bufferedError : ErrorPayload | null = null
350+ // The same thing may happen when the optimizer runs fast enough to
351+ // finish the bundling before the client connects.
352+ let bufferedMessage : ErrorPayload | FullReloadPayload | null = null
347353
348354 const normalizedHotChannel = normalizeHotChannel (
349355 {
350356 send ( payload ) {
351- if ( payload . type === 'error' && ! wss . clients . size ) {
352- bufferedError = payload
357+ if (
358+ ( payload . type === 'error' || payload . type === 'full-reload' ) &&
359+ ! wss . clients . size
360+ ) {
361+ bufferedMessage = payload
353362 return
354363 }
355364
0 commit comments