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:
+31
-5
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user