Skip to content

Commit

Permalink
fix(app): fix navigate with relative url
Browse files Browse the repository at this point in the history
  • Loading branch information
matschik committed Mar 5, 2024
1 parent e43f1aa commit 84fc766
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -54,7 +56,7 @@
onMount(() => {
document.addEventListener("click", handleClick);
navigate(window.location.href);
navigate(window.location.href, { isInitialNavigation: true });
});
onDestroy(() => {
Expand Down

0 comments on commit 84fc766

Please sign in to comment.