@real-router/svelte 0.24.2 → 0.24.4

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.
@@ -39,9 +39,13 @@ 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).
45
49
  *
46
50
  * **Handler reactivity (Svelte):** Svelte composables run **once** at
47
51
  * component init; `handler` is captured in closure at the call site. To
@@ -18,9 +18,13 @@ 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).
24
28
  *
25
29
  * **Handler reactivity (Svelte):** Svelte composables run **once** at
26
30
  * 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.
@@ -32,8 +35,12 @@ export function createDirectionTracker(router) {
32
35
  }
33
36
  let popstateFlag = false;
34
37
  document.documentElement.dataset.navDirection = "forward";
35
- let transitionInFlight = false;
36
- let armedMidTransition = false;
38
+ // The `toState` the transition in flight was announced with.
39
+ let inFlight;
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;
37
44
  // ⚑ The flag is SPENT by the transition it belongs to, whether or not that
38
45
  // transition reaches a leave phase (#1924). It is armed by any `popstate` on
39
46
  // `globalThis`, not only one the router consumes, so `history.back()` onto an
@@ -48,35 +55,49 @@ export function createDirectionTracker(router) {
48
55
  // in-flight transition and replays it from that transition's `finally`. The
49
56
  // gap has no upper bound. Measured on the obvious choice: a task-boundary
50
57
  // expiry disarms a live navigation on ONE async `canDeactivate`.
51
- // ⚑ A traversal onto the URL of the router's own state starts no
52
- // transition: a URL plugin's return after a refused Back or Forward
53
- // (#2740), or a Back onto an entry holding that state. The URL comes from
54
- // the URL plugin's `buildUrl`; without one, every popstate arms the flag.
55
- const landsOnRouterState = () => {
56
- const state = router.getState();
58
+ // The address the URL plugin prints a state at, or `undefined` without
59
+ // one, or when its `buildUrl` throws or answers none.
60
+ const hrefOf = (state) => {
57
61
  const buildUrl = router.buildUrl;
58
- if (state === undefined || buildUrl === undefined) {
59
- return false;
62
+ if (buildUrl === undefined) {
63
+ return undefined;
60
64
  }
61
65
  try {
62
66
  const hash = state.context
63
67
  ?.url?.hash;
64
68
  const url = buildUrl(state.name, state.params, state.search, hash ? { hash } : undefined);
65
- return (url !== undefined &&
66
- new URL(url, globalThis.location.href).href === globalThis.location.href);
69
+ return url === undefined
70
+ ? undefined
71
+ : new URL(url, globalThis.location.href).href;
67
72
  }
68
73
  catch {
69
- return false;
74
+ return undefined;
70
75
  }
71
76
  };
77
+ // ⚑ A traversal onto the URL of the router's own state starts no
78
+ // transition: a URL plugin's return after a refused Back or Forward
79
+ // (#2740), or a Back onto an entry holding that state. The URL comes from
80
+ // the URL plugin's `buildUrl`; without one, every popstate arms the flag.
81
+ const landsOnRouterState = () => {
82
+ const state = router.getState();
83
+ return state !== undefined && hrefOf(state) === globalThis.location.href;
84
+ };
72
85
  const onPopstate = () => {
73
86
  if (landsOnRouterState()) {
87
+ // The URL plugin returns after a traversal it refused or one a guard
88
+ // cancelled quietly, and drops the popstate that waited behind it, so
89
+ // the flag that popstate's replay would have spent ends here.
90
+ if (inFlight === undefined) {
91
+ popstateFlag = false;
92
+ }
74
93
  return;
75
94
  }
76
- popstateFlag = true;
77
- // A deferred event belongs to the replay, not to the transition running
78
- // now, so that transition's terminal event must not spend it.
79
- armedMidTransition = transitionInFlight;
95
+ if (inFlight === undefined) {
96
+ popstateFlag = true;
97
+ }
98
+ else {
99
+ replay = globalThis.location.href;
100
+ }
80
101
  };
81
102
  // IMPORTANT — listener-ordering: `popstate` fires on `window`, which
82
103
  // has no DOM descendants, so capture phase is moot. Listeners are
@@ -87,27 +108,45 @@ export function createDirectionTracker(router) {
87
108
  // `subscribeLeave` while `popstateFlag` is still `false`.
88
109
  globalThis.addEventListener("popstate", onPopstate);
89
110
  const api = getPluginApi(router);
111
+ // The flag of a popstate that arrived during the transition passes to the
112
+ // next one; the transition's own, unspent, ends with it.
90
113
  const onSettled = () => {
91
- transitionInFlight = false;
92
- if (armedMidTransition) {
93
- armedMidTransition = false;
114
+ inFlight = undefined;
115
+ popstateFlag = replay !== undefined;
116
+ replay = undefined;
117
+ };
118
+ // Core also reports a refusal that starts no transition on
119
+ // `TRANSITION_ERROR`, and may do so while another transition runs. A
120
+ // transition's own error carries a `toState` with that transition's
121
+ // `context`, so another one does not settle it (#2779). With nothing in
122
+ // flight every error settles: the `SAME_STATES` arc above spends the flag
123
+ // that way.
124
+ const onError = (toState) => {
125
+ if (inFlight === undefined || toState?.context === inFlight.context) {
126
+ onSettled();
94
127
  return;
95
128
  }
96
- popstateFlag = false;
129
+ // A popstate that arrived while the transition runs may be the one this
130
+ // refusal answers: a URL plugin handles a popstate that arrives during a
131
+ // programmatic transition on the spot. It is, unless the refusal names
132
+ // another address.
133
+ const href = toState === undefined ? undefined : hrefOf(toState);
134
+ if (href === undefined || href === replay) {
135
+ replay = undefined;
136
+ }
97
137
  };
98
138
  const unsubs = [
99
- api.addEventListener(events.TRANSITION_START, () => {
100
- transitionInFlight = true;
139
+ api.addEventListener(events.TRANSITION_START, (toState) => {
140
+ inFlight = toState;
101
141
  }),
102
142
  api.addEventListener(events.TRANSITION_SUCCESS, onSettled),
103
- api.addEventListener(events.TRANSITION_ERROR, onSettled),
143
+ api.addEventListener(events.TRANSITION_ERROR, onError),
104
144
  api.addEventListener(events.TRANSITION_CANCEL, onSettled),
105
145
  router.subscribeLeave(() => {
106
146
  document.documentElement.dataset.navDirection = popstateFlag
107
147
  ? "back"
108
148
  : "forward";
109
149
  popstateFlag = false;
110
- armedMidTransition = false;
111
150
  }),
112
151
  ];
113
152
  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.2",
3
+ "version": "0.24.4",
4
4
  "type": "module",
5
5
  "description": "Svelte 5 integration for Real-Router",
6
6
  "svelte": "./dist/index.js",
@@ -51,11 +51,11 @@
51
51
  "sideEffects": false,
52
52
  "dependencies": {
53
53
  "@real-router/route-utils": "^0.4.0",
54
- "@real-router/sources": "^0.15.0"
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.26.3",
58
+ "@real-router/core": "^0.151.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.151.0",
70
70
  "svelte": ">=5.7.0"
71
71
  },
72
72
  "scripts": {