Skip to content

Fix reconnection loop when devserver is offline #56698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/next/src/client/dev/error-overlay/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ export function sendMessage(data: string) {
return source.send(data)
}

let reconnections = 0

export function connectHMR(options: { path: string; assetPrefix: string }) {
function init() {
if (source) source.close()

function handleOnline() {
reconnections = 0
window.console.log('[HMR] connected')
}

Expand All @@ -42,11 +45,21 @@ export function connectHMR(options: { path: string; assetPrefix: string }) {
}
}

let timer: ReturnType<typeof setTimeout>
function handleDisconnect() {
source.onerror = null
source.onclose = null
source.close()
init()
reconnections++
// After 25 reconnects we'll want to reload the page as it indicates the dev server is no longer running.
if (reconnections > 25) {
window.location.reload()
return
}

clearTimeout(timer)
// Try again after 5 seconds
timer = setTimeout(init, reconnections > 5 ? 5000 : 1000)
}

const { hostname, port } = location
Expand Down