Skip to content

Commit

Permalink
Replace the deprecated unload event with pagehide
Browse files Browse the repository at this point in the history
The unloadHandler is called by an eventListener for the 'unload' event when the user
moves away from the page being served by y-websocket.  However, the unload event is now
deprecated (see https://web.dev/articles/bfcache?utm_source=lighthouse&utm_medium=devtools#never-use-the-unload-event )
and the recommendation is to replace it with the 'pagehide' event.  This PR does that, in both locations
in y-websocket.js where it is used.
  • Loading branch information
micrology committed Jan 14, 2024
1 parent 1f6716b commit 47bb099
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class WebsocketProvider extends Observable {
)
}
if (typeof window !== 'undefined') {
window.addEventListener('unload', this._unloadHandler)
window.addEventListener('pagehide', this._unloadHandler)
} else if (typeof process !== 'undefined') {
process.on('exit', this._unloadHandler)
}
Expand Down Expand Up @@ -401,7 +401,7 @@ export class WebsocketProvider extends Observable {
clearInterval(this._checkInterval)
this.disconnect()
if (typeof window !== 'undefined') {
window.removeEventListener('unload', this._unloadHandler)
window.removeEventListener('pagehide', this._unloadHandler)
} else if (typeof process !== 'undefined') {
process.off('exit', this._unloadHandler)
}
Expand Down

0 comments on commit 47bb099

Please sign in to comment.