@@ -9,7 +9,11 @@ import colors from 'picocolors'
99import type { WebSocket as WebSocketRaw } from 'ws'
1010import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1111import 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'
1317import type { InferCustomEventPayload } from 'types/customEvent'
1418import type { ResolvedConfig } from '..'
1519import { 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