From 3208959e0911ba702e67f109deb19c20effbb3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Luba=C5=84ski?= Date: Tue, 14 May 2024 11:07:43 +0200 Subject: [PATCH] fix(router): prevent from throwing when browser pushes null state --- src/router.js | 2 +- test/spec/router.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/router.js b/src/router.js index 066e1092..6752573e 100644 --- a/src/router.js +++ b/src/router.js @@ -381,7 +381,7 @@ function setupView(hybrids, routerOptions, parent, nestedParent) { return params; }, (host, params, lastParams) => { - if (!lastParams) return; + if (!lastParams || !globalThis.history.state) return; const state = globalThis.history.state; const index = state.findIndex((entry) => { diff --git a/test/spec/router.js b/test/spec/router.js index 6b2359cd..170f726d 100644 --- a/test/spec/router.js +++ b/test/spec/router.js @@ -634,6 +634,19 @@ describe("router:", () => { }); }); + it("does not throw when browser pushes null state", () => { + const state = window.history.state; + window.history.replaceState(null, "", ""); + const rootView = host.views[0]; + rootView.globalB = "value"; + + return resolveTimeout(() => { + expect(hybrids(host.views[0])).toBe(RootView); + expect(hybrids(host.children[0])).toBe(RootView); + window.history.replaceState(state, "", ""); + }); + }); + it("displays root view", () => resolveTimeout(() => { expect(hybrids(host.views[0])).toBe(RootView);