Skip to content

Commit

Permalink
update router to use BASE_URL
Browse files Browse the repository at this point in the history
  • Loading branch information
mishankov committed Mar 1, 2024
1 parent 8aa2f8d commit 2690200
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/components/Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
export const routes = writable<Route[]>();
export const currentRoute = writable<Route>();
function pathWithBase(path: string): string {
if (import.meta.env.BASE_URL === "/") return path
return import.meta.env.BASE_URL + path
}
function currentLocation(pathname: string) {
if (pathname === "/") return pathname;
if (pathname === pathWithBase("/")) return pathname;
if (pathname.endsWith("/")) return pathname.slice(0, pathname.length - 1);
return pathname;
};
function getRoute(href: string, routes: Route[]): Route {
const url = new URL(href);
const route = routes.find(route => route.location === currentLocation(url.pathname)) || routes[0]
const route = routes.find(route => pathWithBase(route.location) === currentLocation(url.pathname)) || routes[0]
return route
}
Expand Down

0 comments on commit 2690200

Please sign in to comment.