sv-router 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/src/actions.svelte.js +6 -2
- package/src/create-router.svelte.js +3 -12
- package/src/helpers/is-active.js +9 -4
- package/src/helpers/preload.js +14 -4
- package/src/helpers/utils.js +9 -0
- package/src/index.d.ts +12 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sv-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Modern Svelte Routing",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"svelte",
|
|
@@ -38,6 +38,9 @@
|
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@changesets/cli": "^2.29.4",
|
|
40
40
|
"@eslint/js": "^9.28.0",
|
|
41
|
+
"@sveltejs/vite-plugin-svelte": "^5.1.0",
|
|
42
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
43
|
+
"@testing-library/svelte": "^5.2.8",
|
|
41
44
|
"@types/node": "^22.15.30",
|
|
42
45
|
"eslint": "^9.28.0",
|
|
43
46
|
"eslint-config-prettier": "^10.1.5",
|
|
@@ -45,6 +48,7 @@
|
|
|
45
48
|
"eslint-plugin-svelte": "^3.9.1",
|
|
46
49
|
"eslint-plugin-unicorn": "^59.0.1",
|
|
47
50
|
"globals": "^16.2.0",
|
|
51
|
+
"jsdom": "^26.1.0",
|
|
48
52
|
"prettier": "^3.5.3",
|
|
49
53
|
"prettier-plugin-jsdoc": "^1.3.2",
|
|
50
54
|
"prettier-plugin-svelte": "^3.4.0",
|
|
@@ -52,7 +56,7 @@
|
|
|
52
56
|
"type-testing": "^0.2.0",
|
|
53
57
|
"typescript": "^5.8.3",
|
|
54
58
|
"typescript-eslint": "^8.33.1",
|
|
55
|
-
"vite": "^
|
|
59
|
+
"vite": "^7.0.0",
|
|
56
60
|
"vitest": "^3.2.2"
|
|
57
61
|
},
|
|
58
62
|
"peerDependencies": {
|
package/src/actions.svelte.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { location } from './create-router.svelte.js';
|
|
1
|
+
import { base, location } from './create-router.svelte.js';
|
|
2
|
+
import { join } from './helpers/utils.js';
|
|
2
3
|
|
|
3
4
|
/** @type {import('./index.d.ts').IsActiveLink} */
|
|
4
5
|
export function isActiveLink(node, { className = 'is-active', startsWith = false } = {}) {
|
|
@@ -7,7 +8,10 @@ export function isActiveLink(node, { className = 'is-active', startsWith = false
|
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
$effect(() => {
|
|
10
|
-
|
|
11
|
+
let pathname = new URL(node.href).pathname;
|
|
12
|
+
if (base.name) {
|
|
13
|
+
pathname = join(base.name, pathname);
|
|
14
|
+
}
|
|
11
15
|
if (startsWith ? location.pathname.startsWith(pathname) : location.pathname === pathname) {
|
|
12
16
|
node.classList.add(className);
|
|
13
17
|
} else {
|
|
@@ -2,7 +2,7 @@ import { BROWSER, DEV } from 'esm-env';
|
|
|
2
2
|
import { isActive } from './helpers/is-active.js';
|
|
3
3
|
import { matchRoute } from './helpers/match-route.js';
|
|
4
4
|
import { preload, preloadOnHover } from './helpers/preload.js';
|
|
5
|
-
import { constructPath, join, resolveRouteComponents } from './helpers/utils.js';
|
|
5
|
+
import { constructPath, join, resolveRouteComponents, updatedLocation } from './helpers/utils.js';
|
|
6
6
|
import { syncSearchParams } from './search-params.svelte.js';
|
|
7
7
|
|
|
8
8
|
/** @type {import('./index.d.ts').Routes} */
|
|
@@ -120,7 +120,7 @@ export async function onNavigate(path, options = {}) {
|
|
|
120
120
|
for (const { beforeLoad } of hooks) {
|
|
121
121
|
try {
|
|
122
122
|
pendingNavigationIndex = currentNavigationIndex;
|
|
123
|
-
await beforeLoad?.();
|
|
123
|
+
await beforeLoad?.({ pathname: matchPath, ...options });
|
|
124
124
|
} catch {
|
|
125
125
|
return;
|
|
126
126
|
}
|
|
@@ -161,7 +161,7 @@ export async function onNavigate(path, options = {}) {
|
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
for (const { afterLoad } of hooks) {
|
|
164
|
-
afterLoad?.();
|
|
164
|
+
afterLoad?.({ pathname: matchPath, ...options });
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
|
|
@@ -187,12 +187,3 @@ export function onGlobalClick(event) {
|
|
|
187
187
|
viewTransition: viewTransition === '' || viewTransition === 'true',
|
|
188
188
|
});
|
|
189
189
|
}
|
|
190
|
-
|
|
191
|
-
function updatedLocation() {
|
|
192
|
-
return {
|
|
193
|
-
pathname: globalThis.location.pathname,
|
|
194
|
-
search: globalThis.location.search,
|
|
195
|
-
state: history.state,
|
|
196
|
-
hash: globalThis.location.hash,
|
|
197
|
-
};
|
|
198
|
-
}
|
package/src/helpers/is-active.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { location } from '../create-router.svelte.js';
|
|
2
|
-
import { constructPath } from './utils.js';
|
|
1
|
+
import { base, location } from '../create-router.svelte.js';
|
|
2
|
+
import { constructPath, join } from './utils.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @param {string} pathname
|
|
@@ -7,7 +7,8 @@ import { constructPath } from './utils.js';
|
|
|
7
7
|
* @returns {boolean}
|
|
8
8
|
*/
|
|
9
9
|
export function isActive(pathname, params) {
|
|
10
|
-
|
|
10
|
+
const p = base.name ? join(base.name, pathname) : pathname;
|
|
11
|
+
return compare((a, b) => a === b, p, params);
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -16,7 +17,8 @@ export function isActive(pathname, params) {
|
|
|
16
17
|
* @returns {boolean}
|
|
17
18
|
*/
|
|
18
19
|
isActive.startsWith = (pathname, params) => {
|
|
19
|
-
|
|
20
|
+
const p = base.name ? join(base.name, pathname) : pathname;
|
|
21
|
+
return compare((a, b) => a.startsWith(b), p, params);
|
|
20
22
|
};
|
|
21
23
|
|
|
22
24
|
/**
|
|
@@ -36,6 +38,9 @@ function compare(compareFn, pathname, params) {
|
|
|
36
38
|
|
|
37
39
|
const pathParts = pathname.split('/').slice(1);
|
|
38
40
|
const routeParts = location.pathname.split('/').slice(1);
|
|
41
|
+
if (pathParts.length > routeParts.length) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
39
44
|
for (const [index, pathPart] of pathParts.entries()) {
|
|
40
45
|
const routePart = routeParts[index];
|
|
41
46
|
if (pathPart.startsWith(':')) {
|
package/src/helpers/preload.js
CHANGED
|
@@ -4,11 +4,12 @@ import { resolveRouteComponents } from './utils.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* @param {import('../index.js').Routes} routes
|
|
6
6
|
* @param {string} path
|
|
7
|
+
* @param {import('../index.d.ts').NavigateOptions} [options]
|
|
7
8
|
*/
|
|
8
|
-
export async function preload(routes, path) {
|
|
9
|
+
export async function preload(routes, path, options) {
|
|
9
10
|
const { match, layouts, hooks } = matchRoute(path, routes);
|
|
10
11
|
for (const { onPreload } of hooks) {
|
|
11
|
-
onPreload?.();
|
|
12
|
+
onPreload?.({ pathname: path, ...options });
|
|
12
13
|
}
|
|
13
14
|
await resolveRouteComponents(match ? [...layouts, match] : layouts);
|
|
14
15
|
}
|
|
@@ -18,7 +19,9 @@ const linkSet = new Set();
|
|
|
18
19
|
/** @param {import('../index.js').Routes} routes */
|
|
19
20
|
export function preloadOnHover(routes) {
|
|
20
21
|
const observer = new MutationObserver(() => {
|
|
21
|
-
const links =
|
|
22
|
+
const links = /** @type {NodeListOf<HTMLAnchorElement>} */ (
|
|
23
|
+
document.querySelectorAll('a[data-preload]')
|
|
24
|
+
);
|
|
22
25
|
for (const link of links) {
|
|
23
26
|
if (linkSet.has(link)) continue;
|
|
24
27
|
linkSet.add(link);
|
|
@@ -27,7 +30,14 @@ export function preloadOnHover(routes) {
|
|
|
27
30
|
link.removeEventListener('mouseenter', callback);
|
|
28
31
|
const href = link.getAttribute('href');
|
|
29
32
|
if (!href) return;
|
|
30
|
-
|
|
33
|
+
const url = new URL(link.href);
|
|
34
|
+
const { replace, state } = link.dataset;
|
|
35
|
+
preload(routes, href, {
|
|
36
|
+
replace: replace === '' || replace === 'true',
|
|
37
|
+
search: url.search,
|
|
38
|
+
state,
|
|
39
|
+
hash: url.hash,
|
|
40
|
+
});
|
|
31
41
|
});
|
|
32
42
|
}
|
|
33
43
|
});
|
package/src/helpers/utils.js
CHANGED
|
@@ -59,3 +59,12 @@ export function join(...parts) {
|
|
|
59
59
|
}
|
|
60
60
|
return result;
|
|
61
61
|
}
|
|
62
|
+
|
|
63
|
+
export function updatedLocation() {
|
|
64
|
+
return {
|
|
65
|
+
pathname: globalThis.location.pathname,
|
|
66
|
+
search: globalThis.location.search,
|
|
67
|
+
state: history.state,
|
|
68
|
+
hash: globalThis.location.hash,
|
|
69
|
+
};
|
|
70
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -56,17 +56,17 @@ export type Hooks = {
|
|
|
56
56
|
* You can throw a `navigate` call to redirect to another route.
|
|
57
57
|
*
|
|
58
58
|
* ```js
|
|
59
|
-
* async beforeLoad() {
|
|
59
|
+
* async beforeLoad({ pathname }) {
|
|
60
60
|
* await ...
|
|
61
61
|
* throw navigate('/home');
|
|
62
62
|
* }
|
|
63
63
|
* ```
|
|
64
64
|
*/
|
|
65
|
-
beforeLoad?(): void | Promise<void>;
|
|
65
|
+
beforeLoad?(context: HooksContext): void | Promise<void>;
|
|
66
66
|
/** A function that will be called after the route is loaded. */
|
|
67
|
-
afterLoad?(): void;
|
|
67
|
+
afterLoad?(context: HooksContext): void;
|
|
68
68
|
/** A function that will be called when the route is preloaded. */
|
|
69
|
-
onPreload?(): void;
|
|
69
|
+
onPreload?(context: HooksContext): void;
|
|
70
70
|
};
|
|
71
71
|
|
|
72
72
|
export type Routes = {
|
|
@@ -205,6 +205,14 @@ export type AllParams<T extends Routes> = Partial<
|
|
|
205
205
|
Record<ExtractParams<RemoveParenthesis<RecursiveKeys<T>>>, string>
|
|
206
206
|
>;
|
|
207
207
|
|
|
208
|
+
export type HooksContext = {
|
|
209
|
+
pathname: string;
|
|
210
|
+
replace?: boolean;
|
|
211
|
+
search?: string;
|
|
212
|
+
state?: string;
|
|
213
|
+
hash?: string;
|
|
214
|
+
};
|
|
215
|
+
|
|
208
216
|
export type NavigateOptions =
|
|
209
217
|
| {
|
|
210
218
|
replace?: boolean;
|