From 2aa705f5c8efcbd76b3bcb57bb6dd230193fc033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 27 Oct 2019 21:11:11 +0100 Subject: [PATCH] [FIX] router: preserve pathname in hash mode closes #397 --- src/router/router.ts | 2 +- tests/router/router.test.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/router/router.ts b/src/router/router.ts index 7eedb6a1..b53f2128 100644 --- a/src/router/router.ts +++ b/src/router/router.ts @@ -148,7 +148,7 @@ export class Router { //-------------------------------------------------------------------------- private setUrlFromPath(path: string) { - const separator = this.mode === "hash" ? "/" : ""; + const separator = this.mode === "hash" ? location.pathname : ""; const url = location.origin + separator + path; if (url !== window.location.href) { window.history.pushState({}, path, url); diff --git a/tests/router/router.test.ts b/tests/router/router.test.ts index 6e63ecb1..03256eb1 100644 --- a/tests/router/router.test.ts +++ b/tests/router/router.test.ts @@ -52,6 +52,15 @@ describe("router miscellaneous", () => { expect(window.location.hash).toBe("#/users/5"); expect(env.qweb.forceUpdate).toHaveBeenCalledTimes(2); }); + + test("navigate in hash mode preserve location", async () => { + router = new TestRouter(env, [{ name: "users", path: "/users/{{id}}" }], {mode: "hash"}); + window.history.pushState({}, "title", window.location.origin + '/test.html'); + expect(window.location.href).toBe("http://localhost/test.html"); + await router.navigate({ to: "users", params: { id: 3 } }); + expect(window.location.href).toBe("http://localhost/test.html#/users/3"); + }); + }); describe("routeToPath", () => {