sanity icon indicating copy to clipboard operation
sanity copied to clipboard

fix: `WebSocket is closed before the connection is established` warning

Open stipsan opened this issue 7 months ago • 3 comments

Description

In production we have this very annoying warning:

WebSocket connection to 'wss://:projectId.api.sanity.io/v2022-06-30/socket/:dataset?tag=sanity.studio' failed: WebSocket is closed before the connection is established.

Here's both the warning, and the Network tab, showing that a WSS were initiated and discarded:

image

After moving the useGlobalPresence and useDocumentPresence hooks to useObservable from react-rx, the problem is resolved in production:

image

It's not resolved when using sanity dev. I'm not sure yet why that is, but I suspect it's a timing problem with the observable in one of its dependencies.

The change from useMemo to useState in ResourceCacheProvider is to reduce possible issues in StrictMode as the hook is called twice there: https://react.dev/reference/react/useMemo#my-calculation-runs-twice-on-every-re-render

What to review

It makes sense?

Testing

Test that global presence and document presence still works ok (it does on my tests, it's nice with a second pair of eyes).

Notes for release

No longer warns about WebSocket is closed before the connection is established in production.

stipsan avatar Jul 22 '24 16:07 stipsan