- Visit http://localhost:3000/ws/html
- The browser tries to connect to the websocket server
- The server receives the request, logs "Websocket trying to connect!"
- The server handles the request (in this case returns a 501)
- The
error
orconnect
event of the websocket is triggered
- Visit http://localhost:3000/
- Close the tab
- Visit http://localhost:3000/ws/html
- The browser tries to connect to the websocket server
- The server doesn't receive the request, doesn't log "Websocket trying to connect!"
- No event is triggered on the websocket
How? Well, the problem is caused by app?.getRequestHandler()()
.
This function adds a listener onto the server matching the upgrade
event.
This can be prevented by overriding the function that's used to add an event listener into caching the function for the event handler into a local variable.
Now you can call this variable if the url matches /_next/webpack-hmr
,
otherwise you can use your own websocket logic.