@real-router/svelte 0.15.11 → 0.15.12

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.
@@ -16,6 +16,13 @@ const NOOP_INSTANCE = Object.freeze({
16
16
  // frontends — the same multi-provider scenario `scroll-restore`'s `storageKey`
17
17
  // exists for) keep writing to the now-orphaned node → silent screen reader (#783).
18
18
  let announcerRefCount = 0;
19
+ // Generation token (#1217): bumped each time a FRESH shared element is created.
20
+ // Each instance captures the generation live at construction; on destroy it
21
+ // touches the shared refcount / element ONLY if its generation is still current.
22
+ // A stale instance — whose element a host wiped without calling destroy() — must
23
+ // not decrement the new generation's refcount (→ negative) or remove its live
24
+ // element (removeAnnouncer used to query by selector = whoever is in the DOM).
25
+ let announcerGeneration = 0;
19
26
  export function createRouteAnnouncer(router, options) {
20
27
  // Defensive SSR / non-browser guard: in SSR (Node.js) or non-DOM
21
28
  // environments, `document` is undefined and the announcer cannot
@@ -37,7 +44,7 @@ export function createRouteAnnouncer(router, options) {
37
44
  let lastAnnouncedText = "";
38
45
  let pendingText = null;
39
46
  let clearTimeoutId;
40
- const announcer = getOrCreateAnnouncer();
47
+ const { element: announcer, generation: myGeneration } = getOrCreateAnnouncer();
41
48
  announcerRefCount += 1;
42
49
  const doAnnounce = (text, h1) => {
43
50
  lastAnnouncedText = text;
@@ -103,10 +110,19 @@ export function createRouteAnnouncer(router, options) {
103
110
  unsubscribe();
104
111
  clearTimeout(clearTimeoutId);
105
112
  clearTimeout(safariTimeoutId);
113
+ // Ownership guard (#1217): if a host wiped the shared element out from
114
+ // under us, the next getOrCreateAnnouncer bumped the generation for the
115
+ // fresh element. A stale instance must NOT decrement the new generation's
116
+ // refcount (→ negative) or remove its live element — bail if not current.
117
+ if (myGeneration !== announcerGeneration) {
118
+ return;
119
+ }
106
120
  announcerRefCount -= 1;
107
- // Only the last holder tears down the shared element.
121
+ // Only the last holder tears down the shared element — via our captured
122
+ // ref, not a selector query (which would delete whoever's element is
123
+ // currently in the DOM, i.e. a newer generation's).
108
124
  if (announcerRefCount === 0) {
109
- removeAnnouncer();
125
+ removeAnnouncer(announcer);
110
126
  }
111
127
  },
112
128
  };
@@ -114,7 +130,7 @@ export function createRouteAnnouncer(router, options) {
114
130
  function getOrCreateAnnouncer() {
115
131
  const existing = document.querySelector(`[${ANNOUNCER_ATTR}]`);
116
132
  if (existing) {
117
- return existing;
133
+ return { element: existing, generation: announcerGeneration };
118
134
  }
119
135
  // Creating a FRESH element means no live instance is validly sharing one, so
120
136
  // the ref-count restarts from zero (the caller increments immediately after).
@@ -122,7 +138,10 @@ function getOrCreateAnnouncer() {
122
138
  // wiping the subtree, or a consumer test whose teardown clears the DOM
123
139
  // without calling every instance's destroy() — would leave a stale positive
124
140
  // count that prevents the new element from ever being torn down (#783).
141
+ // The generation bump (#1217) lets the wiped element's instances recognize
142
+ // themselves as stale so their destroy() does not touch this fresh element.
125
143
  announcerRefCount = 0;
144
+ announcerGeneration += 1;
126
145
  const element = document.createElement("div");
127
146
  element.setAttribute("style", VISUALLY_HIDDEN);
128
147
  element.setAttribute("aria-live", "assertive");
@@ -141,10 +160,10 @@ function getOrCreateAnnouncer() {
141
160
  // runtime can return null per spec. The `as` cast narrows the type to
142
161
  // include null so the `??` short-circuit is type-safe.
143
162
  (document.body ?? document.documentElement).prepend(element);
144
- return element;
163
+ return { element, generation: announcerGeneration };
145
164
  }
146
- function removeAnnouncer() {
147
- document.querySelector(`[${ANNOUNCER_ATTR}]`)?.remove();
165
+ function removeAnnouncer(element) {
166
+ element.remove();
148
167
  }
149
168
  function resolveText(route, prefix, getCustomText, h1) {
150
169
  if (getCustomText) {
@@ -291,6 +291,16 @@ const createObserverPair = (selector, rootMargin, getContainer, onIntersection,
291
291
  mo.observe(observerContainer ?? document.body, MUTATION_OBSERVE_INIT);
292
292
  return {
293
293
  pending,
294
+ // #1216: the MutationObserver is pointed at the container's OWN subtree, so
295
+ // the container's removal (a mutation of its PARENT) is invisible — reconcile
296
+ // never fires on it and a remounted container is never re-observed. Expose a
297
+ // detach check + reconcile so the router.subscribe callback can re-resolve on
298
+ // navigation (exactly when route-tied containers mount/die). When
299
+ // `observerContainer` is null the MO already watches `document.body`, which
300
+ // sees container mounts directly — so only a resolved-then-detached container
301
+ // needs this nav-time nudge.
302
+ isContainerDetached: () => observerContainer !== null && !observerContainer.isConnected,
303
+ reconcile,
294
304
  destroy() {
295
305
  io.disconnect();
296
306
  mo.disconnect();
@@ -440,6 +450,13 @@ export function createScrollSpy(router, options) {
440
450
  if (selfEmitting) {
441
451
  return;
442
452
  }
453
+ // #1216: a route-tied scroll container may have unmounted since the last
454
+ // navigation. The container-scoped MutationObserver can't observe its own
455
+ // removal (a mutation of its parent), so re-resolve + re-observe here —
456
+ // navigation is exactly when such containers mount / die.
457
+ if (observers.isContainerDetached()) {
458
+ observers.reconcile();
459
+ }
443
460
  if (getUrlContext(route)?.hashChanged) {
444
461
  cooldown.start();
445
462
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real-router/svelte",
3
- "version": "0.15.11",
3
+ "version": "0.15.12",
4
4
  "type": "module",
5
5
  "description": "Svelte 5 integration for Real-Router",
6
6
  "svelte": "./dist/index.js",
@@ -48,22 +48,22 @@
48
48
  "license": "MIT",
49
49
  "sideEffects": false,
50
50
  "dependencies": {
51
+ "@real-router/core": "^0.74.3",
51
52
  "@real-router/route-utils": "^0.2.7",
52
- "@real-router/sources": "^0.10.13",
53
- "@real-router/core": "^0.74.0"
53
+ "@real-router/sources": "^0.10.15"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@sveltejs/package": "2.5.8",
57
57
  "@sveltejs/vite-plugin-svelte": "6.2.4",
58
- "@testing-library/jest-dom": "6.9.1",
59
- "@testing-library/svelte": "5.4.2",
60
- "@testing-library/user-event": "14.6.1",
58
+ "@testing-library/jest-dom": "~6.9.1",
59
+ "@testing-library/svelte": "~5.4.2",
60
+ "@testing-library/user-event": "~14.6.1",
61
61
  "eslint-plugin-svelte": "3.19.0",
62
62
  "rimraf": "6.1.3",
63
- "svelte": "5.56.4",
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.18.13"
66
+ "@real-router/browser-plugin": "^0.18.16"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "svelte": ">=5.7.0"