cloudflare / cloudflare/partykit

The "onClose" handler is not triggered if the component using "useWs" unmounts

Open
#400 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.3k
Forks
79
PR merge metrics
No merged PRs in 30d

Description

Hi,
I just encountered an issue while using your "useWs" implementation.
If a component using "useWs" hook unmount, the "onClose" handler is not called.

This is due to the following cleanup code : https://github.com/cloudflare/partykit/blob/main/packages/partysocket/src/use-handlers.ts#L35-L40

The event "close" event is no longer listened to, so if the websocket is closed due to an unmount, the "onClose" handler is never called. The only current solution to this problem is to create a custom cleanup function on unmount which is not ideal as the hook "useWs" already provides one.

Here is a minimal reproduction :
https://github.com/Yovach/partykit-bug-reproduction-unmount-close

Thanks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.