Skip to content

Commit bcd4e21

Browse files
committed
Prevent navigation back to homepage on repeated back button click
1 parent 47e1030 commit bcd4e21

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/client/Main.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,22 @@ class Client {
599599
if (!this.gameStop()) {
600600
console.info("Player is active, ask before leaving game");
601601

602-
// Rollback navigator history (here so the confirm dialog also works on mobile)
603-
history.pushState(null, "", this.currentUrl);
602+
// Rollback navigator history to stay in game on cancel
603+
// Before calling confirm so dialog also shows on mobile
604+
history.replaceState(null, "", this.currentUrl);
604605

605606
const isConfirmed = confirm(
606607
translateText("help_modal.exit_confirmation"),
607608
);
608609

609610
if (!isConfirmed) {
611+
// Rebuild stack:
612+
// - prevent being put back homepage when clicking back button again after cancelling
613+
// - replaceState instead of 2x pushState for cleaner history
614+
// Can't prevent browser blocking popState on clicking back button again without in-game click first
615+
// - preventable by only having pushState this.currentUrl here, which breaks confirm on mobile
616+
history.replaceState(null, "", window.location.origin + "#refresh");
617+
history.pushState(null, "", this.currentUrl);
610618
return;
611619
}
612620
}

0 commit comments

Comments
 (0)