fuzzyworld

word-waltz / commit

pasta

58ec0f1f4f9fa328bc883848ef8d6238839e9685 · Pavel Safronov · 2026-03-26 21:06:12 -0700 · parents 57e1cfbd5b8f

pasta
 src/main.ts | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/main.ts b/src/main.ts
index ea97328..67ed717 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -730,7 +730,10 @@ function showError(app: HTMLElement, message: string): void {
 // Bootstrap
 // ---------------------------------------------------------------------------
 
+let bootstrapSeq = 0;
+
 async function bootstrap(): Promise<void> {
+    const mySeq = ++bootstrapSeq;
     const app = document.getElementById('app');
     if (!app) throw new Error('#app element not found');
 
@@ -801,6 +804,10 @@ async function bootstrap(): Promise<void> {
     const role = myRole as PlayerId;
     const shareUrl = buildLink(gameId, key);
     const inviteUrl = buildLink(gameId, key, 'b');
+
+    // Another navigation happened while we were awaiting — bail out.
+    if (mySeq !== bootstrapSeq) return;
+
     setShareButton(inviteUrl);
 
     // Register the service worker and subscribe to push notifications.
@@ -843,6 +850,7 @@ async function bootstrap(): Promise<void> {
 // full page reload. This works regardless of the deployment base path.
 document.getElementById('home-link')?.addEventListener('click', (e) => {
     e.preventDefault();
+    ++bootstrapSeq;
     unsubscribeGame?.();
     unsubscribeGame = null;
     cleanupView?.();
@@ -862,6 +870,7 @@ document.addEventListener('visibilitychange', () => {
 // Re-run bootstrap whenever the fragment changes (e.g. after game creation
 // navigates to the game URL — a hash-only change doesn't reload the page).
 window.addEventListener('hashchange', () => {
+    ++bootstrapSeq;
     unsubscribeGame?.();
     unsubscribeGame = null;
     cleanupView?.();