Skip to content
Snippets Groups Projects
Commit dd440e61 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Introduce a default routing state

In case the current state is the default one we don't redirect.
This gets rid of the initial redirect/histroy entry when loading the page.
parent e99096a9
No related branches found
No related tags found
1 merge request!65Clean up initial router handling and add a default state
Pipeline #48255 passed
...@@ -228,6 +228,12 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { ...@@ -228,6 +228,12 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) {
setState: (state) => { setState: (state) => {
this.updateLangIfChanged(state.lang); this.updateLangIfChanged(state.lang);
this.switchComponent(state.component); this.switchComponent(state.component);
},
getDefaultState: () => {
return {
lang: 'de',
component: this.routes[0],
};
} }
}, { }, {
baseUrl: new URL(this.basePath, window.location).pathname.replace(/\/$/, ''), baseUrl: new URL(this.basePath, window.location).pathname.replace(/\/$/, ''),
......
...@@ -17,12 +17,14 @@ export class Router { ...@@ -17,12 +17,14 @@ export class Router {
constructor(routes, options, unioptions) { constructor(routes, options, unioptions) {
this.getState = options.getState; this.getState = options.getState;
this.setState = options.setState; this.setState = options.setState;
this.getDefaultState = options.getDefaultState;
// XXX: We only have one route atm // XXX: We only have one route atm
// If we need more we need to pass the route name to each function // If we need more we need to pass the route name to each function
this.routeName = options.routeName; this.routeName = options.routeName;
console.assert(this.getState); console.assert(this.getState);
console.assert(this.setState); console.assert(this.setState);
console.assert(this.getDefaultState);
console.assert(this.routeName); console.assert(this.routeName);
// https://github.com/kriasoft/universal-router // https://github.com/kriasoft/universal-router
...@@ -39,6 +41,7 @@ export class Router { ...@@ -39,6 +41,7 @@ export class Router {
*/ */
setStateFromCurrentLocation() { setStateFromCurrentLocation() {
const oldPathName = location.pathname; const oldPathName = location.pathname;
this.router.resolve({pathname: oldPathName}).then(page => { this.router.resolve({pathname: oldPathName}).then(page => {
const newPathname = this.getPathname(page); const newPathname = this.getPathname(page);
// In case of a router redirect, set the new location // In case of a router redirect, set the new location
...@@ -46,6 +49,8 @@ export class Router { ...@@ -46,6 +49,8 @@ export class Router {
const referrerUrl = location.href; const referrerUrl = location.href;
window.history.replaceState({}, '', newPathname); window.history.replaceState({}, '', newPathname);
this.dispatchLocationChanged(referrerUrl); this.dispatchLocationChanged(referrerUrl);
} else if (this.isBasePath(oldPathName)) {
page = this.getDefaultState();
} }
this.setState(page); this.setState(page);
}).catch((e) => { }).catch((e) => {
...@@ -54,6 +59,10 @@ export class Router { ...@@ -54,6 +59,10 @@ export class Router {
}); });
} }
isBasePath(pathname) {
return pathname.replace(/\/$/, '') === this.router.baseUrl.replace(/\/$/, '');
}
/** /**
* Update the router after some internal state change. * Update the router after some internal state change.
*/ */
...@@ -65,6 +74,12 @@ export class Router { ...@@ -65,6 +74,12 @@ export class Router {
const oldPathname = location.pathname; const oldPathname = location.pathname;
if (newPathname === oldPathname) if (newPathname === oldPathname)
return; return;
const defaultPathname = this.getPathname(this.getDefaultState());
if (newPathname === defaultPathname && this.isBasePath(oldPathname)) {
return;
}
const referrerUrl = location.href; const referrerUrl = location.href;
window.history.pushState({}, '', newPathname); window.history.pushState({}, '', newPathname);
this.dispatchLocationChanged(referrerUrl); this.dispatchLocationChanged(referrerUrl);
......
...@@ -19,6 +19,7 @@ suite('router', () => { ...@@ -19,6 +19,7 @@ suite('router', () => {
routeName: 'foo', routeName: 'foo',
getState: () => { return {}; }, getState: () => { return {}; },
setState: (state) => { }, setState: (state) => { },
getDefaultState: () => { return {}; },
}); });
router.setStateFromCurrentLocation(); router.setStateFromCurrentLocation();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment