NuxtN
Nuxt3mo ago
5 replies
sebbykaz

onMounted triggering twice? (WebSocket establishing issues)

Hey everyone, long time Nuxter, first time writing in the Nuxt Discord.

I've come across a very interesting bug in a Nuxt app I'm making:
If I attempt to create a WebSocket connection in the onMounted() hook, it's actually triggered twice and it fires the WebSocket creation twice.

It's causing a very interesting race condition that prevents the WebSocket from establishing consistently.
Watching a WireShark trace to the WebSocket server shows that it communicates to the server to initialise a connection, then immediately closes it and then attempts to re-create it when it the page silently reloads.

I've only been able to isolate the issue of the Websocket refusing to establish mainly to Safari on macOS and iOS, but Chrome and Firefox do send close signals to the WebSocket server, but manage to connect.
Refreshing the page within a second allows the WebSocket to connect successfully (Safari)

I have a repo where I can get it to occur, but it happens more consistently if the WebSocket server is external (not Nitro)

This issue appeared a couple of months ago for me, but I only tracked down what was going today.
Anyone have any pointers for me? Or should I possibly make a GitHub issue for it?
Was this page helpful?