Skip to content

Commit c1a1583

Browse files
authored
Fix reconnection loop when devserver is offline (#56698)
Ensures the webpack-hmr socket is not reconnected infinitely while (and very quickly) when the dev server is offline. I've implemented a gradual backoff, after 5 tries at 1 second it'll try 20 times at 5 seconds. After those 25 retries it reloads the page as something must be wrong (i.e. the server is offline). <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 7d8cf1f commit c1a1583

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/next/src/client/dev/error-overlay/websocket.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ export function sendMessage(data: string) {
2626
return source.send(data)
2727
}
2828

29+
let reconnections = 0
30+
2931
export function connectHMR(options: { path: string; assetPrefix: string }) {
3032
function init() {
3133
if (source) source.close()
3234

3335
function handleOnline() {
36+
reconnections = 0
3437
window.console.log('[HMR] connected')
3538
}
3639

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

48+
let timer: ReturnType<typeof setTimeout>
4549
function handleDisconnect() {
4650
source.onerror = null
4751
source.onclose = null
4852
source.close()
49-
init()
53+
reconnections++
54+
// After 25 reconnects we'll want to reload the page as it indicates the dev server is no longer running.
55+
if (reconnections > 25) {
56+
window.location.reload()
57+
return
58+
}
59+
60+
clearTimeout(timer)
61+
// Try again after 5 seconds
62+
timer = setTimeout(init, reconnections > 5 ? 5000 : 1000)
5063
}
5164

5265
const { hostname, port } = location

0 commit comments

Comments
 (0)