@real-router/svelte 0.24.3 → 0.24.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/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  npm install @real-router/svelte @real-router/core @real-router/browser-plugin
11
11
  ```
12
12
 
13
- **Peer dependency:** `svelte` >= 5.7.0
13
+ **Peer dependencies:** `@real-router/core`, `svelte` >= 5.7.0
14
14
 
15
15
  ## Quick Start
16
16
 
@@ -497,7 +497,7 @@ Emits a forced same-route transition with `{ hash, replace: true, force: true, h
497
497
 
498
498
  Options: `{ selector: string, rootMargin?: string, scrollContainer?: () => HTMLElement | null }`. Reactive via `$effect` — primitive fields (`selector`, `rootMargin`) wrapped in `$derived`, so inline objects with the same values don't thrash; `scrollContainer` getter pulled via `untrack` (identity changes don't retrigger). Empty `selector` / `undefined` = off. SSR / browsers without `IntersectionObserver` = NOOP. Requires `browser-plugin` or `navigation-plugin` (hash-plugin / memory-plugin → warn-once + NOOP).
499
499
 
500
- Behaviour is identical to the React adapter — see the [React Scroll Spy demo](../../examples/web/react/hash-examples/scroll-spy/) (12 sections, TOC sidebar, 10 e2e scenarios) and the [Scroll Spy guide](https://github.com/greydragon888/real-router/wiki/Scroll-Spy).
500
+ Behaviour is identical to the React adapter — see the [React Scroll Spy demo](../../examples/web/react/hash-examples/scroll-spy/) (12 sections, TOC sidebar, Playwright-tested) and the [Scroll Spy guide](https://github.com/greydragon888/real-router/wiki/Scroll-Spy).
501
501
 
502
502
  ## View Transitions
503
503
 
@@ -553,12 +553,12 @@ Full documentation: [Wiki](https://github.com/greydragon888/real-router/wiki)
553
553
 
554
554
  ## Related Packages
555
555
 
556
- | Package | Description |
557
- | ---------------------------------------------------------------------------------------- | ------------------------------------ |
558
- | [@real-router/core](https://www.npmjs.com/package/@real-router/core) | Core router (required dependency) |
559
- | [@real-router/browser-plugin](https://www.npmjs.com/package/@real-router/browser-plugin) | Browser History API integration |
560
- | [@real-router/sources](https://www.npmjs.com/package/@real-router/sources) | Subscription layer (used internally) |
561
- | [@real-router/route-utils](https://www.npmjs.com/package/@real-router/route-utils) | Route tree queries (`useRouteUtils`) |
556
+ | Package | Description |
557
+ | ---------------------------------------------------------------------------------------- | -------------------------------------- |
558
+ | [@real-router/core](https://www.npmjs.com/package/@real-router/core) | Core router (required peer dependency) |
559
+ | [@real-router/browser-plugin](https://www.npmjs.com/package/@real-router/browser-plugin) | Browser History API integration |
560
+ | [@real-router/sources](https://www.npmjs.com/package/@real-router/sources) | Subscription layer (used internally) |
561
+ | [@real-router/route-utils](https://www.npmjs.com/package/@real-router/route-utils) | Route tree queries (`useRouteUtils`) |
562
562
 
563
563
  ## Contributing
564
564
 
@@ -39,9 +39,15 @@ export type RouteExitHandler = (context: RouteExitContext) => void | Promise<voi
39
39
  *
40
40
  * If the handler returns a Promise, the router blocks on it. If the
41
41
  * Promise resolves, navigation proceeds. If it **rejects**, the router
42
- * rejects `navigate()` with the handler's **original error** and emits
43
- * `TRANSITION_ERROR` — it is NOT re-coded to `TRANSITION_CANCELLED` (that
44
- * arises only when the navigation's `signal` aborts).
42
+ * rejects `navigate()` with the handler's **original error** (a value that
43
+ * is not an `Error` arrives wrapped in one) and emits `TRANSITION_ERROR` —
44
+ * it is NOT re-coded to `TRANSITION_CANCELLED` (that arises when the
45
+ * navigation's `signal` aborts). A handler that rejects with a
46
+ * `RouterError(TRANSITION_CANCELLED)` itself cancels the navigation quietly:
47
+ * `navigate()` rejects with that error and the router emits
48
+ * `TRANSITION_CANCEL` instead (#2787). A handler that rejects with the
49
+ * error the router ended ANOTHER navigation with — it awaited that
50
+ * navigation's work — fails this one with `CANNOT_DEACTIVATE` (#2786).
45
51
  *
46
52
  * **Handler reactivity (Svelte):** Svelte composables run **once** at
47
53
  * component init; `handler` is captured in closure at the call site. To
@@ -18,9 +18,15 @@ import { useRouter } from "./useRouter.svelte";
18
18
  *
19
19
  * If the handler returns a Promise, the router blocks on it. If the
20
20
  * Promise resolves, navigation proceeds. If it **rejects**, the router
21
- * rejects `navigate()` with the handler's **original error** and emits
22
- * `TRANSITION_ERROR` — it is NOT re-coded to `TRANSITION_CANCELLED` (that
23
- * arises only when the navigation's `signal` aborts).
21
+ * rejects `navigate()` with the handler's **original error** (a value that
22
+ * is not an `Error` arrives wrapped in one) and emits `TRANSITION_ERROR` —
23
+ * it is NOT re-coded to `TRANSITION_CANCELLED` (that arises when the
24
+ * navigation's `signal` aborts). A handler that rejects with a
25
+ * `RouterError(TRANSITION_CANCELLED)` itself cancels the navigation quietly:
26
+ * `navigate()` rejects with that error and the router emits
27
+ * `TRANSITION_CANCEL` instead (#2787). A handler that rejects with the
28
+ * error the router ended ANOTHER navigation with — it awaited that
29
+ * navigation's work — fails this one with `CANNOT_DEACTIVATE` (#2786).
24
30
  *
25
31
  * **Handler reactivity (Svelte):** Svelte composables run **once** at
26
32
  * component init; `handler` is captured in closure at the call site. To
@@ -19,6 +19,9 @@ export interface DirectionTracker {
19
19
  * clicks on `<Link>` / programmatic `router.navigate(...)` do not.
20
20
  * - On every `subscribeLeave`, write
21
21
  * `popstateFlag ? "back" : "forward"` and reset the flag.
22
+ * - A `popstate` that arrives while a transition is in flight arms the
23
+ * flag of the transition after it, which is the one a URL plugin starts
24
+ * for it.
22
25
  *
23
26
  * Returns `{ destroy }` to clean up the listener and clear the dataset
24
27
  * attribute.
@@ -22,6 +22,9 @@ const NOOP_INSTANCE = Object.freeze({
22
22
  * clicks on `<Link>` / programmatic `router.navigate(...)` do not.
23
23
  * - On every `subscribeLeave`, write
24
24
  * `popstateFlag ? "back" : "forward"` and reset the flag.
25
+ * - A `popstate` that arrives while a transition is in flight arms the
26
+ * flag of the transition after it, which is the one a URL plugin starts
27
+ * for it.
25
28
  *
26
29
  * Returns `{ destroy }` to clean up the listener and clear the dataset
27
30
  * attribute.
@@ -34,7 +37,12 @@ export function createDirectionTracker(router) {
34
37
  document.documentElement.dataset.navDirection = "forward";
35
38
  // The `toState` the transition in flight was announced with.
36
39
  let inFlight;
37
- let armedMidTransition = false;
40
+ // The address of a popstate that arrived while a transition was in flight.
41
+ // A URL plugin starts its traversal after that transition, so its flag is
42
+ // kept apart from the one the transition in flight may own.
43
+ let replay;
44
+ // The last transition passed the flag on.
45
+ let passedOn = false;
38
46
  // ⚑ The flag is SPENT by the transition it belongs to, whether or not that
39
47
  // transition reaches a leave phase (#1924). It is armed by any `popstate` on
40
48
  // `globalThis`, not only one the router consumes, so `history.back()` onto an
@@ -49,35 +57,49 @@ export function createDirectionTracker(router) {
49
57
  // in-flight transition and replays it from that transition's `finally`. The
50
58
  // gap has no upper bound. Measured on the obvious choice: a task-boundary
51
59
  // expiry disarms a live navigation on ONE async `canDeactivate`.
52
- // ⚑ A traversal onto the URL of the router's own state starts no
53
- // transition: a URL plugin's return after a refused Back or Forward
54
- // (#2740), or a Back onto an entry holding that state. The URL comes from
55
- // the URL plugin's `buildUrl`; without one, every popstate arms the flag.
56
- const landsOnRouterState = () => {
57
- const state = router.getState();
60
+ // The address the URL plugin prints a state at, or `undefined` without
61
+ // one, or when its `buildUrl` throws or answers none.
62
+ const hrefOf = (state) => {
58
63
  const buildUrl = router.buildUrl;
59
- if (state === undefined || buildUrl === undefined) {
60
- return false;
64
+ if (buildUrl === undefined) {
65
+ return undefined;
61
66
  }
62
67
  try {
63
68
  const hash = state.context
64
69
  ?.url?.hash;
65
70
  const url = buildUrl(state.name, state.params, state.search, hash ? { hash } : undefined);
66
- return (url !== undefined &&
67
- new URL(url, globalThis.location.href).href === globalThis.location.href);
71
+ return url === undefined
72
+ ? undefined
73
+ : new URL(url, globalThis.location.href).href;
68
74
  }
69
75
  catch {
70
- return false;
76
+ return undefined;
71
77
  }
72
78
  };
79
+ // ⚑ A traversal onto the URL of the router's own state starts no
80
+ // transition: a URL plugin's return after a refused Back or Forward
81
+ // (#2740), or a Back onto an entry holding that state. The URL comes from
82
+ // the URL plugin's `buildUrl`; without one, every popstate arms the flag.
83
+ const landsOnRouterState = () => {
84
+ const state = router.getState();
85
+ return state !== undefined && hrefOf(state) === globalThis.location.href;
86
+ };
73
87
  const onPopstate = () => {
74
88
  if (landsOnRouterState()) {
89
+ // The URL plugin returns after a traversal it refused or one a guard
90
+ // cancelled quietly, and drops the popstate that waited behind it, so
91
+ // the flag that popstate's replay would have spent ends here.
92
+ if (inFlight === undefined) {
93
+ popstateFlag = false;
94
+ }
75
95
  return;
76
96
  }
77
- popstateFlag = true;
78
- // A deferred event belongs to the replay, not to the transition running
79
- // now, so that transition's terminal event must not spend it.
80
- armedMidTransition = inFlight !== undefined;
97
+ if (inFlight === undefined) {
98
+ popstateFlag = true;
99
+ }
100
+ else {
101
+ replay = globalThis.location.href;
102
+ }
81
103
  };
82
104
  // IMPORTANT — listener-ordering: `popstate` fires on `window`, which
83
105
  // has no DOM descendants, so capture phase is moot. Listeners are
@@ -88,13 +110,13 @@ export function createDirectionTracker(router) {
88
110
  // `subscribeLeave` while `popstateFlag` is still `false`.
89
111
  globalThis.addEventListener("popstate", onPopstate);
90
112
  const api = getPluginApi(router);
113
+ // The flag of a popstate that arrived during the transition passes to the
114
+ // next one; the transition's own, unspent, ends with it.
91
115
  const onSettled = () => {
92
116
  inFlight = undefined;
93
- if (armedMidTransition) {
94
- armedMidTransition = false;
95
- return;
96
- }
97
- popstateFlag = false;
117
+ popstateFlag = replay !== undefined;
118
+ passedOn = popstateFlag;
119
+ replay = undefined;
98
120
  };
99
121
  // Core also reports a refusal that starts no transition on
100
122
  // `TRANSITION_ERROR`, and may do so while another transition runs. A
@@ -105,18 +127,29 @@ export function createDirectionTracker(router) {
105
127
  const onError = (toState) => {
106
128
  if (inFlight === undefined || toState?.context === inFlight.context) {
107
129
  onSettled();
130
+ return;
108
131
  }
109
- else {
110
- // A popstate armed while the transition runs may be the one this
111
- // refusal answers: a URL plugin handles a popstate that arrives during a
112
- // programmatic transition on the spot. So that transition's terminal
113
- // spends the flag instead of keeping it for a replay.
114
- armedMidTransition = false;
132
+ // A popstate that arrived while the transition runs may be the one this
133
+ // refusal answers: a URL plugin handles a popstate that arrives during a
134
+ // programmatic transition on the spot. It is, unless the refusal names
135
+ // another address.
136
+ const href = toState === undefined ? undefined : hrefOf(toState);
137
+ if (href === undefined || href === replay) {
138
+ replay = undefined;
115
139
  }
116
140
  };
117
141
  const unsubs = [
118
142
  api.addEventListener(events.TRANSITION_START, (toState) => {
119
143
  inFlight = toState;
144
+ // A flag armed between transitions that finds the router's address is a
145
+ // return's: it landed before the write of a navigation committed on its
146
+ // way, so its popstate read another address (#2879). A History plugin
147
+ // starts a traversal within its popstate, at the address that popstate
148
+ // read. A flag passed on is left alone: the plugin can write the router's
149
+ // URL over the entry its replay reached.
150
+ if (popstateFlag && !passedOn && landsOnRouterState()) {
151
+ popstateFlag = false;
152
+ }
120
153
  }),
121
154
  api.addEventListener(events.TRANSITION_SUCCESS, onSettled),
122
155
  api.addEventListener(events.TRANSITION_ERROR, onError),
@@ -126,7 +159,6 @@ export function createDirectionTracker(router) {
126
159
  ? "back"
127
160
  : "forward";
128
161
  popstateFlag = false;
129
- armedMidTransition = false;
130
162
  }),
131
163
  ];
132
164
  return {
@@ -55,7 +55,6 @@ export function createRouteAnnouncer(router, options) {
55
55
  }
56
56
  const prefix = options?.prefix ?? "Navigated to ";
57
57
  const getCustomText = options?.getAnnouncementText;
58
- let isInitialNavigation = true;
59
58
  let isReady = false;
60
59
  let isDestroyed = false;
61
60
  let lastAnnouncedText = "";
@@ -85,9 +84,14 @@ export function createRouteAnnouncer(router, options) {
85
84
  doAnnounce(text, document.querySelector("h1"));
86
85
  }
87
86
  }, SAFARI_READY_DELAY);
88
- const unsubscribe = router.subscribe(({ route }) => {
89
- if (isInitialNavigation) {
90
- isInitialNavigation = false;
87
+ // The initial page load is the commit with no previous route — `start()`'s
88
+ // — of a router that shows no state when the announcer is created. The
89
+ // canonical mount order creates it after that commit; a `start()` after
90
+ // `stop()` changes the page with no load, and is announced.
91
+ let initialLoadPending = router.getState() === undefined;
92
+ const unsubscribe = router.subscribe(({ route, previousRoute }) => {
93
+ if (previousRoute === undefined && initialLoadPending) {
94
+ initialLoadPending = false;
91
95
  return;
92
96
  }
93
97
  // Double rAF: waits for two paint frames so the incoming route's DOM
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real-router/svelte",
3
- "version": "0.24.3",
3
+ "version": "0.24.5",
4
4
  "type": "module",
5
5
  "description": "Svelte 5 integration for Real-Router",
6
6
  "svelte": "./dist/index.js",
@@ -54,8 +54,8 @@
54
54
  "@real-router/sources": "^0.15.1"
55
55
  },
56
56
  "devDependencies": {
57
- "@real-router/browser-plugin": "^0.26.2",
58
- "@real-router/core": "^0.148.1",
57
+ "@real-router/browser-plugin": "^0.27.0",
58
+ "@real-router/core": "^0.154.0",
59
59
  "@sveltejs/package": "2.5.8",
60
60
  "@sveltejs/vite-plugin-svelte": "7.3.1",
61
61
  "@testing-library/jest-dom": "~7.0.1",
@@ -66,7 +66,7 @@
66
66
  "svelte-check": "4.7.6"
67
67
  },
68
68
  "peerDependencies": {
69
- "@real-router/core": ">=0.148.1",
69
+ "@real-router/core": ">=0.154.0",
70
70
  "svelte": ">=5.7.0"
71
71
  },
72
72
  "scripts": {