From e017a87e12e750ea23b62ea8167bcbaef0d5f8f6 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 29 Dec 2025 14:53:42 -0600 Subject: [PATCH] fix(web): prevent stopGame from being called on non-game page transitions Only stop the game when navigating away from the game page (/) to avoid WASM runtime errors when navigating between /leaderboard and /download. Add error handling to gracefully handle crashes during page transitions. --- web/pages/+onPageTransitionEnd.ts | 35 ++++++++++++++++------------- web/pages/+onPageTransitionStart.ts | 17 +++++++++----- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/web/pages/+onPageTransitionEnd.ts b/web/pages/+onPageTransitionEnd.ts index 7dd66ba..21a67df 100644 --- a/web/pages/+onPageTransitionEnd.ts +++ b/web/pages/+onPageTransitionEnd.ts @@ -23,23 +23,26 @@ function restartGame() { const win = getPacmanWindow(); const module = win.Module; - if (module?._restart_game) { - const canvas = document.getElementById("canvas") as HTMLCanvasElement | null; - if (!canvas) { - console.error("Canvas element not found during game restart"); - return; - } + if (!module?._restart_game) { + console.warn("Game restart function not available (WASM may not be initialized)"); + return; + } - // Update canvas reference BEFORE restart - App::new() will read from Module.canvas - module.canvas = canvas; - // SDL2's Emscripten backend reads this for canvas lookup - win.SDL_CANVAS_ID = "#canvas"; + const canvas = document.getElementById("canvas") as HTMLCanvasElement | null; + if (!canvas) { + console.error("Canvas element not found during game restart"); + return; + } - try { - console.log("Restarting game with fresh App instance"); - module._restart_game(); - } catch (error) { - console.error("Failed to restart game:", error); - } + // Update canvas reference BEFORE restart - App::new() will read from Module.canvas + module.canvas = canvas; + // SDL2's Emscripten backend reads this for canvas lookup + win.SDL_CANVAS_ID = "#canvas"; + + try { + console.log("Restarting game with fresh App instance"); + module._restart_game(); + } catch (error) { + console.error("Failed to restart game:", error); } } diff --git a/web/pages/+onPageTransitionStart.ts b/web/pages/+onPageTransitionStart.ts index 93a4f6e..1ca715c 100644 --- a/web/pages/+onPageTransitionStart.ts +++ b/web/pages/+onPageTransitionStart.ts @@ -10,11 +10,18 @@ export const onPageTransitionStart: OnPageTransitionStartAsync = async (pageCont setPendingNavigation(pageContext.urlPathname); document.querySelector("body")?.classList.add("page-is-transitioning"); - // Stop the game loop when navigating away from the game page - const win = getPacmanWindow(); - if (win.Module?._stop_game) { - console.log("Stopping game loop for page transition"); - win.Module._stop_game(); + // Only stop the game when navigating AWAY FROM the game page + // Don't stop when navigating between other pages (e.g., /leaderboard <-> /download) + if (window.location.pathname === "/") { + const win = getPacmanWindow(); + if (win.Module?._stop_game) { + try { + console.log("Stopping game loop for page transition"); + win.Module._stop_game(); + } catch (error) { + console.warn("Failed to stop game (game may have already crashed):", error); + } + } } // Wait for fade-out animation to complete before page content changes