AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack HMR WebSocket Closure On First Load Breaks Initial Client-Side Navigation

In Next.js dev with Turbopack, the HMR WebSocket connection is closed before it is established, causing the first client-side navigation after a fresh page load to delay or fall back to a full page reload. After a manual reload, the connection succeeds and navigation works normally.

highConfidence 65%Next.js

Origin Analysis

The Turbopack HMR client sends initialization or ping messages before the WebSocket connection is fully open, causing the browser to close the socket with the error 'WebSocket is closed before the connection is established'. This prevents the client-side router from receiving the HMR ready signal, so the first navigation cannot complete as a client-side transition and instead triggers a full page load.
1. Clone the reproduction repository. 2. Run `npm install`. 3. Start `npm run dev` (default Turbopack). 4. Open the app in a new Chrome tab/private window. 5. Click a navigation link (e.g., `/about` or `/contact`). 6. Observe that the first navigation stalls or falls back to a full page load. 7. Reload the page once; navigation works normally. 8. Verify the issue does not occur with `next dev --webpack` or production build.

Fixing Code Block

Edge Case Audit

The pending message buffer could grow unbounded if the socket never opens (e.g., network failure). Add a maximum buffer size and a retry/backoff strategy. Test across Chrome, Firefox, and Safari; ensure no regression in hot reload updates. If issues arise, roll back this change and investigate server-side Turbopack HMR handshake ordering.

Ecosystem Topology