From e99096a99bfeaca174ca402fb884c96630e2c325 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Thu, 24 Jun 2021 16:01:59 +0200 Subject: [PATCH] Clean up initial router handling If the current state can't be used to generate a path just fall back to the current path --- packages/app-shell/src/app-shell.js | 3 ++- packages/app-shell/src/router.js | 18 +++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/app-shell/src/app-shell.js b/packages/app-shell/src/app-shell.js index c6b136c4..616079c7 100644 --- a/packages/app-shell/src/app-shell.js +++ b/packages/app-shell/src/app-shell.js @@ -219,10 +219,11 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { this.router = new Router(routes, { routeName: 'mainRoute', getState: () => { - return { + let state = { component: this.activeView, lang: this.lang, }; + return state; }, setState: (state) => { this.updateLangIfChanged(state.lang); diff --git a/packages/app-shell/src/router.js b/packages/app-shell/src/router.js index c05b899c..8970e175 100644 --- a/packages/app-shell/src/router.js +++ b/packages/app-shell/src/router.js @@ -44,9 +44,7 @@ export class Router { // In case of a router redirect, set the new location if (newPathname !== oldPathName) { const referrerUrl = location.href; - if (!newPathname.endsWith('/root')) { - window.history.replaceState({}, '', newPathname); - } + window.history.replaceState({}, '', newPathname); this.dispatchLocationChanged(referrerUrl); } this.setState(page); @@ -68,9 +66,7 @@ export class Router { if (newPathname === oldPathname) return; const referrerUrl = location.href; - if (!newPathname.endsWith('/root')) { - window.history.pushState({}, '', newPathname); - } + window.history.pushState({}, '', newPathname); this.dispatchLocationChanged(referrerUrl); }); } @@ -107,12 +103,12 @@ export class Router { partialState = {}; let combined = {...currentState, ...partialState}; - // prevent: Uncaught TypeError: Expected "component" to match "[^\/#\?]+?", but got "" - if (combined.component === '') { - combined.component = 'root'; + try { + return generateUrls(this.router)(this.routeName, combined); + } catch { + // XXX: In case we have partial state this will fail, just return the old path + return location.pathname; } - - return generateUrls(this.router)(this.routeName, combined); } dispatchLocationChanged(referrerUrl = "") { -- GitLab