Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 2e555a2

Browse files
committed
Improve hmr ws connection
1 parent 910495c commit 2e555a2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

framework/core/hmr.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ export function connect(basePath: string) {
6868
const { protocol, host } = location
6969
const wsUrl = (protocol === 'https:' ? 'wss' : 'ws') + '://' + host + basePath.replace(/\/+$/, '') + '/_hmr'
7070
const ws = new WebSocket(wsUrl)
71+
const contact = (callback: () => void) => {
72+
setTimeout(() => {
73+
const ws = new WebSocket(wsUrl)
74+
ws.addEventListener('open', callback)
75+
ws.addEventListener('close', () => {
76+
contact(callback) // retry
77+
})
78+
}, 500)
79+
}
7180

7281
ws.addEventListener('open', () => {
7382
state.socket = ws
@@ -76,21 +85,16 @@ export function connect(basePath: string) {
7685
})
7786

7887
ws.addEventListener('close', () => {
79-
if (state.socket === null) {
88+
if (state.socket !== null) {
89+
state.socket = null
90+
console.log('[HMR] closed.')
8091
// re-connect
8192
setTimeout(() => {
8293
connect(basePath)
8394
}, 300)
8495
} else {
85-
state.socket = null
86-
console.log('[HMR] closed.')
8796
// reload the page when re-connected
88-
setInterval(() => {
89-
const ws = new WebSocket(wsUrl)
90-
ws.addEventListener('open', () => {
91-
location.reload()
92-
})
93-
}, 300)
97+
contact(() => location.reload())
9498
}
9599
})
96100

0 commit comments

Comments
 (0)