Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WebSocket is closed before the connection is established warning #7227

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Jul 22, 2024

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 stipsan requested a review from a team as a code owner July 22, 2024 16:58
@stipsan stipsan requested review from juice49 and removed request for a team July 22, 2024 16:58
Copy link

vercel bot commented Jul 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 29, 2024 9:55am
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 29, 2024 9:55am
test-compiled-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 29, 2024 9:55am
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 29, 2024 9:55am
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 29, 2024 9:55am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Aug 29, 2024 9:55am

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented Jul 22, 2024

Component Testing Report Updated Aug 29, 2024 9:52 AM (UTC)

❌ Failed Tests (2) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 44s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 9s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 31s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 36s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 16s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 10s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 3m 0s 0 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 43s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 47s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 14s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 8s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 25s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 17s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ❌ Failed (Inspect) 1m 32s 19 0 2
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 0s 0 3 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 0s 0 3 0

@stipsan stipsan force-pushed the fix-WebSocket-is-closed-before-the-connection-is-established branch from bb62db9 to 0bd30a5 Compare July 29, 2024 08:12
@stipsan stipsan force-pushed the fix-WebSocket-is-closed-before-the-connection-is-established branch from 0bd30a5 to e807718 Compare July 31, 2024 07:40
@stipsan stipsan force-pushed the fix-WebSocket-is-closed-before-the-connection-is-established branch from e807718 to 7d5915e Compare August 1, 2024 07:50
@runeb runeb requested review from ricokahler and removed request for juice49 August 5, 2024 17:52
@stipsan stipsan force-pushed the fix-WebSocket-is-closed-before-the-connection-is-established branch from 7d5915e to 21b3dbc Compare August 29, 2024 09:36
@stipsan stipsan enabled auto-merge August 29, 2024 09:43
@stipsan
Copy link
Member Author

stipsan commented Aug 29, 2024

Closing this as after I rebased this PR onto next it now makes matters worse.
Instead of removing the console.warn in production, while dev mode still has a single console.warn, production now logs twice 🤯:
image

Dev mode still logs once. I don't have time to bisect and chase what commit on next that were made since this PR originally opened, and now no longer fixes the issue, so I'll close this and leave it for @sanity-io/studio-dx to dive in and get to the bottom of the production console.warn 🙌 💖

@stipsan stipsan closed this Aug 29, 2024
auto-merge was automatically disabled August 29, 2024 09:57

Pull request was closed

@stipsan stipsan deleted the fix-WebSocket-is-closed-before-the-connection-is-established branch August 29, 2024 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant