@real-router/svelte 0.17.31 → 0.17.33

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.
@@ -71,15 +71,25 @@ export type RouteExitHandler = (context: RouteExitContext) => void | Promise<voi
71
71
  * <div bind:this={el}>...</div>
72
72
  * ```
73
73
  *
74
- * @example Reading rich transition metadata via `nextRoute.transition`
74
+ * @example Detecting that you are leaving a subtree
75
75
  * ```svelte
76
76
  * <script lang="ts">
77
- * useRouteExit(({ nextRoute }) => {
78
- * if (nextRoute.transition.segments.deactivated.includes("products")) {
77
+ * const inProducts = (name: string) =>
78
+ * name === "products" || name.startsWith("products.");
79
+ *
80
+ * useRouteExit(({ route, nextRoute }) => {
81
+ * if (inProducts(route.name) && !inProducts(nextRoute.name)) {
79
82
  * productCache.clear();
80
83
  * }
81
84
  * });
82
85
  * </script>
83
86
  * ```
87
+ *
88
+ * ⚠ Do NOT read `nextRoute.transition` here. `nextRoute` is the PENDING target,
89
+ * and the pipeline gives it the neutral default — empty `segments`, optional
90
+ * flags `undefined` — so the example this replaced read `[]` and `undefined`
91
+ * whatever the navigation was. (It threw outright until real-router#1976
92
+ * attached the field.) The real metadata is written at the COMMIT: read it in
93
+ * `router.subscribe`, `onTransitionSuccess`, or `getState()`.
84
94
  */
85
95
  export declare function useRouteExit(handler: RouteExitHandler, options?: UseRouteExitOptions): void;
@@ -50,16 +50,26 @@ import { useRouter } from "./useRouter.svelte";
50
50
  * <div bind:this={el}>...</div>
51
51
  * ```
52
52
  *
53
- * @example Reading rich transition metadata via `nextRoute.transition`
53
+ * @example Detecting that you are leaving a subtree
54
54
  * ```svelte
55
55
  * <script lang="ts">
56
- * useRouteExit(({ nextRoute }) => {
57
- * if (nextRoute.transition.segments.deactivated.includes("products")) {
56
+ * const inProducts = (name: string) =>
57
+ * name === "products" || name.startsWith("products.");
58
+ *
59
+ * useRouteExit(({ route, nextRoute }) => {
60
+ * if (inProducts(route.name) && !inProducts(nextRoute.name)) {
58
61
  * productCache.clear();
59
62
  * }
60
63
  * });
61
64
  * </script>
62
65
  * ```
66
+ *
67
+ * ⚠ Do NOT read `nextRoute.transition` here. `nextRoute` is the PENDING target,
68
+ * and the pipeline gives it the neutral default — empty `segments`, optional
69
+ * flags `undefined` — so the example this replaced read `[]` and `undefined`
70
+ * whatever the navigation was. (It threw outright until real-router#1976
71
+ * attached the field.) The real metadata is written at the COMMIT: read it in
72
+ * `router.subscribe`, `onTransitionSuccess`, or `getState()`.
63
73
  */
64
74
  export function useRouteExit(handler, options) {
65
75
  const router = useRouter();
@@ -127,37 +127,62 @@ export function navigateWithHash(router, routeName, routeParams, routeSearch, ha
127
127
  opts.hash = hash;
128
128
  }
129
129
  const current = router.getState();
130
- // "Does this link point at where we already are?" is a question the router
131
- // owns, and asking it here by hand got two things wrong at once (#1555).
132
- //
133
- // The hand-rolled version compared with `Object.is` per key, so a link writing
134
- // `routeSearch={{ page: "2" }}` never matched a state parsed from `?page=2`,
135
- // where the value is the NUMBER 2 — the bypass silently did not fire, core
136
- // rejected the navigation as SAME_STATES, and `<Link hash>` looked dead. The
137
- // predicate carries the provenance-tolerant comparison (#1554) that makes the
138
- // two forms equal because they print the same URL.
139
- //
140
- // It also compared the caller's whole `routeParams` bag against `state.params`,
141
- // which stopped meaning anything once the channels split (#1548) — the
142
- // predicate applies the channel rule instead of re-deriving it.
143
- //
144
- // `strictEquality: true` — a link to a PARENT route is a different location, so
145
- // the hierarchical arm must not match. `ignoreQueryParams: false` — the query is
146
- // part of the location here; ignoring it would fire the bypass across a real
147
- // query change and let `force: true` smuggle it through as a hash change.
148
- if (current !== undefined &&
149
- router.isActiveRoute(routeName, routeParams, routeSearch ?? current.search, true, false)) {
150
- const currentHash = current.context?.url?.hash ??
151
- "";
152
- const newHash = hash ?? currentHash;
153
- if (currentHash !== newHash) {
154
- opts.force = true;
155
- opts.hashChange = true;
130
+ // What the navigation gets — the caller's `routeSearch` unless the bypass
131
+ // below substitutes it (#1925).
132
+ let navigatedSearch = routeSearch;
133
+ if (current !== undefined) {
134
+ // ONE expression, asked ONCE and navigated with under the bypass (#1925).
135
+ // The predicate's answer is only as good as the value it answered ABOUT, so
136
+ // the two must be the same value — a second copy of this expression would
137
+ // let them drift apart with nothing to catch it. The name is what keeps
138
+ // them in agreement structurally rather than by convention.
139
+ const sameLocationSearch = routeSearch ?? current.search;
140
+ // "Does this link point at where we already are?" is a question the router
141
+ // owns, and asking it here by hand got two things wrong at once (#1555).
142
+ //
143
+ // The hand-rolled version compared with `Object.is` per key, so a link
144
+ // writing `routeSearch={{ page: "2" }}` never matched a state parsed from
145
+ // `?page=2`, where the value is the NUMBER 2 — the bypass silently did not
146
+ // fire, core rejected the navigation as SAME_STATES, and `<Link hash>`
147
+ // looked dead. The predicate carries the provenance-tolerant comparison
148
+ // (#1554) that makes the two forms equal because they print the same URL.
149
+ //
150
+ // It also compared the caller's whole `routeParams` bag against
151
+ // `state.params`, which stopped meaning anything once the channels split
152
+ // (#1548) — the predicate applies the channel rule instead of re-deriving
153
+ // it.
154
+ //
155
+ // `strictEquality: true` — a link to a PARENT route is a different
156
+ // location, so the hierarchical arm must not match. `ignoreQueryParams:
157
+ // false` — the query is part of the location here; ignoring it would fire
158
+ // the bypass across a real query change and let `force: true` smuggle it
159
+ // through as a hash change.
160
+ if (router.isActiveRoute(routeName, routeParams, sameLocationSearch, true, false)) {
161
+ const currentHash = current.context?.url
162
+ ?.hash ?? "";
163
+ const newHash = hash ?? currentHash;
164
+ if (currentHash !== newHash) {
165
+ opts.force = true;
166
+ opts.hashChange = true;
167
+ // Reaching here means the predicate answered "this is where we already
168
+ // are, only the fragment differs", and `force` + `hashChange` announce
169
+ // exactly that. The bare `routeSearch` would say "no query" rather than
170
+ // "unchanged" — on `/docs?tab=api` a fragment link would land on
171
+ // `/docs`, moving the location it just announced as unmoved. Same slot,
172
+ // same conclusion as `scroll-spy.ts`, which re-navigates with both
173
+ // channels of the CURRENT state.
174
+ //
175
+ // ⚠ Only under the bypass. Without it the helper claims no sameness, so
176
+ // `<Link routeName="docs">` means `/docs` — substituting there would
177
+ // turn a real navigation into a no-op. An explicit `routeSearch={{}}`
178
+ // still clears the query, because `{}` is not nullish. Both pinned by
179
+ // the #1925 suite.
180
+ navigatedSearch = sameLocationSearch;
181
+ }
156
182
  }
157
183
  }
158
- // Query channel at position 3 (RFC-4 M2 / #1548) — from the `routeSearch`
159
- // prop; opts at position 4. `undefined` when the link has no `routeSearch`.
160
- return router.navigate(routeName, routeParams, routeSearch, opts);
184
+ // Query channel at position 3 (RFC-4 M2 / #1548); opts at position 4.
185
+ return router.navigate(routeName, routeParams, navigatedSearch, opts);
161
186
  }
162
187
  // Match-any-whitespace regex shared across calls. RegExp literals at
163
188
  // call-site recompile in some engines; lifting it avoids that microcost
@@ -294,7 +294,12 @@ export function createScrollRestoration(router, options) {
294
294
  // leaves opts.reload undefined, so dropping the plugin arm would regress F5
295
295
  // scroll-restore. Browser-plugin's F5 is not covered (no priming, out of
296
296
  // scope).
297
- if (route.transition.reload || nav?.navigationType === "reload") {
297
+ // `?.` on a required field: core commits a foreign State's ABSENT
298
+ // `transition` rather than fabricating one (#1792 / #1976), and this runs
299
+ // on whatever `subscribe` hands over. Absent falls through to the plugin
300
+ // arm, which is the pre-#1976 answer for a state carrying no meta.
301
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- the required field is genuinely absent on a foreign committed State
302
+ if (route.transition?.reload || nav?.navigationType === "reload") {
298
303
  const key = safeKeyOf(route);
299
304
  restorePos(key === null ? 0 : (loadStore()[key] ?? 0));
300
305
  return;
@@ -305,7 +310,9 @@ export function createScrollRestoration(router, options) {
305
310
  return;
306
311
  }
307
312
  // Genuine in-place replace (not a traversal) — leave scroll untouched.
308
- if (route.transition.replace || nav?.navigationType === "replace") {
313
+ // `?.` for the same reason as the reload arm above.
314
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- the required field is genuinely absent on a foreign committed State
315
+ if (route.transition?.replace || nav?.navigationType === "replace") {
309
316
  return;
310
317
  }
311
318
  scrollToHashOrTop(route);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real-router/svelte",
3
- "version": "0.17.31",
3
+ "version": "0.17.33",
4
4
  "type": "module",
5
5
  "description": "Svelte 5 integration for Real-Router",
6
6
  "svelte": "./dist/index.js",
@@ -48,9 +48,9 @@
48
48
  "license": "MIT",
49
49
  "sideEffects": false,
50
50
  "dependencies": {
51
- "@real-router/core": "^0.110.0",
51
+ "@real-router/core": "^0.111.5",
52
52
  "@real-router/route-utils": "^0.3.0",
53
- "@real-router/sources": "^0.14.13"
53
+ "@real-router/sources": "^0.14.14"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@sveltejs/package": "2.5.8",
@@ -63,7 +63,7 @@
63
63
  "svelte": "~5.56.4",
64
64
  "svelte-check": "4.6.0",
65
65
  "svelte-eslint-parser": "1.8.0",
66
- "@real-router/browser-plugin": "^0.21.13"
66
+ "@real-router/browser-plugin": "^0.21.14"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "svelte": ">=5.7.0"