Skip to content

Commit 5b4da14

Browse files
committed
[FIX] language switch loses route on reload
fixes 3a64554 Since commit merge of #368, `deepUrl({lang})` calls `navigate("/en/map")` instead of `navigate("#/en/map")`. Without the leading "#", Navigo leaves `match.hashString` empty. `Router.customRoute` was relying on `match.hashString` to reconstruct the URL before reloading the page on a language change, so it ended up doing `location.hash = "/"` and the reload picked up no language from the URL — the locale silently fell back to the browser default. Rebuild the reload URL from `currentState` via `generateLink()` instead, which is already populated by the time the lang-change branch runs and is independent of how navigate() was originally invoked.
1 parent 98749eb commit 5b4da14

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

lib/utils/router.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import { Router } from "./router.js";
33

44
describe("Router.deepUrl", () => {
@@ -27,3 +27,56 @@ describe("Router.deepUrl", () => {
2727
expect(navigate).toHaveBeenCalledWith("/de/graph");
2828
});
2929
});
30+
31+
describe("Router.customRoute language change", () => {
32+
const hashSetter = vi.fn<(v: string) => void>();
33+
const reload = vi.fn<() => void>();
34+
35+
beforeEach(() => {
36+
hashSetter.mockReset();
37+
reload.mockReset();
38+
vi.stubGlobal("location", {
39+
get hash() {
40+
return "";
41+
},
42+
set hash(v: string) {
43+
hashSetter(v);
44+
},
45+
reload,
46+
});
47+
});
48+
49+
afterEach(() => {
50+
vi.unstubAllGlobals();
51+
});
52+
53+
it("rebuilds the hash from currentState when navigate omits the # prefix", () => {
54+
const fakeRouter = {
55+
state: { lang: "de", view: "map" },
56+
currentState: {},
57+
language: { getLocale: (l: string) => l },
58+
targets: [],
59+
init: true,
60+
objects: { nodes: { all: [], lost: [], new: [], offline: [], online: [] }, links: [], nodeDict: {} },
61+
view: vi.fn(),
62+
gotoNode: vi.fn(),
63+
gotoLink: vi.fn(),
64+
resetView: vi.fn(),
65+
getParams: () => ({}),
66+
paramsToUrl: Router.prototype.paramsToUrl,
67+
generateLink: Router.prototype.generateLink,
68+
};
69+
70+
// Mimic what Navigo passes to the handler after deepUrl({lang: "en"})
71+
// navigates to "/en/map" — note hashString is "" because there is no "#".
72+
const match = {
73+
data: ["en", "map", undefined, undefined, undefined, undefined, undefined],
74+
hashString: "",
75+
};
76+
77+
Router.prototype.customRoute.call(fakeRouter as never, match as never);
78+
79+
expect(hashSetter).toHaveBeenCalledWith("/en/map");
80+
expect(reload).toHaveBeenCalled();
81+
});
82+
});

lib/utils/router.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ export class Router extends Navigo {
107107

108108
if (lang && lang !== this.state.lang && lang === this.language.getLocale(lang)) {
109109
console.debug("Language change reload");
110-
const prefix = match.hashString.startsWith("/") ? "" : "/";
111-
location.hash = prefix + match.hashString;
110+
location.hash = this.generateLink().substring(1);
112111
location.reload();
113112
}
114113

0 commit comments

Comments
 (0)