[FIX] router: properly redirect if necessary when started

This commit is contained in:
Géry Debongnie
2019-08-05 16:00:58 +02:00
parent 0a276fbf1d
commit 6cd5ec10f0
2 changed files with 24 additions and 2 deletions
+10 -2
View File
@@ -101,6 +101,10 @@ export class Router {
if (result.type === "match") {
this.currentRoute = result.route;
this.currentParams = result.params;
const currentPath = this.routeToPath(result.route, result.params);
if (currentPath !== this.currentPath()) {
this.setUrlFromPath(currentPath);
}
}
}
@@ -110,8 +114,7 @@ export class Router {
const result = await this.matchAndApplyRules(path);
if (result.type === "match") {
const finalPath = this.routeToPath(result.route, result.params);
const url = location.origin + finalPath;
history.pushState({}, path, url);
this.setUrlFromPath(finalPath);
this.currentRoute = result.route;
this.currentParams = result.params;
} else if (result.type === "nomatch") {
@@ -136,6 +139,11 @@ export class Router {
// Private helpers
//--------------------------------------------------------------------------
private setUrlFromPath(path: string) {
const url = location.origin + path;
window.history.pushState({}, path, url);
}
private validateDestination(dest: Destination) {
if ((!dest.path && !dest.to) || (dest.path && dest.to)) {
throw new Error(`Invalid destination: ${JSON.stringify(dest)}`);