From 5fcf8022a1789727c0de0f7c2933f814d3d3c2f9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 14 Oct 2020 15:30:40 +0200 Subject: [PATCH] store the url on every change, as PWAs don't trigger beforeunload ... ... when (force) closed --- src/main.js | 12 +++--------- src/ui/web/dom/History.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main.js b/src/main.js index af686d2b..d482c078 100644 --- a/src/main.js +++ b/src/main.js @@ -118,7 +118,8 @@ export async function main(container, paths, legacyExtras) { } const navigation = createNavigation(); - const urlRouter = createRouter({navigation, history: new History()}); + const history = new History(); + const urlRouter = createRouter({navigation, history}); urlRouter.attach(); const vm = new RootViewModel({ @@ -142,18 +143,11 @@ export async function main(container, paths, legacyExtras) { navigation }); window.__brawlViewModel = vm; - const lastUrlHash = window.localStorage?.getItem("hydrogen_last_url_hash"); - await vm.load(lastUrlHash); + await vm.load(history.getLastUrl()); // TODO: replace with platform.createAndMountRootView(vm, container); const view = new RootView(vm); container.appendChild(view.mount()); - window.addEventListener("beforeunload", storeUrlHashOnClose); } catch(err) { console.error(`${err.message}:\n${err.stack}`); } } - -function storeUrlHashOnClose() { - window.localStorage?.setItem("hydrogen_last_url_hash", document.location.hash); - window.removeEventListener("beforeunload", storeUrlHashOnClose); -} diff --git a/src/ui/web/dom/History.js b/src/ui/web/dom/History.js index 5a5794ae..8bc433cd 100644 --- a/src/ui/web/dom/History.js +++ b/src/ui/web/dom/History.js @@ -29,6 +29,7 @@ export class History extends BaseObservableValue { return; } this.emit(this.get()); + this._storeHash(this.get()); } get() { @@ -38,11 +39,13 @@ export class History extends BaseObservableValue { /** does not emit */ replaceUrl(url) { window.history.replaceState(null, null, url); + this._storeHash(url); } /** does not emit */ pushUrl(url) { window.history.pushState(null, null, url); + this._storeHash(url); // const hash = this.urlAsPath(url); // // important to check before we expect an echo // // as setting the hash to it's current value doesn't @@ -79,4 +82,12 @@ export class History extends BaseObservableValue { window.removeEventListener('hashchange', this._boundOnHashChange); this._boundOnHashChange = null; } + + _storeHash(hash) { + window.localStorage?.setItem("hydrogen_last_url_hash", hash); + } + + getLastUrl() { + return window.localStorage?.getItem("hydrogen_last_url_hash"); + } }