@real-router/svelte 0.21.0 → 0.22.1

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.
@@ -1,17 +1,17 @@
1
1
  <script lang="ts">
2
2
  import { getNavigator } from "@real-router/core";
3
3
  import { createRouteSource, primeErrorSource } from "@real-router/sources";
4
+ import { setContext, untrack } from "svelte";
5
+
6
+ import { NAVIGATOR_KEY, ROUTE_KEY, ROUTER_KEY } from "./context";
7
+ import { createReactiveSource } from "./createReactiveSource.svelte";
8
+ import { createRouteContext } from "./createRouteContext.svelte";
4
9
  import {
5
10
  createRouteAnnouncer,
6
11
  createScrollRestoration,
7
12
  createScrollSpy,
8
13
  createViewTransitions,
9
14
  } from "./dom-utils";
10
- import { setContext, untrack } from "svelte";
11
-
12
- import { createReactiveSource } from "./createReactiveSource.svelte";
13
- import { createRouteContext } from "./createRouteContext.svelte";
14
- import { NAVIGATOR_KEY, ROUTE_KEY, ROUTER_KEY } from "./context";
15
15
 
16
16
  import type {
17
17
  RouteAnnouncerOptions,
@@ -54,13 +54,19 @@
54
54
  );
55
55
 
56
56
  $effect(() => {
57
- if (!anEnabled) return;
57
+ if (!anEnabled) {
58
+ return;
59
+ }
60
+
58
61
  void anPrefix;
59
62
  const options = untrack(() =>
60
63
  typeof announceNavigation === "object" ? announceNavigation : undefined,
61
64
  );
62
65
  const announcer = createRouteAnnouncer(router, options);
63
- return () => announcer.destroy();
66
+
67
+ return () => {
68
+ announcer.destroy();
69
+ };
64
70
  });
65
71
 
66
72
  // $derived memoizes by === so inline `{ mode: "restore" }` doesn't thrash:
@@ -73,7 +79,10 @@
73
79
  const srStorageKey = $derived(scrollRestoration?.storageKey);
74
80
 
75
81
  $effect(() => {
76
- if (!srEnabled) return;
82
+ if (!srEnabled) {
83
+ return;
84
+ }
85
+
77
86
  // Pin primitive $derived deps as explicit dependencies of this effect
78
87
  // BEFORE constructing the utility. The four `void srX` reads make
79
88
  // intent unambiguous: even if `createScrollRestoration` throws after
@@ -97,7 +106,10 @@
97
106
  storageKey: srStorageKey,
98
107
  scrollContainer: untrack(() => scrollRestoration?.scrollContainer),
99
108
  });
100
- return () => sr.destroy();
109
+
110
+ return () => {
111
+ sr.destroy();
112
+ };
101
113
  });
102
114
 
