mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] router: properly redirect if necessary when started
This commit is contained in:
+10
-2
@@ -101,6 +101,10 @@ export class Router {
|
|||||||
if (result.type === "match") {
|
if (result.type === "match") {
|
||||||
this.currentRoute = result.route;
|
this.currentRoute = result.route;
|
||||||
this.currentParams = result.params;
|
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);
|
const result = await this.matchAndApplyRules(path);
|
||||||
if (result.type === "match") {
|
if (result.type === "match") {
|
||||||
const finalPath = this.routeToPath(result.route, result.params);
|
const finalPath = this.routeToPath(result.route, result.params);
|
||||||
const url = location.origin + finalPath;
|
this.setUrlFromPath(finalPath);
|
||||||
history.pushState({}, path, url);
|
|
||||||
this.currentRoute = result.route;
|
this.currentRoute = result.route;
|
||||||
this.currentParams = result.params;
|
this.currentParams = result.params;
|
||||||
} else if (result.type === "nomatch") {
|
} else if (result.type === "nomatch") {
|
||||||
@@ -136,6 +139,11 @@ export class Router {
|
|||||||
// Private helpers
|
// Private helpers
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private setUrlFromPath(path: string) {
|
||||||
|
const url = location.origin + path;
|
||||||
|
window.history.pushState({}, path, url);
|
||||||
|
}
|
||||||
|
|
||||||
private validateDestination(dest: Destination) {
|
private validateDestination(dest: Destination) {
|
||||||
if ((!dest.path && !dest.to) || (dest.path && dest.to)) {
|
if ((!dest.path && !dest.to) || (dest.path && dest.to)) {
|
||||||
throw new Error(`Invalid destination: ${JSON.stringify(dest)}`);
|
throw new Error(`Invalid destination: ${JSON.stringify(dest)}`);
|
||||||
|
|||||||
@@ -161,4 +161,18 @@ describe("beforeRouteEnter", () => {
|
|||||||
expect(window.location.pathname).toBe("/some/pathc");
|
expect(window.location.pathname).toBe("/some/pathc");
|
||||||
expect(router.currentRouteName).toBe("routec");
|
expect(router.currentRouteName).toBe("routec");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("navigation is initially redirected if guard decides so", async () => {
|
||||||
|
expect(window.location.pathname).toBe("/");
|
||||||
|
const guard = jest.fn(() => {return {to: "otherroute"}});
|
||||||
|
router = new TestRouter(env, [
|
||||||
|
{ name: "landing", path: "/", beforeRouteEnter: guard},
|
||||||
|
{ name: "otherroute", path: "/some/other/route"}
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(window.location.pathname).toBe("/");
|
||||||
|
|
||||||
|
await router.start();
|
||||||
|
expect(window.location.pathname).toBe("/some/other/route");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user