Skip to content

Commit ace70ee

Browse files
fix: pass parameters into onNavigate in Router.svelte
1 parent 9d2a2fd commit ace70ee

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

.changeset/hot-pugs-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv-router': patch
3+
---
4+
5+
pass initial page pathname, search, hash to onNavigate, correctly handle cancellation of navigations, return Error in navigate

src/Router.svelte

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77
/** @type {{ base?: string }} */
88
let { base: basename } = $props();
99
10+
const url = new URL(globalThis.location.href);
1011
if (basename) {
1112
base.name = (basename.startsWith('/') ? '' : '/') + basename;
12-
const url = new URL(globalThis.location.href);
1313
if (!url.pathname.startsWith(base.name)) {
1414
url.pathname = join(base.name, url.pathname);
1515
history.replaceState(history.state || {}, '', url.href);
1616
}
1717
}
1818
19-
onNavigate();
19+
onNavigate(url.pathname, {
20+
search: url.search,
21+
hash: url.hash,
22+
});
2023
2124
$effect(() => {
22-
const off1 = on(globalThis, 'popstate', () => onNavigate());
25+
const off1 = on(globalThis, 'popstate', () =>
26+
onNavigate(url.pathname, {
27+
search: url.search,
28+
hash: url.hash,
29+
}),
30+
);
2331
const off2 = on(globalThis, 'click', onGlobalClick);
2432
2533
return () => {

src/create-router.svelte.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ export function createRouter(r) {
8181
/**
8282
* @param {string | number} path
8383
* @param {import('./index.d.ts').NavigateOptions & { params?: Record<string, string> }} options
84+
* @returns Promise<Error>
8485
*/
8586
function navigate(path, options = {}) {
8687
if (typeof path === 'number') {
8788
globalThis.history.go(path);
88-
return;
89+
return new Error(`Navigating to history entry: ${path}`);
8990
}
9091
if (options.params) {
9192
path = constructPath(path, options.params);
@@ -97,10 +98,11 @@ function navigate(path, options = {}) {
9798
options.hash = '#' + options.hash;
9899
}
99100
onNavigate(path, options);
101+
return new Error(`Navigating to: ${path}${options?.search ?? ''}${options?.hash ?? ''}`);
100102
}
101103

102104
/**
103-
* @param {string} [path]
105+
* @param {string} path
104106
* @param {import('./index.d.ts').NavigateOptions} options
105107
*/
106108
export async function onNavigate(path, options = {}) {
@@ -111,7 +113,7 @@ export async function onNavigate(path, options = {}) {
111113
navigationIndex++;
112114
const currentNavigationIndex = navigationIndex;
113115

114-
let matchPath = path || globalThis.location.pathname;
116+
let matchPath = path;
115117
if (base.name && matchPath.startsWith(base.name)) {
116118
matchPath = matchPath.slice(base.name.length) || '/';
117119
}
@@ -178,7 +180,7 @@ export function onGlobalClick(event) {
178180

179181
event.preventDefault();
180182
const { replace, state, scrollToTop, viewTransition } = anchor.dataset;
181-
onNavigate(url.pathname, {
183+
void onNavigate(url.pathname, {
182184
replace: replace === '' || replace === 'true',
183185
search: url.search,
184186
state,

src/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ export type RouterApi<T extends Routes> = {
129129
*
130130
* @param route The route to navigate to.
131131
* @param options The navigation options.
132+
*
133+
* Returns an Error for use with `throw navigate(...)` inside hooks.
132134
*/
133-
navigate<U extends Path<T>>(...args: NavigateArgs<U>): void;
135+
navigate<U extends Path<T>>(...args: NavigateArgs<U>): Error;
134136

135137
/**
136138
* Will return `true` if the given path is active.

0 commit comments

Comments
 (0)