forked from mystiq/hydrogen-web
store the url on every change, as PWAs don't trigger beforeunload ...
... when (force) closed
This commit is contained in:
parent
3d8dfc9635
commit
5fcf8022a1
2 changed files with 14 additions and 9 deletions
12
src/main.js
12
src/main.js
|
@ -118,7 +118,8 @@ export async function main(container, paths, legacyExtras) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const navigation = createNavigation();
|
const navigation = createNavigation();
|
||||||
const urlRouter = createRouter({navigation, history: new History()});
|
const history = new History();
|
||||||
|
const urlRouter = createRouter({navigation, history});
|
||||||
urlRouter.attach();
|
urlRouter.attach();
|
||||||
|
|
||||||
const vm = new RootViewModel({
|
const vm = new RootViewModel({
|
||||||
|
@ -142,18 +143,11 @@ export async function main(container, paths, legacyExtras) {
|
||||||
navigation
|
navigation
|
||||||
});
|
});
|
||||||
window.__brawlViewModel = vm;
|
window.__brawlViewModel = vm;
|
||||||
const lastUrlHash = window.localStorage?.getItem("hydrogen_last_url_hash");
|
await vm.load(history.getLastUrl());
|
||||||
await vm.load(lastUrlHash);
|
|
||||||
// TODO: replace with platform.createAndMountRootView(vm, container);
|
// TODO: replace with platform.createAndMountRootView(vm, container);
|
||||||
const view = new RootView(vm);
|
const view = new RootView(vm);
|
||||||
container.appendChild(view.mount());
|
container.appendChild(view.mount());
|
||||||
window.addEventListener("beforeunload", storeUrlHashOnClose);
|
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(`${err.message}:\n${err.stack}`);
|
console.error(`${err.message}:\n${err.stack}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeUrlHashOnClose() {
|
|
||||||
window.localStorage?.setItem("hydrogen_last_url_hash", document.location.hash);
|
|
||||||
window.removeEventListener("beforeunload", storeUrlHashOnClose);
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ export class History extends BaseObservableValue {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.emit(this.get());
|
this.emit(this.get());
|
||||||
|
this._storeHash(this.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
get() {
|
get() {
|
||||||
|
@ -38,11 +39,13 @@ export class History extends BaseObservableValue {
|
||||||
/** does not emit */
|
/** does not emit */
|
||||||
replaceUrl(url) {
|
replaceUrl(url) {
|
||||||
window.history.replaceState(null, null, url);
|
window.history.replaceState(null, null, url);
|
||||||
|
this._storeHash(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** does not emit */
|
/** does not emit */
|
||||||
pushUrl(url) {
|
pushUrl(url) {
|
||||||
window.history.pushState(null, null, url);
|
window.history.pushState(null, null, url);
|
||||||
|
this._storeHash(url);
|
||||||
// const hash = this.urlAsPath(url);
|
// const hash = this.urlAsPath(url);
|
||||||
// // important to check before we expect an echo
|
// // important to check before we expect an echo
|
||||||
// // as setting the hash to it's current value doesn't
|
// // 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);
|
window.removeEventListener('hashchange', this._boundOnHashChange);
|
||||||
this._boundOnHashChange = null;
|
this._boundOnHashChange = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_storeHash(hash) {
|
||||||
|
window.localStorage?.setItem("hydrogen_last_url_hash", hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
getLastUrl() {
|
||||||
|
return window.localStorage?.getItem("hydrogen_last_url_hash");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue