Skip to content

Commit db46f52

Browse files
fix: pass parameters into onNavigate in Router.svelte
1 parent 87f5e67 commit db46f52

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
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/create-router.svelte.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ export function createRouter(r) {
114114
* params?: Record<string, string>;
115115
* search?: import('./index.d.ts').Search;
116116
* }} options
117+
* @returns Promise<Error>
117118
*/
118119
function navigate(path, options = {}) {
119120
if (typeof path === 'number') {
120121
globalThis.history.go(path);
121-
return;
122+
return new Error(`Navigating to history entry: ${path}`);
122123
}
123124

124125
path = constructPath(path, options.params);
@@ -128,10 +129,11 @@ function navigate(path, options = {}) {
128129
options.hash = '#' + options.hash;
129130
}
130131
onNavigate(path, options);
132+
return new Error(`Navigating to: ${path}${options?.search ?? ''}${options?.hash ?? ''}`);
131133
}
132134

133135
/**
134-
* @param {string} [path]
136+
* @param {string} path
135137
* @param {import('./index.d.ts').NavigateOptions} options
136138
*/
137139
export async function onNavigate(path, options = {}) {
@@ -253,7 +255,7 @@ export function onGlobalClick(event) {
253255

254256
event.preventDefault();
255257
const { replace, state, scrollToTop, viewTransition } = anchor.dataset;
256-
onNavigate(path, {
258+
void onNavigate(path, {
257259
replace: replace === '' || replace === 'true',
258260
search: url.search,
259261
state,

src/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ export type RouterApi<T extends Routes> = {
139139
*
140140
* @param route The route to navigate to.
141141
* @param options The navigation options.
142+
*
143+
* Returns an Error for use with `throw navigate(...)` inside hooks.
142144
*/
143-
navigate<U extends Path<T>>(...args: NavigateArgs<U>): void;
145+
navigate<U extends Path<T>>(...args: NavigateArgs<U>): Error;
144146

145147
/**
146148
* Will return `true` if the given path is active.

0 commit comments

Comments
 (0)