However, unlike the unload event, there is a legitimate use case for the beforeunload event: the scenario where the user has entered unsaved data that will be lost if the page is unloaded.
It is recommended that developers listen for beforeunload only in this scenario, and only when they actually have unsaved changes, so as to minimize the effect on performance. See the Examples section below for an example of this.
So I can listen for this event while the player is in game and remove it as soon as the game is finished.
handle it with svelte:window
MDN Page
So I can listen for this event while the player is in game and remove it as soon as the game is finished.