@ecopages/browser-router 0.2.0-alpha.1 → 0.2.0-alpha.11

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +6 -6
  2. package/README.md +59 -36
  3. package/package.json +4 -2
  4. package/src/client/document-element-sync.d.ts +24 -0
  5. package/src/client/document-element-sync.js +20 -0
  6. package/src/client/eco-router.d.ts +35 -1
  7. package/src/client/eco-router.js +337 -68
  8. package/src/client/services/dom-swapper.d.ts +101 -0
  9. package/src/client/services/dom-swapper.js +299 -39
  10. package/src/client/services/prefetch-manager.d.ts +1 -0
  11. package/src/client/services/prefetch-manager.js +5 -0
  12. package/src/client/services/view-transition-manager.d.ts +7 -1
  13. package/src/client/services/view-transition-manager.js +21 -5
  14. package/src/client/types.d.ts +12 -0
  15. package/src/client/types.js +4 -0
  16. package/src/index.d.ts +2 -1
  17. package/src/index.js +9 -2
  18. package/src/types.d.ts +1 -1
  19. package/src/types.js +2 -1
  20. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Initialization-should-create-router-instance-1.png +0 -0
  21. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Initialization-should-start-and-stop-without-errors-1.png +0 -0
  22. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Lifecycle-Events-should-dispatch-eco-before-swap--eco-after-swap--and-eco-page-load-events-1.png +0 -0
  23. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Lifecycle-Events-should-dispatch-eco-page-load-event-after-animation-frame-1.png +0 -0
  24. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Lifecycle-Events-should-provide-event-details-with-url-and-direction-1.png +0 -0
  25. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Custom-Link-Selector-should-work-with-data-attribute-selector-1.png +0 -0
  26. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Custom-Reload-Attribute-should-intercept-links-with-default-reload-attribute-when-custom-is-set-1.png +0 -0
  27. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Custom-link-selector-should-only-intercept-links-matching-custom-selector-1.png +0 -0
  28. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-External-Links--should-NOT-intercept--should-NOT-intercept-external-links-1.png +0 -0
  29. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Internal-Links-should-intercept-clicks-on-relative-path-links-1.png +0 -0
  30. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Internal-Links-should-intercept-clicks-on-same-origin-absolute-URLs-1.png +0 -0
  31. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Link-Attributes--should-NOT-intercept--should-NOT-intercept-links-with-download-attribute-1.png +0 -0
  32. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Link-Attributes--should-NOT-intercept--should-intercept-links-with-target---self--1.png +0 -0
  33. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-alt-click-1.png +0 -0
  34. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-ctrl-click-1.png +0 -0
  35. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-meta-click--cmd-on-Mac--1.png +0 -0
  36. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-middle-mouse-button-click-1.png +0 -0
  37. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-right-mouse-button-click-1.png +0 -0
  38. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Modifier-keys-should-NOT-intercept-shift-click-1.png +0 -0
  39. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-external-links--different-origin--1.png +0 -0
  40. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-hash-only-links-1.png +0 -0
  41. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-javascript--links-1.png +0 -0
  42. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-custom-reload-attribute-1.png +0 -0
  43. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-data-eco-reload-attribute-1.png +0 -0
  44. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-download-attribute-1.png +0 -0
  45. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-empty-href-1.png +0 -0
  46. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-target---blank--1.png +0 -0
  47. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-with-target---parent--1.png +0 -0
  48. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-NOT-intercept-links-without-href-attribute-1.png +0 -0
  49. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-intercept-internal-links-with-absolute-same-origin-paths-1.png +0 -0
  50. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-intercept-internal-links-with-relative-paths-1.png +0 -0
  51. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-Should-intercept-nested-elements-inside-links-1.png +0 -0
  52. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Link-Interception-should-NOT-intercept-external-links-1.png +0 -0
  53. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Navigation-Abort-should-abort-previous-navigation-when-new-one-starts-1.png +0 -0
  54. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Programmatic-Navigation-should-navigate-and-update-history-with-pushState-1.png +0 -0
  55. package/src/client/__screenshots__/eco-router.test.browser.ts/EcoRouter-Programmatic-Navigation-should-use-replaceState-when-replace-option-is-true-1.png +0 -0
  56. package/src/client/__screenshots__/eco-router.test.ts/EcoRouter-Error-Handling-should-fall-back-to-full-page-navigation-on-fetch-error-1.png +0 -0
  57. package/src/client/__screenshots__/eco-router.test.ts/EcoRouter-Error-Handling-should-log-error-and-attempt-fallback-navigation-on-fetch-error-1.png +0 -0
  58. package/src/client/eco-router.ts +0 -290
  59. package/src/client/services/dom-swapper.ts +0 -325
  60. package/src/client/services/index.ts +0 -9
  61. package/src/client/services/prefetch-manager.ts +0 -451
  62. package/src/client/services/scroll-manager.ts +0 -48
  63. package/src/client/services/view-transition-manager.ts +0 -75
  64. package/src/client/types.ts +0 -109
  65. package/src/client/view-transition-utils.ts +0 -98
  66. package/src/index.ts +0 -19
  67. package/src/types.ts +0 -19
