-
Notifications
You must be signed in to change notification settings - Fork 4
fix: close WebSocket on pagehide to support bfcache #162
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,11 @@ export default class Socket implements Transport { | |||||
| */ | ||||||
| private reconnectionAttempts: number; | ||||||
|
|
||||||
| /** | ||||||
| * Page hide event handler reference (for removal) | ||||||
| */ | ||||||
| private pageHideHandler: () => void; | ||||||
|
|
||||||
| /** | ||||||
| * Creates new Socket instance. Setup initial socket params. | ||||||
| * | ||||||
|
|
@@ -77,6 +82,11 @@ export default class Socket implements Transport { | |||||
| this.reconnectionTimeout = reconnectionTimeout; | ||||||
| this.reconnectionAttempts = reconnectionAttempts; | ||||||
|
|
||||||
| this.pageHideHandler = () => { | ||||||
| log('Page entering bfcache, closing WebSocket', 'info'); | ||||||
| this.close(); | ||||||
| }; | ||||||
|
Comment on lines
+85
to
+88
|
||||||
|
|
||||||
| this.eventsQueue = []; | ||||||
| this.ws = null; | ||||||
|
|
||||||
|
|
@@ -120,7 +130,21 @@ export default class Socket implements Transport { | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Create new WebSocket connection and setup event listeners | ||||||
| * Remove window event listeners | ||||||
| */ | ||||||
| public destroyListeners(): void { | ||||||
|
||||||
| public destroyListeners(): void { | |
| private destroyListeners(): void { |
Copilot
AI
Feb 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new bfcache behavior (listening to pagehide and closing the WebSocket) isn’t covered by tests. Since this package already uses Vitest + jsdom, it should be possible to add a unit test that mocks WebSocket, dispatches a pagehide event (with persisted: true if you adopt that check), and asserts that close() is called and that a later send() recreates the connection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think users will be happy to see this log in their application. Let's remove it after testing.