navgo 3.0.4 → 3.0.5
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/index.d.ts +20 -20
- package/index.js +17 -16
- package/package.json +2 -1
- package/readme.md +18 -19
package/index.d.ts
CHANGED
|
@@ -7,23 +7,23 @@ export type Params = Record<string, string | null | undefined>
|
|
|
7
7
|
|
|
8
8
|
/** Built-in validator helpers shape. */
|
|
9
9
|
export interface ValidatorHelpers {
|
|
10
|
-
int(
|
|
10
|
+
int(_opts?: {
|
|
11
11
|
min?: number | null
|
|
12
12
|
max?: number | null
|
|
13
|
-
}): (
|
|
14
|
-
|
|
13
|
+
}): (_value: string | null | undefined) => boolean
|
|
14
|
+
one_of(_values: Iterable<string>): (_value: string | null | undefined) => boolean
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/** Optional per-route hooks recognized by Navgo. */
|
|
18
18
|
export interface Hooks {
|
|
19
19
|
/** Validate params with custom per-param validators. Return `false` to skip a match. */
|
|
20
|
-
param_validators?: Record<string, (
|
|
20
|
+
param_validators?: Record<string, (_value: string | null | undefined) => boolean>
|
|
21
21
|
/** Load data for a route before navigation. May return a Promise or an array of values/promises. */
|
|
22
|
-
loaders?(
|
|
22
|
+
loaders?(_params: Params): unknown | Promise<unknown> | Array<unknown | Promise<unknown>>
|
|
23
23
|
/** Predicate used during match(); may be async. If it returns `false`, the route is skipped. */
|
|
24
|
-
validate?(
|
|
24
|
+
validate?(_params: Params): boolean | Promise<boolean>
|
|
25
25
|
/** Route-level navigation guard, called on the current route when leaving it. Synchronous only; call `nav.cancel()` to prevent navigation. */
|
|
26
|
-
|
|
26
|
+
before_route_leave?(_nav: Navigation): void
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export interface NavigationTarget {
|
|
@@ -39,7 +39,7 @@ export interface Navigation {
|
|
|
39
39
|
type: 'link' | 'goto' | 'popstate' | 'leave'
|
|
40
40
|
from: NavigationTarget | null
|
|
41
41
|
to: NavigationTarget | null
|
|
42
|
-
|
|
42
|
+
will_unload: boolean
|
|
43
43
|
cancelled: boolean
|
|
44
44
|
/** The original browser event that initiated navigation, when available. */
|
|
45
45
|
event?: Event
|
|
@@ -62,7 +62,7 @@ export type Router<T = unknown> = Navgo<T>
|
|
|
62
62
|
export interface NavgoHistoryMeta {
|
|
63
63
|
/** Monotonic index of the current history entry for scroll restoration. */
|
|
64
64
|
idx: number
|
|
65
|
-
/** Present when the entry was created via shallow `
|
|
65
|
+
/** Present when the entry was created via shallow `push_state`/`replace_state`. */
|
|
66
66
|
shallow?: boolean
|
|
67
67
|
/** Origin of the navigation that created this entry. */
|
|
68
68
|
type?: 'link' | 'goto' | 'popstate'
|
|
@@ -75,32 +75,32 @@ export interface Options {
|
|
|
75
75
|
preload_delay?: number
|
|
76
76
|
/** Disable hover/touch preloading when `false`. Default true. */
|
|
77
77
|
preload_on_hover?: boolean
|
|
78
|
-
/** Global hook fired after per-route `
|
|
79
|
-
before_navigate?(
|
|
78
|
+
/** Global hook fired after per-route `before_route_leave`, before loaders/history change. Can cancel. */
|
|
79
|
+
before_navigate?(_nav: Navigation): void
|
|
80
80
|
/** Global hook fired after routing completes (data loaded, URL updated, handlers run). */
|
|
81
|
-
after_navigate?(
|
|
81
|
+
after_navigate?(_nav: Navigation): void
|
|
82
82
|
/** Global hook fired whenever the URL changes.
|
|
83
83
|
* Triggers for shallow pushes/replaces, hash changes, popstate-shallow, 404s, and full navigations.
|
|
84
84
|
* Receives the router's current snapshot (eg `{ url: URL, route: RouteTuple|null, params: Params }`).
|
|
85
85
|
*/
|
|
86
|
-
url_changed?(
|
|
86
|
+
url_changed?(_payload: any): void
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/** Navgo default export: class-based router. */
|
|
90
90
|
export default class Navgo<T = unknown> {
|
|
91
|
-
constructor(
|
|
91
|
+
constructor(_routes?: Array<RouteTuple<T>>, _opts?: Options)
|
|
92
92
|
/** Format `url` relative to the configured base. */
|
|
93
|
-
format(
|
|
93
|
+
format(_url: string): string | false
|
|
94
94
|
/** SvelteKit-like navigation that runs loaders before updating the URL. */
|
|
95
|
-
goto(
|
|
95
|
+
goto(_url: string, _opts?: { replace?: boolean }): Promise<void>
|
|
96
96
|
/** Shallow push — updates URL/state without triggering handlers. */
|
|
97
|
-
|
|
97
|
+
push_state(_url?: string | URL, _state?: any): void
|
|
98
98
|
/** Shallow replace — updates URL/state without triggering handlers. */
|
|
99
|
-
|
|
99
|
+
replace_state(_url?: string | URL, _state?: any): void
|
|
100
100
|
/** Manually preload loaders for a URL (deduped). */
|
|
101
|
-
preload(
|
|
101
|
+
preload(_url: string): Promise<unknown | void>
|
|
102
102
|
/** Try to match `url`; returns route tuple and params or `null`. Supports async `validate`. */
|
|
103
|
-
match(
|
|
103
|
+
match(_url: string): Promise<MatchResult<T> | null>
|
|
104
104
|
/** Attach history + click listeners and immediately process current location. */
|
|
105
105
|
init(): Promise<void>
|
|
106
106
|
/** Remove listeners installed by `init()`. */
|
package/index.js
CHANGED
|
@@ -135,12 +135,12 @@ export default class Navgo {
|
|
|
135
135
|
const nav = this.#make_nav({
|
|
136
136
|
type: 'leave',
|
|
137
137
|
to: null,
|
|
138
|
-
|
|
138
|
+
will_unload: true,
|
|
139
139
|
event: ev,
|
|
140
140
|
})
|
|
141
|
-
this.#current.route?.[1]?.
|
|
141
|
+
this.#current.route?.[1]?.before_route_leave?.(nav)
|
|
142
142
|
if (nav.cancelled) {
|
|
143
|
-
ℹ('[🧭 navigate]', 'cancelled by
|
|
143
|
+
ℹ('[🧭 navigate]', 'cancelled by before_route_leave during unload')
|
|
144
144
|
ev.preventDefault()
|
|
145
145
|
ev.returnValue = ''
|
|
146
146
|
}
|
|
@@ -163,7 +163,7 @@ export default class Navgo {
|
|
|
163
163
|
#resolve_url_and_path(url_raw) {
|
|
164
164
|
if (url_raw[0] == '/' && !this.#base_rgx.test(url_raw)) url_raw = this.#base + url_raw
|
|
165
165
|
const url = new URL(url_raw, location.href)
|
|
166
|
-
const path = this.format(url.pathname)?.
|
|
166
|
+
const path = this.format(url.pathname).match?.(/[^?#]*/)?.[0]
|
|
167
167
|
ℹ('[🧭 resolve]', { url_in: url_raw, url: url.href, path })
|
|
168
168
|
return path ? { url, path } : null
|
|
169
169
|
}
|
|
@@ -178,7 +178,8 @@ export default class Navgo {
|
|
|
178
178
|
!a.target &&
|
|
179
179
|
!a.download &&
|
|
180
180
|
a.host === location.host &&
|
|
181
|
-
|
|
181
|
+
href[0] != '#' &&
|
|
182
|
+
this.#base_rgx.test(a.pathname)
|
|
182
183
|
? { a, href }
|
|
183
184
|
: null
|
|
184
185
|
}
|
|
@@ -200,7 +201,7 @@ export default class Navgo {
|
|
|
200
201
|
/**
|
|
201
202
|
* @returns {Navigation}
|
|
202
203
|
*/
|
|
203
|
-
#make_nav({ type, from = undefined, to = undefined,
|
|
204
|
+
#make_nav({ type, from = undefined, to = undefined, will_unload = false, event = undefined }) {
|
|
204
205
|
const from_obj =
|
|
205
206
|
from !== undefined
|
|
206
207
|
? from
|
|
@@ -215,7 +216,7 @@ export default class Navgo {
|
|
|
215
216
|
type, // 'link' | 'goto' | 'popstate' | 'leave'
|
|
216
217
|
from: from_obj,
|
|
217
218
|
to,
|
|
218
|
-
|
|
219
|
+
will_unload,
|
|
219
220
|
cancelled: false,
|
|
220
221
|
event,
|
|
221
222
|
cancel() {
|
|
@@ -253,9 +254,9 @@ export default class Navgo {
|
|
|
253
254
|
})
|
|
254
255
|
|
|
255
256
|
//
|
|
256
|
-
//
|
|
257
|
+
// before_route_leave
|
|
257
258
|
//
|
|
258
|
-
this.#current.route?.[1]?.
|
|
259
|
+
this.#current.route?.[1]?.before_route_leave?.(nav)
|
|
259
260
|
if (nav.cancelled) {
|
|
260
261
|
// use history.go to cancel the nav, and jump back to where we are
|
|
261
262
|
if (is_popstate) {
|
|
@@ -270,7 +271,7 @@ export default class Navgo {
|
|
|
270
271
|
}
|
|
271
272
|
}
|
|
272
273
|
}
|
|
273
|
-
ℹ('[🧭 goto]', 'cancelled by
|
|
274
|
+
ℹ('[🧭 goto]', 'cancelled by before_route_leave')
|
|
274
275
|
return
|
|
275
276
|
}
|
|
276
277
|
|
|
@@ -370,13 +371,13 @@ export default class Navgo {
|
|
|
370
371
|
// save scroll for current index before shallow change
|
|
371
372
|
this.#save_scroll()
|
|
372
373
|
const idx = this.#route_idx + (replace ? 0 : 1)
|
|
373
|
-
const st = { ...state,
|
|
374
|
+
const st = { ...state, __navgo: { ...state?.__navgo, shallow: true, idx } }
|
|
374
375
|
history[(replace ? 'replace' : 'push') + 'State'](st, '', u.href)
|
|
375
|
-
ℹ('[🧭 history]', replace ? '
|
|
376
|
+
ℹ('[🧭 history]', replace ? 'replace_state(shallow)' : 'push_state(shallow)', {
|
|
376
377
|
idx,
|
|
377
378
|
href: u.href,
|
|
378
379
|
})
|
|
379
|
-
// Popstate handler checks state.
|
|
380
|
+
// Popstate handler checks state.__navgo.shallow and skips router processing
|
|
380
381
|
this.#route_idx = idx
|
|
381
382
|
// carry forward current scroll position for the shallow entry so Forward restores correctly
|
|
382
383
|
this.#scroll.set(idx, { x: scrollX, y: scrollY })
|
|
@@ -387,11 +388,11 @@ export default class Navgo {
|
|
|
387
388
|
}
|
|
388
389
|
|
|
389
390
|
/** @param {string|URL} [url] @param {any} [state] */
|
|
390
|
-
|
|
391
|
+
push_state(url, state) {
|
|
391
392
|
this.#commit_shallow(url, state, false)
|
|
392
393
|
}
|
|
393
394
|
/** @param {string|URL} [url] @param {any} [state] */
|
|
394
|
-
|
|
395
|
+
replace_state(url, state) {
|
|
395
396
|
this.#commit_shallow(url, state, true)
|
|
396
397
|
}
|
|
397
398
|
|
|
@@ -627,7 +628,7 @@ export default class Navgo {
|
|
|
627
628
|
return true
|
|
628
629
|
}
|
|
629
630
|
},
|
|
630
|
-
|
|
631
|
+
one_of(values) {
|
|
631
632
|
const set = new Set(values)
|
|
632
633
|
return v => set.has(v)
|
|
633
634
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "navgo",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.5",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/mustafa0x/navgo.git"
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"build": "perl -0777 -i -pe 's/console.debug\\(...args\\)/{}/g' index.js",
|
|
19
19
|
"prepublishOnly": "pnpm run build",
|
|
20
20
|
"test": "vitest run index.test.js",
|
|
21
|
+
"test:e2e": "playwright test",
|
|
21
22
|
"types": "tsc -p test/types"
|
|
22
23
|
},
|
|
23
24
|
"files": [
|
package/readme.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
## Install
|
|
2
2
|
|
|
3
3
|
```
|
|
4
|
-
$ pnpm install --dev
|
|
4
|
+
$ pnpm install --dev navgo
|
|
5
5
|
```
|
|
6
6
|
|
|
7
7
|
## Usage
|
|
@@ -26,7 +26,7 @@ const routes = [
|
|
|
26
26
|
// load data before URL changes; result goes to after_navigate(...)
|
|
27
27
|
loaders: params => fetch('/api/admin').then(r => r.json()),
|
|
28
28
|
// per-route guard; cancel synchronously to block nav
|
|
29
|
-
|
|
29
|
+
before_route_leave(nav) {
|
|
30
30
|
if ((nav.type === 'link' || nav.type === 'nav') && !confirm('Enter admin?')) {
|
|
31
31
|
nav.cancel()
|
|
32
32
|
}
|
|
@@ -98,7 +98,7 @@ Notes:
|
|
|
98
98
|
- `after_navigate`: `(nav: Navigation) => void`
|
|
99
99
|
- App-level hook called after routing completes (URL updated, data loaded). `nav.to.data` holds any loader data.
|
|
100
100
|
- `url_changed`: `(snapshot: any) => void`
|
|
101
|
-
- Fires on every URL change — shallow `
|
|
101
|
+
- Fires on every URL change — shallow `push_state`/`replace_state`, hash changes, `popstate` shallow entries, 404s, and full navigations.
|
|
102
102
|
- Receives the router's current snapshot: an object like `{ url: URL, route: RouteTuple|null, params: Params }`.
|
|
103
103
|
- The snapshot type is intentionally `any` and may evolve without a breaking change.
|
|
104
104
|
- `preload_delay`: `number` (default `20`)
|
|
@@ -116,7 +116,7 @@ Important: Navgo only processes routes that match your `base` path.
|
|
|
116
116
|
- Run before URL changes on `link`/`nav`. Results are cached per formatted path and forwarded to `after_navigate`.
|
|
117
117
|
- validate?(params): `boolean | Promise<boolean>`
|
|
118
118
|
- Predicate called during matching. If it returns or resolves to `false`, the route is skipped.
|
|
119
|
-
-
|
|
119
|
+
- before_route_leave?(nav): `(nav: Navigation) => void`
|
|
120
120
|
- Guard called once per navigation attempt on the current route (leave). Call `nav.cancel()` synchronously to prevent navigation. For `popstate`, cancellation auto-reverts the history jump.
|
|
121
121
|
|
|
122
122
|
The `Navigation` object contains:
|
|
@@ -126,7 +126,7 @@ The `Navigation` object contains:
|
|
|
126
126
|
type: 'link' | 'nav' | 'popstate' | 'leave',
|
|
127
127
|
from: { url, params, route } | null,
|
|
128
128
|
to: { url, params, route } | null,
|
|
129
|
-
|
|
129
|
+
will_unload: boolean,
|
|
130
130
|
cancelled: boolean,
|
|
131
131
|
event?: Event,
|
|
132
132
|
cancel(): void
|
|
@@ -152,7 +152,7 @@ const routes = [
|
|
|
152
152
|
/* ... */
|
|
153
153
|
},
|
|
154
154
|
loaders: params => fetch('/api/admin/stats').then(r => r.json()),
|
|
155
|
-
|
|
155
|
+
before_route_leave(nav) {
|
|
156
156
|
if (nav.type === 'link' || nav.type === 'nav') {
|
|
157
157
|
if (!confirm('Enter admin area?')) nav.cancel()
|
|
158
158
|
}
|
|
@@ -204,7 +204,7 @@ The desired path to navigate. If it begins with `/` and does not match the confi
|
|
|
204
204
|
Type: `Object`
|
|
205
205
|
|
|
206
206
|
- replace: `Boolean` (default `false`)
|
|
207
|
-
|
|
207
|
+
- When `true`, uses `history.replaceState`; otherwise `history.pushState`.
|
|
208
208
|
|
|
209
209
|
### init()
|
|
210
210
|
|
|
@@ -240,13 +240,13 @@ Returns: `Promise<unknown | void>`
|
|
|
240
240
|
Preload a route's `loaders` data for a given `uri` without navigating. Concurrent calls for the same path are deduped.
|
|
241
241
|
Note: Resolves to `undefined` when the matched route has no `loaders`.
|
|
242
242
|
|
|
243
|
-
###
|
|
243
|
+
### push_state(url?, state?)
|
|
244
244
|
|
|
245
245
|
Returns: `void`
|
|
246
246
|
|
|
247
247
|
Perform a shallow history push: updates the URL/state without triggering route processing.
|
|
248
248
|
|
|
249
|
-
###
|
|
249
|
+
### replace_state(url?, state?)
|
|
250
250
|
|
|
251
251
|
Returns: `void`
|
|
252
252
|
|
|
@@ -266,9 +266,8 @@ This section explains, in detail, how navigation is processed: matching, hooks,
|
|
|
266
266
|
- `goto` — programmatic navigation via `router.goto(...)`.
|
|
267
267
|
- `popstate` — browser back/forward.
|
|
268
268
|
- `leave` — page is unloading (refresh, external navigation, tab close) via `beforeunload`.
|
|
269
|
-
- `pushState` (shallow)?
|
|
270
269
|
|
|
271
|
-
The router passes the type to your route-level `
|
|
270
|
+
The router passes the type to your route-level `before_route_leave(nav)` hook.
|
|
272
271
|
|
|
273
272
|
### Matching and Params
|
|
274
273
|
|
|
@@ -286,7 +285,7 @@ For `link` and `goto` navigations that match a route:
|
|
|
286
285
|
|
|
287
286
|
```
|
|
288
287
|
[click <a>] or [router.goto()]
|
|
289
|
-
→
|
|
288
|
+
→ before_route_leave({ type }) // per-route guard
|
|
290
289
|
→ before_navigate(nav) // app-level start
|
|
291
290
|
→ cancelled? yes → stop
|
|
292
291
|
→ no → run loaders(params) // may be value, Promise, or Promise[]
|
|
@@ -300,10 +299,10 @@ For `link` and `goto` navigations that match a route:
|
|
|
300
299
|
|
|
301
300
|
### Shallow Routing
|
|
302
301
|
|
|
303
|
-
Use `
|
|
302
|
+
Use `push_state(url, state?)` or `replace_state(url, state?)` to update the URL/state without re-running routing logic.
|
|
304
303
|
|
|
305
304
|
```
|
|
306
|
-
|
|
305
|
+
push_state/replace_state (shallow)
|
|
307
306
|
→ updates history.state and URL
|
|
308
307
|
→ router does not process routing on shallow operations
|
|
309
308
|
```
|
|
@@ -323,7 +322,7 @@ Navgo manages scroll manually (sets `history.scrollRestoration = 'manual'`) and
|
|
|
323
322
|
- If the URL has a `#hash`, scroll to the matching element `id` or `[name="..."]`.
|
|
324
323
|
- Otherwise, scroll to the top `(0, 0)`.
|
|
325
324
|
- On `popstate`: restore the saved position for the target history index; if not found but there is a `#hash`, scroll to the anchor instead.
|
|
326
|
-
- Shallow `
|
|
325
|
+
- Shallow `push_state`/`replace_state` never adjust scroll (routing is skipped).
|
|
327
326
|
|
|
328
327
|
```
|
|
329
328
|
scroll flow
|
|
@@ -336,16 +335,16 @@ scroll flow
|
|
|
336
335
|
|
|
337
336
|
- `format(uri)` — normalizes a path relative to `base`. Returns `false` when `uri` is outside of `base`.
|
|
338
337
|
- `match(uri)` — returns a Promise of `{ route, params } | null` using string/RegExp patterns and validators. Awaits an async `validate(params)` if provided.
|
|
339
|
-
- `goto(uri, { replace? })` — fires route-level `
|
|
338
|
+
- `goto(uri, { replace? })` — fires route-level `before_route_leave('goto')`, calls global `before_navigate`, saves scroll, runs loaders, pushes/replaces, and completes via `after_navigate`.
|
|
340
339
|
- `init()` — wires global listeners (`popstate`, `pushstate`, `replacestate`, click) and optional hover/tap preloading; immediately processes the current location.
|
|
341
340
|
- `destroy()` — removes listeners added by `init()`.
|
|
342
341
|
- `preload(uri)` — pre-executes a route's `loaders` for a path and caches the result; concurrent calls are deduped.
|
|
343
|
-
- `
|
|
344
|
-
- `
|
|
342
|
+
- `push_state(url?, state?)` — shallow push that updates the URL and `history.state` without route processing.
|
|
343
|
+
- `replace_state(url?, state?)` — shallow replace that updates the URL and `history.state` without route processing.
|
|
345
344
|
|
|
346
345
|
### Built-in Validators
|
|
347
346
|
|
|
348
347
|
- `Navgo.validators.int({ min?, max? })` — `true` iff the value is an integer within optional bounds.
|
|
349
|
-
- `Navgo.validators.
|
|
348
|
+
- `Navgo.validators.one_of(iterable)` — `true` iff the value is in the provided set.
|
|
350
349
|
|
|
351
350
|
Attach validators via a route tuple's `data.param_validators` to constrain matches.
|