Fix Stop permanently: navigate to real server path /game/{id} to force full page unload, add pathname routing to SPA

This commit is contained in:
David Alvarez
2026-05-23 19:21:50 +02:00
parent a3ba5f8098
commit f6a1dccdf2
2 changed files with 34 additions and 8 deletions
+31 -5
View File
@@ -8,7 +8,28 @@
let route = $state("/"); let route = $state("/");
let gameId = $state(null); let gameId = $state(null);
function parseHash() { function parseRoute() {
// Check pathname first (full page navigations from Stop button)
const path = window.location.pathname.replace(/\/+$/, "") || "/";
if (path !== "/") {
const parts = path.split("/").filter(Boolean);
if (parts.length >= 2) {
const [routeName, routeId] = parts;
if (routeName === "game" || routeName === "play") {
route = "/" + routeName;
gameId = routeId;
// Sync the hash so hash-based links still work
window.history.replaceState(null, "", "#/" + routeName + "/" + routeId);
return;
}
}
// Unknown path, reset to home
route = "/";
gameId = null;
return;
}
// Fall back to hash-based routing (SPA transitions)
const hash = window.location.hash.slice(1) || "/"; const hash = window.location.hash.slice(1) || "/";
const parts = hash.split("/").filter(Boolean); const parts = hash.split("/").filter(Boolean);
if (parts.length === 0 || (parts.length === 1 && parts[0] === "")) { if (parts.length === 0 || (parts.length === 1 && parts[0] === "")) {
@@ -26,12 +47,17 @@
} }
} }
// Listen for hash changes // Listen for hash changes and page loads
$effect(() => { $effect(() => {
parseHash(); parseRoute();
const handler = () => parseHash(); const handler = () => parseRoute();
window.addEventListener("hashchange", handler); window.addEventListener("hashchange", handler);
return () => window.removeEventListener("hashchange", handler); // Also listen for popstate (for browser back/forward)
window.addEventListener("popstate", handler);
return () => {
window.removeEventListener("hashchange", handler);
window.removeEventListener("popstate", handler);
};
}); });
</script> </script>
+3 -3
View File
@@ -65,9 +65,9 @@
} }
function stopEmulator() { function stopEmulator() {
// Full page reload to guarantee all workers, AudioContexts, // Navigate to an actual server path to force full page unload
// and WebAssembly threads are fully terminated // which kills Web Workers, AudioContexts, and WASM threads
window.location.href = `/#/game/${game.id}`; window.location.href = `/game/${game.id}`;
} }
$effect(() => { load(); }); $effect(() => { load(); });