@real-router/svelte 0.24.8 → 0.25.0

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
@@ -553,12 +553,13 @@ 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 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`) |
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
+ | [@real-router/ssr-utils](https://www.npmjs.com/package/@real-router/ssr-utils) | SSR helpers: serialize, hydrate, request scope, loader errors |
562
563
 
563
564
  ## Contributing
564
565
 
@@ -91,12 +91,13 @@ export declare function buildHref(router: Router, routeName: string, routeParams
91
91
  * `<Link>` click-handler navigation helper (#532).
92
92
  *
93
93
  * Wraps `router.navigate(name, params, search, opts)` — the query channel took
94
- * slot 3 in RFC-4 M2 (#1548) — with same-route different-hash
95
- * detection: when the consumer clicks a hash-bearing Link that targets the
96
- * current route with the same params but a different fragment, core's
97
- * SAME_STATES check would otherwise reject the navigation. The helper adds
98
- * `force: true` and `hashChange: true` automatically — subscribers can then
99
- * disambiguate via `state.context.url.hashChanged`.
94
+ * slot 3 in RFC-4 M2 (#1548) — with same-page different-hash detection. A
95
+ * hash-bearing Link that asks for a different fragment gets `force: true` and
96
+ * `hashChange: true` in two cases: its values match the page, or core would
97
+ * refuse it as `SAME_STATES` because it builds the committed path with other
98
+ * values — then the page itself is re-committed. `printsPath` and the path arm
99
+ * name the exceptions to the second case. Subscribers can then disambiguate
100
+ * via `state.context.url.hashChanged`.
100
101
  *
101
102
  * For pure programmatic same-route hash-only navigation, callers are
102
103
  * documented to pass `{ force: true }` themselves; the auto-bypass here is
@@ -1,3 +1,4 @@
1
+ import { UNKNOWN_ROUTE } from "@real-router/core";
1
2
  import { getPluginApi } from "@real-router/core/api";
2
3
  import { putField } from "@real-router/core/utils";
3
4
  /**
@@ -14,6 +15,7 @@ import { putField } from "@real-router/core/utils";
14
15
  * (#1798), which is the doctrine's own caveat and travels with it.
15
16
  */
16
17
  const objectKeys = Object.keys;
18
+ const hasOwn = Object.hasOwn;
17
19
  /**
18
20
  * The brand `packages/core/src/internals.ts` puts on every router it registers,
19
21
  * RE-DECLARED rather than imported (#2294).
@@ -299,16 +301,62 @@ export function buildHref(router, routeName, routeParams, routeSearch, hash) {
299
301
  return undefined;
300
302
  }
301
303
  }
304
+ /**
305
+ * Does `(routeName, routeParams, routeSearch)` print `path` through the
306
+ * resolving door `buildHref` renders (#2250)? `forwardState` resolves the
307
+ * chain — `forwardTo` and the interceptors plugins register on it — and
308
+ * `buildPathResolved` prints the target: for a link `navigate` accepts, the
309
+ * path core compares with the committed one.
310
+ *
311
+ * ⚠ No literal fallback: `router.buildPath` does not follow `forwardTo`, so it
312
+ * is not the path `navigate` builds.
313
+ *
314
+ * A router `getPluginApi` refuses — a test double, a `Proxy` wrapper — answers
315
+ * "no", as does a print that throws; the caller then navigates as the link
316
+ * wrote it.
317
+ */
318
+ function printsPath(router, routeName, routeParams, routeSearch, path) {
319
+ try {
320
+ const api = getPluginApi(router);
321
+ const forwarded = api.forwardState(routeName, routeParams, routeSearch);
322
+ return (api.buildPathResolved(forwarded.name, forwarded.params, forwarded.search) === path);
323
+ }
324
+ catch {
325
+ return false;
326
+ }
327
+ }
328
+ /**
329
+ * The bypass both arms of `navigateWithHash` add. `putField`, as for the
330
+ * fragment slot there (#2141): the spread `opts` has no own key for either flag
331
+ * unless the caller's extra options carried one, so a plain assignment would
332
+ * reach an ambient accessor on `Object.prototype`.
333
+ */
334
+ function addBypass(opts) {
335
+ const slots = opts;
336
+ putField(slots, "force", true);
337
+ putField(slots, "hashChange", true);
338
+ }
339
+ /**
340
+ * Whether the caller's own options already take the navigation past
341
+ * `SAME_STATES`: core reads `reload` and `force` as own keys of its copy of
342
+ * the options, by truth (`isSameNavigation`). The spread `opts` holds the
343
+ * caller's own enumerable keys as data, so reading it runs no caller code.
344
+ */
345
+ function callerBypasses(opts) {
346
+ return ((hasOwn(opts, "reload") && Boolean(opts.reload)) ||
347
+ (hasOwn(opts, "force") && Boolean(opts.force)));
348
+ }
302
349
  /**
303
350
  * `<Link>` click-handler navigation helper (#532).
304
351
  *
305
352
  * Wraps `router.navigate(name, params, search, opts)` — the query channel took
306
- * slot 3 in RFC-4 M2 (#1548) — with same-route different-hash
307
- * detection: when the consumer clicks a hash-bearing Link that targets the
308
- * current route with the same params but a different fragment, core's
309
- * SAME_STATES check would otherwise reject the navigation. The helper adds
310
- * `force: true` and `hashChange: true` automatically — subscribers can then
311
- * disambiguate via `state.context.url.hashChanged`.
353
+ * slot 3 in RFC-4 M2 (#1548) — with same-page different-hash detection. A
354
+ * hash-bearing Link that asks for a different fragment gets `force: true` and
355
+ * `hashChange: true` in two cases: its values match the page, or core would
356
+ * refuse it as `SAME_STATES` because it builds the committed path with other
357
+ * values — then the page itself is re-committed. `printsPath` and the path arm
358
+ * name the exceptions to the second case. Subscribers can then disambiguate
359
+ * via `state.context.url.hashChanged`.
312
360
  *
313
361
  * For pure programmatic same-route hash-only navigation, callers are
314
362
  * documented to pass `{ force: true }` themselves; the auto-bypass here is
@@ -355,18 +403,18 @@ export function navigateWithHash(router, routeName, routeParams, routeSearch, ha
355
403
  // (#1548) — the predicate applies the channel rule instead of re-deriving
356
404
  // it.
357
405
  //
358
- // `strictEquality: true` — a link to a PARENT route is a different
359
- // location, so the hierarchical arm must not match. `ignoreQueryParams:
360
- // false` — the query is part of the location here; ignoring it would fire
361
- // the bypass across a real query change and let `force: true` smuggle it
362
- // through as a hash change.
363
- if (router.isActiveRoute(routeName, routeParams, sameLocationSearch, true, false)) {
364
- const currentHash = current.context?.url
365
- ?.hash ?? "";
366
- const newHash = hash ?? currentHash;
367
- if (currentHash !== newHash) {
368
- opts.force = true;
369
- opts.hashChange = true;
406
+ // `strictEquality: true` — the hierarchical arm would match a parent route
407
+ // on a child page at another URL, and the bypass would announce that move
408
+ // as a fragment change. `ignoreQueryParams: false` — the query is part of
409
+ // the location here; ignoring it would fire the bypass across a real query
410
+ // change and let `force: true` smuggle it through as a hash change.
411
+ const sameValues = router.isActiveRoute(routeName, routeParams, sameLocationSearch, true, false);
412
+ const currentHash = current.context?.url?.hash ??
413
+ "";
414
+ const newHash = hash ?? currentHash;
415
+ if (currentHash !== newHash) {
416
+ if (sameValues) {
417
+ addBypass(opts);
370
418
  // Reaching here means the predicate answered "this is where we already
371
419
  // are, only the fragment differs", and `force` + `hashChange` announce
372
420
  // exactly that. The bare `routeSearch` would say "no query" rather than
@@ -382,6 +430,28 @@ export function navigateWithHash(router, routeName, routeParams, routeSearch, ha
382
430
  // the #1925 suite.
383
431
  navigatedSearch = sameLocationSearch;
384
432
  }
433
+ else if (!callerBypasses(opts) &&
434
+ current.name !== UNKNOWN_ROUTE &&
435
+ printsPath(router, routeName, routeParams, routeSearch, current.path)) {
436
+ // `sameValues` misses part of what core refuses: core refuses a
437
+ // navigation with neither `reload` nor `force` truthy in its options
438
+ // that builds the path the router shows (`isSameNavigation`), and a
439
+ // link can build that path with other values — for example one that
440
+ // prints no key (`[]`, `null` under `nullFormat: "hidden"`), a key an
441
+ // interceptor adds, the parent of an index route at the same URL
442
+ // (#3040). Such a link points at this page, so the page is
443
+ // re-committed with the new fragment: the route, params and query the
444
+ // router shows. The link's own values could commit a state the URL
445
+ // does not show — `{ tags: [] }` behind `/products` — and run guards
446
+ // on a fragment click: an empty value re-enters the route, the parent
447
+ // of an index route leaves its child.
448
+ //
449
+ // ⚠ A 404 page is not re-committed: `navigate` cannot target
450
+ // `UNKNOWN_ROUTE`, so a link that prints its URL stays refused as
451
+ // `SAME_STATES`.
452
+ addBypass(opts);
453
+ return router.navigate(current.name, current.params, current.search, opts);
454
+ }
385
455
  }
386
456
  }
387
457
  // Query channel at position 3 (RFC-4 M2 / #1548); opts at position 4.
package/dist/types.d.ts CHANGED
@@ -49,9 +49,10 @@ export interface LinkProps<P extends Params = Params> {
49
49
  * URL fragment (decoded, no leading "#") — #532.
50
50
  * - `undefined` → preserve current `state.context.url.hash` on click.
51
51
  * - `""` → clear the hash.
52
- * - `"value"` → set the hash; same-route different-hash clicks route through
53
- * `navigateWithHash`, which adds `force: true, hashChange: true` to
54
- * bypass core's SAME_STATES check.
52
+ * - `"value"` → set the hash; clicks route through `navigateWithHash`, which
53
+ * adds `force: true, hashChange: true` when the link points at the page the
54
+ * router shows and asks for another fragment (bypasses core's SAME_STATES
55
+ * check — the Hash wiki page has the details).
55
56
  */
56
57
  readonly hash?: string;
57
58
  readonly target?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real-router/svelte",
3
- "version": "0.24.8",
3
+ "version": "0.25.0",
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.17.0"
55
55
  },
56
56
  "devDependencies": {
57
- "@real-router/browser-plugin": "^0.28.0",
58
- "@real-router/core": "^0.160.0",
57
+ "@real-router/browser-plugin": "^0.28.1",
58
+ "@real-router/core": "^0.172.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.160.0",
69
+ "@real-router/core": ">=0.172.0",
70
70
  "svelte": ">=5.7.0"
71
71
  },
72
72
  "scripts": {