@@ -1,5 +1,6 @@
1
1
  import morphdom from "morphdom";
2
2
  const DEFAULT_PERSIST_ATTR = "data-eco-persist";
3
+ const RERUN_SRC_ATTR = "data-eco-rerun-src";
3
4
  function isPersisted(element, persistAttribute) {
4
5
  return element.hasAttribute(persistAttribute) || element.hasAttribute(DEFAULT_PERSIST_ATTR);
5
6
  }
@@ -8,6 +9,9 @@ function isHydratedCustomElement(element) {
8
9
  }
9
10
  class DomSwapper {
10
11
  persistAttribute;
12
+ pendingHeadScripts = [];
13
+ pendingRerunScripts = [];
14
+ rerunNonce = 0;
11
15
  constructor(persistAttribute) {
12
16
  this.persistAttribute = persistAttribute;
13
17
  }
@@ -78,6 +82,9 @@ class DomSwapper {
78
82
  * - Injects new scripts from the incoming page that are absent from the current head
79
83
  */
80
84
  morphHead(newDocument) {
85
+ this.pendingHeadScripts = [];
86
+ this.pendingRerunScripts = this.collectRerunScripts(newDocument);
87
+ this.removeStaleHeadScripts(newDocument);
81
88
  const newTitle = newDocument.head.querySelector("title");
82
89
  if (newTitle && document.title !== newTitle.textContent) {
83
90
  document.title = newTitle.textContent || "";
@@ -97,25 +104,6 @@ class DomSwapper {
97
104
  document.head.appendChild(newMeta.cloneNode(true));
98
105
  }
99
106
  }
100
- const existingScriptIds = new Set(
101
- Array.from(document.head.querySelectorAll("script[data-eco-script-id]")).map(
102
- (s) => s.getAttribute("data-eco-script-id")
103
- )
104
- );
105
- const rerunScripts = newDocument.head.querySelectorAll("script[data-eco-rerun]");
106
- for (const script of rerunScripts) {
107
- const scriptId = script.getAttribute("data-eco-script-id");
108
- if (scriptId && !existingScriptIds.has(scriptId)) {
109
- const newScript = document.createElement("script");
110
- for (const attr of script.attributes) {
111
- if (attr.name !== "data-eco-rerun") {
112
- newScript.setAttribute(attr.name, attr.value);
113
- }
114
- }
115
- newScript.textContent = script.textContent;
116
- document.head.appendChild(newScript);
117
- }
118
- }
119
107
  const existingScriptSrcs = new Set(
120
108
  Array.from(document.head.querySelectorAll("script[src]")).map((s) => s.getAttribute("src"))
121
109
  );
@@ -126,27 +114,102 @@ class DomSwapper {
126
114
  for (const script of allNewHeadScripts) {
127
115
  if (script.hasAttribute("data-eco-rerun")) continue;
128
116
  const src = script.getAttribute("src");
117
+ const scriptId = script.getAttribute("data-eco-script-id") || script.getAttribute("id");
118
+ const existingScript = this.findExistingHeadScript(script);
119
+ if (scriptId && existingScript) {
120
+ if (!this.isNonExecutableHeadScript(script)) {
121
+ continue;
122
+ }
123
+ if (this.areHeadScriptsEquivalent(script, existingScript)) {
124
+ continue;
125
+ }
126
+ this.pendingHeadScripts.push({
127
+ attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
128
+ textContent: script.textContent ?? "",
129
+ src,
130
+ scriptId,
131
+ replaceExisting: true
132
+ });
133
+ continue;
134
+ }
129
135
  if (src) {
130
136
  if (existingScriptSrcs.has(src)) continue;
131
- const newScript = document.createElement("script");
132
- for (const attr of script.attributes) {
133
- newScript.setAttribute(attr.name, attr.value);
134
- }
135
- document.head.appendChild(newScript);
137
+ this.pendingHeadScripts.push({
138
+ attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
139
+ textContent: script.textContent ?? "",
140
+ src,
141
+ scriptId,
142
+ replaceExisting: false
143
+ });
136
144
  existingScriptSrcs.add(src);
137
145
  } else {
138
146
  const content = (script.textContent ?? "").trim();
139
147
  if (!content || existingInlineContents.has(content)) continue;
140
- const newScript = document.createElement("script");
141
- for (const attr of script.attributes) {
142
- newScript.setAttribute(attr.name, attr.value);
143
- }
144
- newScript.textContent = script.textContent;
145
- document.head.appendChild(newScript);
148
+ this.pendingHeadScripts.push({
149
+ attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
150
+ textContent: script.textContent ?? "",
151
+ src: null,
152
+ scriptId,
153
+ replaceExisting: false
154
+ });
146
155
  existingInlineContents.add(content);
147
156
  }
148
157
  }
149
158
  }
159
+ /**
160
+ * Replays queued `data-eco-rerun` scripts after the body swap completes.
161
+ *
162
+ * Scripts are intentionally flushed after the new body is in place so DOM-
163
+ * dependent bootstraps bind against the incoming page rather than the page
164
+ * being replaced.
165
+ */
166
+ flushRerunScripts() {
167
+ for (const script of this.pendingHeadScripts) {
168
+ const replacement = document.createElement("script");
169
+ for (const [name, value] of script.attributes) {
170
+ replacement.setAttribute(name, value);
171
+ }
172
+ replacement.textContent = script.textContent;
173
+ const existingScript = this.findExistingHeadScript(script);
174
+ if (script.replaceExisting && existingScript) {
175
+ existingScript.replaceWith(replacement);
176
+ continue;
177
+ }
178
+ document.head.appendChild(replacement);
179
+ }
180
+ this.pendingHeadScripts = [];
181
+ for (const script of this.pendingRerunScripts) {
182
+ const targetParent = script.parent === "body" ? document.body : document.head;
183
+ const replacement = document.createElement("script");
184
+ const shouldBustModuleSrc = this.isExternalModuleRerunScript(script);
185
+ for (const [name, value] of script.attributes) {
186
+ if (name === "data-eco-rerun") {
187
+ continue;
188
+ }
189
+ if (name === "src" && shouldBustModuleSrc) {
190
+ replacement.setAttribute(RERUN_SRC_ATTR, value);
191
+ replacement.setAttribute("src", this.createRerunScriptUrl(value));
192
+ continue;
193
+ }
194
+ replacement.setAttribute(name, value);
195
+ }
196
+ replacement.textContent = script.textContent;
197
+ const existingScript = this.findExistingRerunScript(targetParent, script);
198
+ if (existingScript) {
199
+ existingScript.replaceWith(replacement);
200
+ continue;
201
+ }
202
+ targetParent.appendChild(replacement);
203
+ }
204
+ this.pendingRerunScripts = [];
205
+ }
206
+ /**
207
+ * Returns whether pending rerun scripts require a full body replacement
208
+ * instead of morphing, so DOM-dependent bootstraps bind against fresh markup.
209
+ */
210
+ shouldReplaceBodyForRerunScripts() {
211
+ return this.pendingRerunScripts.length > 0;
212
+ }
150
213
  /**
151
214
  * Detects custom elements without shadow DOM (light-DOM custom elements).
152
215
  * These need full replacement rather than morphing, because morphdom would
@@ -155,6 +218,39 @@ class DomSwapper {
155
218
  isLightDomCustomElement(element) {
156
219
  return element.localName.includes("-") && element.shadowRoot === null;
157
220
  }
221
+ /**
222
+ * Queues a custom element for deferred replacement instead of replacing it
223
+ * inline during the morphdom walk.
224
+ *
225
+ * Replacing elements during morphdom traversal mutates the live DOM tree,
226
+ * which can cause morphdom to skip siblings or process stale nodes.
227
+ * Deferring the replacement until after morphdom finishes avoids this.
228
+ *
229
+ * @returns Always `false` to tell morphdom to skip updating this element.
230
+ */
231
+ replaceCustomElement(fromEl, toEl, deferred) {
232
+ deferred.push({ from: fromEl, to: toEl });
233
+ return false;
234
+ }
235
+ /**
236
+ * Replaces all deferred custom elements after morphdom has finished traversing.
237
+ *
238
+ * Each element is recreated via `document.createElement` so the browser fires
239
+ * the full custom element lifecycle (`disconnectedCallback` on the old instance,
240
+ * `connectedCallback` on the new one). Elements that were already removed during
241
+ * the morph pass are skipped via the `isConnected` guard.
242
+ */
243
+ flushDeferredCustomElementReplacements(deferred) {
244
+ for (const { from, to } of deferred) {
245
+ if (!from.isConnected) continue;
246
+ const newEl = document.createElement(to.tagName);
247
+ for (const attr of to.attributes) {
248
+ newEl.setAttribute(attr.name, attr.value);
249
+ }
250
+ newEl.innerHTML = to.innerHTML;
251
+ from.replaceWith(newEl);
252
+ }
253
+ }
158
254
  /**
159
255
  * Morphs document body using morphdom.
160
256
  * Preserves persisted elements and hydrated custom elements.
@@ -163,22 +259,17 @@ class DomSwapper {
163
259
  */
164
260
  morphBody(newDocument) {
165
261
  const persistAttr = this.persistAttribute;
262
+ const deferredReplacements = [];
166
263
  morphdom(document.body, newDocument.body, {
167
264
  onBeforeElUpdated: (fromEl, toEl) => {
168
265
  if (isPersisted(fromEl, persistAttr)) {
169
266
  return false;
170
267
  }
171
268
  if (isHydratedCustomElement(fromEl)) {
172
- return false;
269
+ return this.replaceCustomElement(fromEl, toEl, deferredReplacements);
173
270
  }
174
271
  if (this.isLightDomCustomElement(fromEl)) {
175
- const newEl = document.createElement(toEl.tagName);
176
- for (const attr of toEl.attributes) {
177
- newEl.setAttribute(attr.name, attr.value);
178
- }
179
- newEl.innerHTML = toEl.innerHTML;
180
- fromEl.replaceWith(newEl);
181
- return false;
272
+ return this.replaceCustomElement(fromEl, toEl, deferredReplacements);
182
273
  }
183
274
  if (fromEl.isEqualNode(toEl)) {
184
275
  return false;
@@ -186,6 +277,7 @@ class DomSwapper {
186
277
  return true;
187
278
  }
188
279
  });
280
+ this.flushDeferredCustomElementReplacements(deferredReplacements);
189
281
  this.processDeclarativeShadowDOM(document.body);
190
282
  }
191
283
  /**
@@ -211,9 +303,177 @@ class DomSwapper {
211
303
  placeholder.replaceWith(oldEl);
212
304
  }
213
305
  }
214
- document.body.replaceChildren(...newDocument.body.childNodes);
306
+ document.body.replaceChildren(...Array.from(newDocument.body.childNodes));
215
307
  this.processDeclarativeShadowDOM(document.body);
216
308
  }
309
+ /**
310
+ * Collects all `data-eco-rerun` scripts from the incoming document.
311
+ *
312
+ * These scripts are re-executed after each navigation so their side-effects
313
+ * (event listeners, DOM bootstraps) bind against the new page content.
314
+ */
315
+ collectRerunScripts(newDocument) {
316
+ return Array.from(newDocument.querySelectorAll("script[data-eco-rerun]")).map((script) => ({
317
+ parent: script.closest("body") ? "body" : "head",
318
+ attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
319
+ textContent: script.textContent ?? "",
320
+ src: script.getAttribute("src"),
321
+ scriptId: script.getAttribute("data-eco-script-id")
322
+ }));
323
+ }
324
+ /**
325
+ * Removes head scripts that are no longer present in the incoming document.
326
+ *
327
+ * Persisted scripts and executable inline scripts with stable identifiers
328
+ * are kept to avoid breaking long-lived runtime state.
329
+ */
330
+ removeStaleHeadScripts(newDocument) {
331
+ const nextScriptKeys = new Set(
332
+ Array.from(newDocument.head.querySelectorAll("script")).map((script) => this.getHeadScriptKey(script)).filter((key) => key !== null)
333
+ );
334
+ for (const script of Array.from(document.head.querySelectorAll("script"))) {
335
+ const key = this.getHeadScriptKey(script);
336
+ if (!key || nextScriptKeys.has(key)) {
337
+ continue;
338
+ }
339
+ if (isPersisted(script, this.persistAttribute)) {
340
+ continue;
341
+ }
342
+ if (this.shouldPersistExecutableInlineHeadScript(script)) {
343
+ continue;
344
+ }
345
+ script.remove();
346
+ }
347
+ }
348
+ /**
349
+ * Determines whether an inline head script should survive navigation.
350
+ *
351
+ * Only identified (`data-eco-script-id` or `id`), executable inline scripts
352
+ * are persisted. External scripts and rerun scripts are never persisted here
353
+ * because they have their own lifecycle management.
354
+ */
355
+ shouldPersistExecutableInlineHeadScript(script) {
356
+ const scriptId = script.getAttribute("data-eco-script-id") || script.getAttribute("id");
357
+ if (!scriptId) {
358
+ return false;
359
+ }
360
+ if (script.hasAttribute("data-eco-rerun")) {
361
+ return false;
362
+ }
363
+ if (script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute("src")) {
364
+ return false;
365
+ }
366
+ return !this.isNonExecutableHeadScript(script);
367
+ }
368
+ /**
369
+ * Returns whether a script is non-executable (e.g. `type="application/json"`).
370
+ *
371
+ * Non-executable scripts are data carriers (JSON-LD, page data) that can be
372
+ * safely replaced without side-effects, unlike executable scripts that would
373
+ * re-run their bootstrap logic.
374
+ */
375
+ isNonExecutableHeadScript(script) {
376
+ const type = (script.getAttribute("type") ?? "").trim().toLowerCase();
377
+ if (!type) {
378
+ return false;
379
+ }
380
+ return ![
381
+ "application/javascript",
382
+ "application/ecmascript",
383
+ "module",
384
+ "text/ecmascript",
385
+ "text/javascript"
386
+ ].includes(type);
387
+ }
388
+ /**
389
+ * Compares two head scripts for structural equality (key, content, attributes).
390
+ *
391
+ * Used to skip replacing non-executable data scripts when their content
392
+ * has not changed between navigations.
393
+ */
394
+ areHeadScriptsEquivalent(nextScript, currentScript) {
395
+ if (this.getHeadScriptKey(nextScript) !== this.getHeadScriptKey(currentScript)) {
396
+ return false;
397
+ }
398
+ if ((nextScript.textContent ?? "") !== (currentScript.textContent ?? "")) {
399
+ return false;
400
+ }
401
+ const nextAttributes = Array.from(nextScript.attributes).map((attribute) => [attribute.name, attribute.value]);
402
+ const currentAttributes = Array.from(currentScript.attributes).map((attribute) => [
403
+ attribute.name,
404
+ attribute.value
405
+ ]);
406
+ if (nextAttributes.length !== currentAttributes.length) {
407
+ return false;
408
+ }
409
+ return nextAttributes.every(
410
+ ([name, value], index) => currentAttributes[index]?.[0] === name && currentAttributes[index]?.[1] === value
411
+ );
412
+ }
413
+ /**
414
+ * Derives a stable identity key for a head script.
415
+ *
416
+ * Priority: `data-eco-script-id` / `id` > `src` > trimmed inline content.
417
+ * Returns `null` for empty anonymous inline scripts that cannot be tracked.
418
+ */
419
+ getHeadScriptKey(script) {
420
+ const scriptId = script instanceof HTMLScriptElement ? script.getAttribute("data-eco-script-id") || script.getAttribute("id") : script.scriptId;
421
+ if (scriptId) {
422
+ return `id:${scriptId}`;
423
+ }
424
+ const src = script instanceof HTMLScriptElement ? script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute("src") : script.src;
425
+ if (src) {
426
+ return `src:${src}`;
427
+ }
428
+ const textContent = (script.textContent ?? "").trim();
429
+ return textContent ? `inline:${textContent}` : null;
430
+ }
431
+ /**
432
+ * Finds an existing head script that matches the given script's identity key.
433
+ */
434
+ findExistingHeadScript(script) {
435
+ const scriptKey = this.getHeadScriptKey(script);
436
+ if (!scriptKey) {
437
+ return null;
438
+ }
439
+ return Array.from(document.head.querySelectorAll("script")).find(
440
+ (candidate) => this.getHeadScriptKey(candidate) === scriptKey
441
+ ) ?? null;
442
+ }
443
+ /**
444
+ * Finds an existing rerun script in the given root by `data-eco-script-id` or
445
+ * by matching `src` and `textContent`.
446
+ */
447
+ findExistingRerunScript(root, script) {
448
+ const scripts = Array.from(root.querySelectorAll("script"));
449
+ if (script.scriptId) {
450
+ return scripts.find((candidate) => candidate.getAttribute("data-eco-script-id") === script.scriptId) ?? null;
451
+ }
452
+ return scripts.find(
453
+ (candidate) => (candidate.getAttribute(RERUN_SRC_ATTR) ?? candidate.getAttribute("src")) === script.src && (candidate.textContent ?? "") === script.textContent
454
+ ) ?? null;
455
+ }
456
+ /**
457
+ * Returns whether a rerun script is an external ES module (`type="module"` with `src`).
458
+ *
459
+ * Module scripts are cached by URL, so re-execution requires cache-busting
460
+ * via a query parameter nonce.
461
+ */
462
+ isExternalModuleRerunScript(script) {
463
+ if (!script.src) {
464
+ return false;
465
+ }
466
+ return script.attributes.some(([name, value]) => name === "type" && value === "module");
467
+ }
468
+ /**
469
+ * Appends a nonce query parameter to a script URL to bust the browser's
470
+ * module cache and force re-execution on navigation.
471
+ */
472
+ createRerunScriptUrl(src) {
473
+ const url = new URL(src, document.baseURI);
474
+ url.searchParams.set("__eco_rerun", String(++this.rerunNonce));
475
+ return url.toString();
476
+ }
217
477
  /**
218
478
  * Manually attaches declarative shadow DOM templates.
219
479
  * Browsers only process `<template shadowrootmode>` during initial parse.
@@ -52,6 +52,7 @@ export declare class PrefetchManager {
52
52
  * @returns The cached HTML string, or null if not cached
53
53
  */
54
54
  getCachedHtml(href: string): string | null;
55
+ invalidate(href: string): void;
55
56
  /**
56
57
  * Caches HTML content for a visited page and triggers background revalidation.
57
58
  *
@@ -89,6 +89,11 @@ class PrefetchManager {
89
89
  const url = new URL(href, window.location.origin);
90
90
  return this.htmlCache.get(url.href) ?? null;
91
91
  }
92
+ invalidate(href) {
93
+ const url = new URL(href, window.location.origin);
94
+ this.htmlCache.delete(url.href);
95
+ this.prefetched.delete(url.href);
96
+ }
92
97
  /**
93
98
  * Caches HTML content for a visited page and triggers background revalidation.
94
99
  *
@@ -16,8 +16,14 @@ export declare class ViewTransitionManager {
16
16
  /**
17
17
  * Execute a callback with view transition if available and enabled.
18
18
  * Falls back to direct execution if not supported.
19
+ *
20
+ * Navigation correctness depends on the DOM update callback completing, not on
21
+ * the browser finishing the visual transition animation. Awaiting
22
+ * `finished` here can leave router transactions artificially in-flight and
23
+ * block later navigations during rapid repeated clicks, so the router waits
24
+ * for `updateCallbackDone` and lets the animation finish in the background.
19
25
  * @param callback - The DOM update callback to execute
20
- * @returns Promise that resolves when the transition completes
26
+ * @returns Promise that resolves when the DOM update has committed
21
27
  */
22
28
  transition(callback: () => void | Promise<void>): Promise<void>;
23
29
  }
@@ -13,8 +13,14 @@ class ViewTransitionManager {
13
13
  /**
14
14
  * Execute a callback with view transition if available and enabled.
15
15
  * Falls back to direct execution if not supported.
16
+ *
17
+ * Navigation correctness depends on the DOM update callback completing, not on
18
+ * the browser finishing the visual transition animation. Awaiting
19
+ * `finished` here can leave router transactions artificially in-flight and
20
+ * block later navigations during rapid repeated clicks, so the router waits
21
+ * for `updateCallbackDone` and lets the animation finish in the background.
16
22
  * @param callback - The DOM update callback to execute
17
- * @returns Promise that resolves when the transition completes
23
+ * @returns Promise that resolves when the DOM update has committed
18
24
  */
19
25
  async transition(callback) {
20
26
  if (!this.enabled || !this.isSupported()) {
@@ -26,11 +32,21 @@ class ViewTransitionManager {
26
32
  await callback();
27
33
  applyViewTransitionNames();
28
34
  });
29
- try {
30
- await transition.finished;
31
- } finally {
35
+ void transition.ready.catch((error) => {
36
+ if (error instanceof Error && error.name === "AbortError") {
37
+ return;
38
+ }
39
+ console.error("[ecopages] View transition failed to start:", error);
40
+ });
41
+ void transition.finished.catch((error) => {
42
+ if (error instanceof Error && error.name === "AbortError") {
43
+ return;
44
+ }
45
+ console.error("[ecopages] View transition lifecycle failed:", error);
46
+ }).finally(() => {
32
47
  clearViewTransitionNames();
33
- }
48
+ });
49
+ await transition.updateCallbackDone;
34
50
  }
35
51
  }
36
52
  export {
@@ -36,6 +36,13 @@ export interface PrefetchConfig {
36
36
  export interface EcoRouterOptions {
37
37
  /** Selector for links to intercept. @default 'a[href]' */
38
38
  linkSelector?: string;
39
+ /**
40
+ * Document-level `<html>` attributes to sync from the incoming document during navigation.
41
+ * Attributes not listed here are preserved on the live document element so client-managed
42
+ * state such as theme classes or data attributes is not clobbered during swaps.
43
+ * @default ['lang', 'dir', 'data-eco-document-owner']
44
+ */
45
+ documentElementAttributesToSync?: string[];
39
46
  /** Attribute to mark elements for DOM persistence. @default 'data-eco-persist' */
40
47
  persistAttribute?: string;
41
48
  /** Attribute to force full page reload. @default 'data-eco-reload' */
@@ -67,6 +74,9 @@ export interface EcoRouterOptions {
67
74
  */
68
75
  prefetch?: PrefetchConfig | false;
69
76
  }
77
+ export type BrowserRouterNavigateOptions = {
78
+ direction?: 'forward' | 'back' | 'replace';
79
+ };
70
80
  /** Events emitted during the navigation lifecycle */
71
81
  export interface EcoNavigationEvent {
72
82
  url: URL;
@@ -80,5 +90,7 @@ export interface EcoBeforeSwapEvent extends EcoNavigationEvent {
80
90
  /** Event fired after the DOM swap completes */
81
91
  export interface EcoAfterSwapEvent extends EcoNavigationEvent {
82
92
  }
93
+ /** Default document-level `<html>` attributes synchronized during navigation swaps. */
94
+ export declare const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC: string[];
83
95
  /** Default configuration options */
84
96
  export declare const DEFAULT_OPTIONS: Required<EcoRouterOptions>;
@@ -1,11 +1,14 @@
1
+ import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from "@ecopages/core/router/navigation-coordinator";
1
2
  const DEFAULT_PREFETCH_CONFIG = {
2
3
  strategy: "intent",
3
4
  delay: 65,
4
5
  noPrefetchAttribute: "data-eco-no-prefetch",
5
6
  respectDataSaver: true
6
7
  };
8
+ const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC = ["lang", "dir", ECO_DOCUMENT_OWNER_ATTRIBUTE];
7
9
  const DEFAULT_OPTIONS = {
8
10
  linkSelector: "a[href]",
11
+ documentElementAttributesToSync: DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
9
12
  persistAttribute: "data-eco-persist",
10
13
  reloadAttribute: "data-eco-reload",
11
14
  updateHistory: true,
@@ -15,5 +18,6 @@ const DEFAULT_OPTIONS = {
15
18
  prefetch: DEFAULT_PREFETCH_CONFIG
16
19
  };
17
20
  export {
21
+ DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
18
22
  DEFAULT_OPTIONS
19
23
  };
package/src/index.d.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * @module
5
5
  */
6
6
  export type { EcoRouterOptions, EcoNavigationEvent, EcoBeforeSwapEvent, EcoAfterSwapEvent, EcoRouterEventMap, } from './types.js';
7
- export { DEFAULT_OPTIONS } from './types.js';
7
+ export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from './types.js';
8
+ export { defaultDocumentElementAttributesToSync, syncDocumentElementAttributes, } from './client/document-element-sync.js';
8
9
  export { EcoRouter, createRouter } from './client/eco-router.js';
9
10
  export { DomSwapper, ScrollManager, ViewTransitionManager } from './client/services/index.js';
package/src/index.js CHANGED
@@ -1,11 +1,18 @@
1
- import { DEFAULT_OPTIONS } from "./types.js";
1
+ import { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from "./types.js";
2
+ import {
3
+ defaultDocumentElementAttributesToSync,
4
+ syncDocumentElementAttributes
5
+ } from "./client/document-element-sync.js";
2
6
  import { EcoRouter, createRouter } from "./client/eco-router.js";
3
7
  import { DomSwapper, ScrollManager, ViewTransitionManager } from "./client/services/index.js";
4
8
  export {
9
+ DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
5
10
  DEFAULT_OPTIONS,
6
11
  DomSwapper,
7
12
  EcoRouter,
8
13
  ScrollManager,
9
14
  ViewTransitionManager,
10
- createRouter
15
+ createRouter,
16
+ defaultDocumentElementAttributesToSync,
17
+ syncDocumentElementAttributes
11
18
  };
package/src/types.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import type { EcoNavigationEvent, EcoBeforeSwapEvent, EcoAfterSwapEvent } from './client/types';
6
6
  export type { EcoRouterOptions, EcoNavigationEvent, EcoBeforeSwapEvent, EcoAfterSwapEvent } from './client/types';
7
- export { DEFAULT_OPTIONS } from './client/types';
7
+ export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from './client/types';
8
8
  /**
9
9
  * Custom event map for navigation lifecycle
10
10
  */
package/src/types.js CHANGED
@@ -1,4 +1,5 @@
1
- import { DEFAULT_OPTIONS } from "./client/types";
1
+ import { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from "./client/types";
2
2
  export {
3
+ DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
3
4
  DEFAULT_OPTIONS
4
5
  };