From 84fc766888f9efd1ebf86fe57fe8f554d1ecc47b Mon Sep 17 00:00:00 2001 From: Mathieu Schimmerling Date: Tue, 5 Mar 2024 13:49:49 +0100 Subject: [PATCH] fix(app): fix navigate with relative url --- src/Router.svelte | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Router.svelte b/src/Router.svelte index 07f37bec..2a4543c0 100644 --- a/src/Router.svelte +++ b/src/Router.svelte @@ -16,17 +16,19 @@ const currentRoute = writable({ component: null }); - function navigate(path) { - const urlParsed = new URL(path); + function navigate(path, state) { + state = state || {}; + const urlParsed = new URL(path, window.location.origin); const routePayload = router.lookup(urlParsed.pathname); + if (routePayload) { if (routePayload.component.toString().startsWith("class")) { currentRoute.set(routePayload); - window.history.pushState({}, "", path); + window.history.pushState(state, "", path); } else if (typeof routePayload.component === "function") { routePayload.component().then((module) => { currentRoute.set({ ...routePayload, component: module.default }); - window.history.pushState({}, "", path); + window.history.pushState(state, "", path); }); } else { console.error("Invalid route component"); @@ -54,7 +56,7 @@ onMount(() => { document.addEventListener("click", handleClick); - navigate(window.location.href); + navigate(window.location.href, { isInitialNavigation: true }); }); onDestroy(() => {