diff --git a/src/client/ClientGameRunner.ts b/src/client/ClientGameRunner.ts index bd377d35d9..aed695dc79 100644 --- a/src/client/ClientGameRunner.ts +++ b/src/client/ClientGameRunner.ts @@ -281,12 +281,15 @@ export class ClientGameRunner { * the window or navigate away during an active game session. * * @returns {boolean} `true` if the window close should be prevented - * (when the player is alive in the game), `false` otherwise + * (when the player is alive in the game or during spawn phase), `false` otherwise * (when the player is not alive or doesn't exist) */ public shouldPreventWindowClose(): boolean { - // Show confirmation dialog if player is alive in the game - return !!this.myPlayer?.isAlive(); + // gameView, not this.myPlayer, so it works before 1st click after spawn phase too + return ( + (this.gameView.myPlayer()?.isAlive() ?? false) || + this.gameView.inSpawnPhase() + ); } private async saveGame(update: WinUpdate) { diff --git a/src/client/Main.ts b/src/client/Main.ts index 8858b2f43f..e7a25f7c49 100644 --- a/src/client/Main.ts +++ b/src/client/Main.ts @@ -599,13 +599,23 @@ class Client { if (!this.gameStop()) { console.info("Player is active, ask before leaving game"); + // Rollback navigator history to stay in game on cancel + // Before calling confirm so dialog also shows on mobile + history.pushState(null, "", window.location.origin + "#refresh"); + history.pushState(null, "", this.currentUrl); + const isConfirmed = confirm( translateText("help_modal.exit_confirmation"), ); if (!isConfirmed) { - // Rollback navigator history - history.pushState(null, "", this.currentUrl); + // Rebuild stack: + // - prevent being put back to homepage when clicking back button again after cancelling + // - replaceState instead of 2x pushState for cleaner history + // Can't prevent browser blocking popState on clicking back button again without in-game click first + // - preventable by only having pushState this.currentUrl here, which breaks confirm on mobile + // history.replaceState(null, "", window.location.origin + "#refresh"); + // history.pushState(null, "", this.currentUrl); return; } }