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 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 parts = hash.split("/").filter(Boolean);
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(() => {
parseHash();
const handler = () => parseHash();
parseRoute();
const handler = () => parseRoute();
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>