store the url on every change, as PWAs don't trigger beforeunload ...

... when (force) closed
This commit is contained in:
Bruno Windels 2020-10-14 15:30:40 +02:00
parent 3d8dfc9635
commit 5fcf8022a1
2 changed files with 14 additions and 9 deletions

View file

@ -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);
}

View file

@ -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");
}
}