From f1b56268bbd487b59d09da99641a091d3c8e91ad Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 14 May 2025 21:15:55 +0100 Subject: [PATCH] setup: fix url-based navigation popstate messages from the browser don't have an event.state, and i don't even know what the overridePopState stuff is trying to do. If event.state is null, try window.location.hash, or the last part of the URL path. --- ts/modules/pages.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ts/modules/pages.ts b/ts/modules/pages.ts index edf21f3..cc29c9a 100644 --- a/ts/modules/pages.ts +++ b/ts/modules/pages.ts @@ -32,7 +32,15 @@ export class PageManager { private _onpopstate = (event: PopStateEvent) => { let name = event.state; - if (!this.pages.has(event.state)) { + if (name == null) { + // Attempt to use hash from URL, if it isn't there, try the last part of the URL. + if (window.location.hash && window.location.hash.charAt(0) == "#") { + name = window.location.hash.substring(1); + } else { + name = window.location.pathname.split("/").filter(Boolean).at(-1); + } + } + if (!this.pages.has(name)) { name = this.pageList[0] } let success = this.pages.get(name).show(); @@ -73,7 +81,6 @@ export class PageManager { } loadPage (p: Page) { - console.log("loading page with", p.name || this.defaultName, p.title, p.url + window.location.search); window.history.pushState(p.name || this.defaultName, p.title, p.url + window.location.search); }