103
115
  const spyEnabled = $derived(
@@ -107,20 +119,32 @@
107
119
  const spyRootMargin = $derived(scrollSpy?.rootMargin);
108
120
 
109
121
  $effect(() => {
110
- if (!spyEnabled || !spySelector) return;
122
+ if (!spyEnabled || !spySelector) {
123
+ return;
124
+ }
125
+
111
126
  void spyRootMargin;
112
127
  const spy = createScrollSpy(router, {
113
128
  selector: spySelector,
114
129
  rootMargin: spyRootMargin,
115
130
  scrollContainer: untrack(() => scrollSpy?.scrollContainer),
116
131
  });
117
- return () => spy.destroy();
132
+
133
+ return () => {
134
+ spy.destroy();
135
+ };
118
136
  });
119
137
 
120
138
  $effect(() => {
121
- if (!viewTransitions) return;
139
+ if (!viewTransitions) {
140
+ return;
141
+ }
142
+
122
143
  const vt = createViewTransitions(router);
123
- return () => vt.destroy();
144
+
145
+ return () => {
146
+ vt.destroy();
147
+ };
124
148
  });
125
149
 
126
150
  // svelte-ignore state_referenced_locally
@@ -142,7 +166,6 @@
142
166
  // svelte-ignore state_referenced_locally
143
167
  // Context exposes the same stable router instance for this provider.
144
168
  setContext(ROUTER_KEY, router);
145
- // svelte-ignore state_referenced_locally
146
169
  // Context exposes the navigator derived once from the stable router.
147
170
  setContext(NAVIGATOR_KEY, navigator);
148
171
  setContext(ROUTE_KEY, routeContext);
@@ -1,5 +1,5 @@
1
- import type { ActionReturn } from "svelte/action";
2
1
  import type { Params, NavigationOptions } from "@real-router/core";
2
+ import type { ActionReturn } from "svelte/action";
3
3
  export interface LinkActionParams {
4
4
  name: string;
5
5
  params?: Params;
@@ -1,14 +1,14 @@
1
- import { ROUTER_KEY, getContextOrThrow } from "../context";
2
1
  import { EMPTY_OPTIONS, EMPTY_PARAMS, NOOP } from "../constants";
2
+ import { ROUTER_KEY, getContextOrThrow } from "../context";
3
3
  import { shouldNavigate, anchorTargetsAnotherContext, applyLinkA11y, } from "../dom-utils";
4
4
  const delegationByRouter = new WeakMap();
5
5
  function findRegisteredNode(nodes, target) {
6
- let el = target instanceof HTMLElement ? target : null;
7
- while (el) {
8
- if (nodes.has(el)) {
9
- return el;
6
+ let element = target instanceof HTMLElement ? target : null;
7
+ while (element) {
8
+ if (nodes.has(element)) {
9
+ return element;
10
10
  }
11
- el = el.parentElement;
11
+ element = element.parentElement;
12
12
  }
13
13
  return undefined;
14
14
  }
@@ -41,6 +41,7 @@ function getDelegation(router) {
41
41
  return;
42
42
  }
43
43
  evt.preventDefault();
44
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- findRegisteredNode returns only a node the map holds
44
45
  navigate(nodes.get(node));
45
46
  }
46
47
  function handleKeyDown(evt) {
@@ -59,6 +60,7 @@ function getDelegation(router) {
59
60
  if (anchorTargetsAnotherContext(node)) {
60
61
  return;
61
62
  }
63
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- findRegisteredNode returns only a node the map holds
62
64
  navigate(nodes.get(node));
63
65
  }
64
66
  const state = {
@@ -36,6 +36,7 @@
36
36
  // `useDeferred(name)` reads `state.context.ssrDataDeferred[name]` —
37
37
  // wrap in `$derived` so a dynamic `name` prop re-resolves the promise
38
38
  // (vs. capturing the initial value at component init).
39
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments -- `T` is this component's generic; without it the promise is `Promise<unknown>` and svelte-check fails
39
40
  const promise = $derived(useDeferred<T>(name));
40
41
  </script>
41
42
 
@@ -8,6 +8,7 @@
8
8
 
9
9
  let { children, fallback }: Props = $props();
10
10
 
11
+ // eslint-disable-next-line svelte/prefer-writable-derived -- `$effect` runs only in the browser after mount; a `$derived` would be true on the server too
11
12
  let mounted = $state(false);
12
13
 
13
14
  $effect(() => {
@@ -54,6 +54,7 @@
54
54
  const sink = getContext<HttpStatusSink | undefined>(HTTP_STATUS_KEY);
55
55
 
56
56
  if (sink) {
57
+ // eslint-disable-next-line svelte/no-unused-svelte-ignore -- svelte-check reports `state_referenced_locally` on `code` without it
57
58
  // svelte-ignore state_referenced_locally
58
59
  // Intentional one-time read at component init: the sink is read by the
59
60
  // server after `await render()` and a single value is the contract.
@@ -28,27 +28,36 @@
28
28
  // invoking the loader synchronously on mount (throw-isolation class, #806).
29
29
  let pending: Promise<{ default: Component }>;
30
30
 
31
+ // eslint-disable-next-line sonarjs/no-try-promise -- the try catches a loader that throws before it returns a promise, as the comment above says
31
32
  try {
32
33
  pending = loader();
33
- } catch (err) {
34
- pending = Promise.reject(err);
34
+ } catch (error) {
35
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors -- the `.catch` below wraps a non-Error reason in an Error
36
+ pending = Promise.reject(error);
35
37
  }
36
38
 
37
39
  pending
38
40
  .then((module) => {
39
- if (!active) return;
40
- if (!module || typeof module.default === "undefined") {
41
+ if (!active) {
42
+ return;
43
+ }
44
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- a loader outside its type can resolve to anything
45
+ if (typeof module?.default === "undefined") {
41
46
  throw new Error(
42
47
  "[real-router] Lazy loader resolved without a `default` export.",
43
48
  );
44
49
  }
50
+
45
51
  state = { status: "ready", component: module.default };
46
52
  })
47
- .catch((err: unknown) => {
48
- if (!active) return;
53
+ .catch((error: unknown) => {
54
+ if (!active) {
55
+ return;
56
+ }
57
+
49
58
  state = {
50
59
  status: "error",
51
- error: err instanceof Error ? err : new Error(String(err)),
60
+ error: error instanceof Error ? error : new Error(String(error)),
52
61
  };
53
62
  });
54
63
 
@@ -25,16 +25,17 @@
25
25
  routeSearch,
26
26
  to,
27
27
  routeOptions = EMPTY_OPTIONS,
28
- class: className = undefined,
28
+ class: className,
29
29
  activeClassName = "active",
30
30
  activeStrict = false,
31
31
  ignoreQueryParams = true,
32
- hash = undefined,
33
- target = undefined,
34
- children = undefined,
35
- onclick: userOnClick = undefined,
32
+ hash,
33
+ target,
34
+ children,
35
+ onclick: userOnClick,
36
36
  ...restProps
37
37
  }: {
38
+ [key: string]: unknown;
38
39
  routeName?: string;
39
40
  routeParams?: Params;
40
41
  routeSearch?: SearchParams;
@@ -54,7 +55,6 @@
54
55
  target?: string;
55
56
  children?: Snippet;
56
57
  onclick?: (evt: MouseEvent) => void;
57
- [key: string]: unknown;
58
58
  } = $props();
59
59
 
60
60
  const router = useRouter();
@@ -1,6 +1,7 @@
1
1
  import type { NavigationOptions, NavigationTarget, Params, SearchParams } from "@real-router/core";
2
2
  import type { Snippet } from "svelte";
3
3
  type $$ComponentProps = {
4
+ [key: string]: unknown;
4
5
  routeName?: string;
5
6
  routeParams?: Params;
6
7
  routeSearch?: SearchParams;
@@ -20,7 +21,6 @@ type $$ComponentProps = {
20
21
  target?: string;
21
22
  children?: Snippet;
22
23
  onclick?: (evt: MouseEvent) => void;
23
- [key: string]: unknown;
24
24
  };
25
25
  declare const Link: import("svelte").Component<$$ComponentProps, {}, "">;
26
26
  type Link = ReturnType<typeof Link>;
@@ -1,8 +1,8 @@
1
1
  <script lang="ts">
2
2
  import { UNKNOWN_ROUTE } from "@real-router/core";
3
3
 
4
- import { useRouteNode } from "../composables/useRouteNode.svelte";
5
4
  import { getActiveSegment } from "./RouteView.helpers";
5
+ import { useRouteNode } from "../composables/useRouteNode.svelte";
6
6
 
7
7
  import type { Snippet } from "svelte";
8
8
 
@@ -12,10 +12,10 @@
12
12
  notFound,
13
13
  ...segmentSnippets
14
14
  }: {
15
+ [key: string]: Snippet | string | undefined;
15
16
  nodeName: string;
16
17
  self?: Snippet;
17
18
  notFound?: Snippet;
18
- [key: string]: Snippet | string | undefined;
19
19
  } = $props();
20
20
 
21
21
  // svelte-ignore state_referenced_locally
@@ -1,9 +1,9 @@
1
1
  import type { Snippet } from "svelte";
2
2
  type $$ComponentProps = {
3
+ [key: string]: Snippet | string | undefined;
3
4
  nodeName: string;
4
5
  self?: Snippet;
5
6
  notFound?: Snippet;
6
- [key: string]: Snippet | string | undefined;
7
7
  };
8
8
  declare const RouteView: import("svelte").Component<$$ComponentProps, {}, "">;
9
9
  type RouteView = ReturnType<typeof RouteView>;
@@ -25,9 +25,15 @@
25
25
 
26
26
  $effect(() => {
27
27
  const snap = snapshot.current;
28
- if (!snap.error) return;
28
+
29
+ if (!snap.error) {
30
+
31
+ return;
32
+
33
+ }
29
34
 
30
35
  const { error, toRoute, fromRoute } = snap;
36
+
31
37
  untrack(() => {
32
38
  try {
33
39
  onError?.(error, toRoute, fromRoute);
@@ -41,6 +47,7 @@
41
47
  });
42
48
  </script>
43
49
 
50
+ <!-- eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- `children` is typed as required; a consumer outside the type renders nothing rather than throwing -->
44
51
  {@render children?.()}
45
52
  {#if snapshot.current.error}
46
53
  {@render fallback(snapshot.current.error, snapshot.current.resetError)}
@@ -8,6 +8,7 @@
8
8
 
9
9
  let { children, fallback }: Props = $props();
10
10
 
11
+ // eslint-disable-next-line svelte/prefer-writable-derived -- `$effect` runs only in the browser after mount; a `$derived` would be true on the server too
11
12
  let mounted = $state(false);
12
13
 
13
14
  $effect(() => {
@@ -1,5 +1,5 @@
1
- import type { Navigator, State } from "@real-router/core";
2
1
  import type { RouteContext } from "./types";
2
+ import type { Navigator, State } from "@real-router/core";
3
3
  export interface RouteSnapshot {
4
4
  readonly route: State | undefined;
5
5
  readonly previousRoute: State | undefined;
@@ -14,6 +14,57 @@ import { putField } from "@real-router/core/utils";
14
14
  * (#1798), which is the doctrine's own caveat and travels with it.
15
15
  */
16
16
  const objectKeys = Object.keys;
17
+ /**
18
+ * The brand `packages/core/src/internals.ts` puts on every router it registers,
19
+ * RE-DECLARED rather than imported (#2294).
20
+ *
21
+ * ⚑ `Symbol.for` makes the two the same symbol, so **the STRING is the
22
+ * contract** — change it in one place and this warning silently stops firing.
23
+ * The same shape core already uses for `CONFIG_FAULT`, declared twice across a
24
+ * boundary it cannot import over, and pinned the same way: the guard is
25
+ * `foreign-router-href-2294.test.ts`, whose cells fail on exactly that drift.
26
+ */
27
+ const ROUTER_BRAND = Symbol.for("real-router.router");
28
+ /**
29
+ * Is this a real router that core cannot read — as opposed to the `Router`-SHAPED
30
+ * double this helper accepts by contract?
31
+ *
32
+ * ⚠ Read defensively: the argument is the caller's object and may be a `Proxy`
33
+ * whose `get` trap throws. A diagnostic that throws would change where the error
34
+ * comes FROM, which is the #1572 class, so a throwing read means "cannot tell".
35
+ */
36
+ function isUnreadableRouter(candidate) {
37
+ try {
38
+ return (candidate?.[ROUTER_BRAND] === true);
39
+ }
40
+ catch {
41
+ return false;
42
+ }
43
+ }
44
+ /**
45
+ * The registry lookup, ALONE — and standing alone is what makes the warning
46
+ * correct (#2294).
47
+ *
48
+ * ⚑ Inside the resolve-and-print `try` this could not be told apart from a
49
+ * `forwardState` that threw for a reason the arm handles deliberately. The
50
+ * channel guard (#1572) is exactly that, and it fires on a router that IS
51
+ * registered and IS branded — so a check made there would report duplicated core
52
+ * on a perfectly healthy one.
53
+ */
54
+ function readPluginApi(router, routeName) {
55
+ try {
56
+ return getPluginApi(router);
57
+ }
58
+ catch {
59
+ if (isUnreadableRouter(router)) {
60
+ console.error(`[real-router] Route "${routeName}" rendered its LITERAL path: this IS a router, ` +
61
+ "but not one this copy of @real-router/core built, so `forwardTo` was not resolved. " +
62
+ "It is either wrapped in a Proxy (Vue `reactive()` / Pinia — store it with `markRaw`), " +
63
+ "or your dependency tree holds two copies of @real-router/core — dedupe it to one.");
64
+ }
65
+ return undefined;
66
+ }
67
+ }
17
68
  /**
18
69
  * Collapses a `<Link>`'s two prop forms (RFC-4 M2 B2, #1548) into one channel
19
70
  * triple:
@@ -192,6 +243,7 @@ export function buildHref(router, routeName, routeParams, routeSearch, hash) {
192
243
  // `packages/react/tests/functional/dom-utils/forwarding-link-href-2250.test.ts`
193
244
  // owns that.
194
245
  let resolved;
246
+ const api = readPluginApi(router, routeName);
195
247
  try {
196
248
  // ⚠ **`forwardState`, not `buildNavigationState`.** Both resolve the whole
197
249
  // chain, and the href is identical — but the committing door opts into
@@ -209,9 +261,16 @@ export function buildHref(router, routeName, routeParams, routeSearch, hash) {
209
261
  // two invocations per href — a stateful one double-counts. Cost is the
210
262
  // lesser half: +1333 ns per href with `search-schema` +
211
263
  // `persistent-params` installed, per `<Link>` per render.
212
- const api = getPluginApi(router);
213
- const forwarded = api.forwardState(routeName, routeParams, routeSearch);
214
- resolved = api.buildPathResolved(forwarded.name, forwarded.params, forwarded.search);
264
+ //
265
+ // ⚠ `api?.` is a TYPE gate, not a runtime guard, and naming it so keeps it
266
+ // from being read as one: calling `forwardState` on `undefined` throws
267
+ // into the same `catch` for the same outcome, so a mutant dropping the
268
+ // check is EQUIVALENT. What it buys is that TypeScript can see the call is
269
+ // safe below a lookup that may have failed (#2294).
270
+ const forwarded = api?.forwardState(routeName, routeParams, routeSearch);
271
+ resolved =
272
+ forwarded &&
273
+ api?.buildPathResolved(forwarded.name, forwarded.params, forwarded.search);
215
274
  }
216
275
  catch {
217
276
  resolved = undefined;
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "name": "@real-router/svelte",
3
- "version": "0.21.0",
3
+ "version": "0.22.1",
4
4
  "type": "module",
5
5
  "description": "Svelte 5 integration for Real-Router",
6
6
  "svelte": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
+ "@real-router/internal-source": "./src/index.ts",
10
11
  "types": "./dist/index.d.ts",
11
12
  "svelte": "./dist/index.js",
12
13
  "default": "./dist/index.js"
13
14
  },
14
15
  "./ssr": {
16
+ "@real-router/internal-source": "./src/ssr.ts",
15
17
  "types": "./dist/ssr.d.ts",
16
18
  "svelte": "./dist/ssr.js",
17
19
  "default": "./dist/ssr.js"
@@ -48,24 +50,25 @@
48
50
  "license": "MIT",
49
51
  "sideEffects": false,
50
52
  "dependencies": {
51
- "@real-router/core": "^0.134.0",
52
53
  "@real-router/route-utils": "^0.4.0",
53
- "@real-router/sources": "^0.14.41"
54
+ "@real-router/sources": "^0.15.0"
54
55
  },
55
56
  "devDependencies": {
56
- "@real-router/browser-plugin": "^0.24.0",
57
+ "@real-router/browser-plugin": "^0.25.0",
58
+ "@real-router/core": "^0.147.8",
57
59
  "@sveltejs/package": "2.5.8",
58
60
  "@sveltejs/vite-plugin-svelte": "7.3.0",
59
- "@testing-library/jest-dom": "~6.9.1",
61
+ "@testing-library/jest-dom": "~7.0.1",
60
62
  "@testing-library/svelte": "~5.4.2",
61
63
  "@testing-library/user-event": "~14.6.1",
62
- "eslint-plugin-svelte": "3.19.0",
64
+ "eslint-plugin-svelte": "3.23.0",
63
65
  "rimraf": "6.1.3",
64
66
  "svelte": "~5.57.0",
65
67
  "svelte-check": "4.7.6",
66
- "svelte-eslint-parser": "1.8.0"
68
+ "svelte-eslint-parser": "1.8.1"
67
69
  },
68
70
  "peerDependencies": {
71
+ "@real-router/core": ">=0.147.0",
69
72
  "svelte": ">=5.7.0"
70
73
  },
71
74
  "scripts": {