Skip to content

Commit

Permalink
fix: buffer full reload messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jan 24, 2025
1 parent 1f11e47 commit a0c63f8
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import colors from 'picocolors'
import type { WebSocket as WebSocketRaw } from 'ws'
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
import type { ErrorPayload, HotPayload } from 'types/hmrPayload'
import type {
ErrorPayload,
FullReloadPayload,
HotPayload,
} from 'types/hmrPayload'
import type { InferCustomEventPayload } from 'types/customEvent'
import type { ResolvedConfig } from '..'
import { isObject } from '../utils'
Expand Down Expand Up @@ -231,9 +235,9 @@ export function createWebSocketServer(
})
})
socket.send(JSON.stringify({ type: 'connected' }))
if (bufferedError) {
socket.send(JSON.stringify(bufferedError))
bufferedError = null
if (bufferedMessage) {
socket.send(JSON.stringify(bufferedMessage))
bufferedMessage = null
}
})

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

const normalizedHotChannel = normalizeHotChannel(
{
send(payload) {
if (payload.type === 'error' && !wss.clients.size) {
bufferedError = payload
if (
(payload.type === 'error' || payload.type === 'full-reload') &&
!wss.clients.size
) {
bufferedMessage = payload
return
}

Expand Down

0 comments on commit a0c63f8

Please sign in to comment.