Files
Pac-Man/web/pages/+onPageTransitionStart.ts
Xevion e017a87e12 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.
2025-12-29 14:53:42 -06:00

30 lines
1.1 KiB
TypeScript

import type { OnPageTransitionStartAsync } from "vike/types";
import { getPacmanWindow } from "@/lib/pacman";
import { setPendingNavigation } from "@/lib/navigation";
// Must match --transition-duration in layouts/tailwind.css
const TRANSITION_DURATION = 200;
export const onPageTransitionStart: OnPageTransitionStartAsync = async (pageContext) => {
console.log("Page transition start");
setPendingNavigation(pageContext.urlPathname);
document.querySelector("body")?.classList.add("page-is-transitioning");
// 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
await new Promise((resolve) => setTimeout(resolve, TRANSITION_DURATION));
};