@ecopages/browser-router 0.2.0-beta.3 → 0.2.0-beta.30
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 +8 -2
- package/package.json +7 -7
- package/src/client/document-element-sync.d.ts +0 -7
- package/src/client/document-element-sync.js +0 -3
- package/src/client/dom/body-morpher.d.ts +24 -0
- package/src/client/dom/body-morpher.js +103 -0
- package/src/client/dom/dom-swapper.d.ts +30 -0
- package/src/client/dom/dom-swapper.js +51 -0
- package/src/client/dom/head-updater.d.ts +42 -0
- package/src/client/dom/head-updater.js +173 -0
- package/src/client/dom/html-parser.d.ts +4 -0
- package/src/client/dom/html-parser.js +8 -0
- package/src/client/dom/stylesheet-discovery.d.ts +12 -0
- package/src/client/dom/stylesheet-discovery.js +20 -0
- package/src/client/dom/stylesheet-preloader.d.ts +10 -0
- package/src/client/dom/stylesheet-preloader.js +38 -0
- package/src/client/eco-router.d.ts +1 -33
- package/src/client/eco-router.js +61 -211
- package/src/client/index.d.ts +3 -0
- package/src/client/index.js +13 -0
- package/src/client/navigation-commit.d.ts +21 -0
- package/src/client/navigation-commit.js +114 -0
- package/src/client/navigation-fetch.d.ts +8 -0
- package/src/client/navigation-fetch.js +24 -0
- package/src/client/services/prefetch-manager.d.ts +2 -14
- package/src/client/services/prefetch-manager.js +29 -47
- package/src/client/services/view-transition-manager.js +1 -1
- package/src/client/types.d.ts +6 -0
- package/src/index.d.ts +3 -5
- package/src/index.js +7 -7
- package/src/client/services/dom-swapper.d.ts +0 -168
- package/src/client/services/dom-swapper.js +0 -529
- package/src/client/services/index.d.ts +0 -8
- package/src/client/services/index.js +0 -10
- package/src/client/services/scroll-manager.d.ts +0 -19
- package/src/client/services/scroll-manager.js +0 -36
- package/src/client/view-transition-utils.d.ts +0 -14
- package/src/client/view-transition-utils.js +0 -60
- package/src/types.d.ts +0 -15
- package/src/types.js +0 -5
|
@@ -142,21 +142,9 @@ export declare class PrefetchManager {
|
|
|
142
142
|
*/
|
|
143
143
|
private cancelHoverPrefetch;
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* Called once during initialization. Eager links are prefetched immediately;
|
|
148
|
-
* viewport-strategy links are registered with the IntersectionObserver.
|
|
149
|
-
*/
|
|
150
|
-
private observeExistingLinks;
|
|
151
|
-
/**
|
|
152
|
-
* Observes newly added links after DOM mutations.
|
|
153
|
-
*
|
|
154
|
-
* Should be called after client-side navigation or dynamic content updates
|
|
155
|
-
* to ensure new links are tracked for prefetching.
|
|
156
|
-
*
|
|
157
|
-
* @param root - The root element to search for links (defaults to document)
|
|
145
|
+
* Observes links for prefetching within `root`.
|
|
158
146
|
*/
|
|
159
|
-
|
|
147
|
+
observeLinks(root?: Element | Document): void;
|
|
160
148
|
/**
|
|
161
149
|
* Prefetches stylesheets discovered in HTML content.
|
|
162
150
|
*
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { assertHtmlPageResponse, shouldPrefetchLink } from "@ecopages/core/router/link-navigation-policy";
|
|
2
|
+
import { discoverNewStylesheetLinks, getCurrentStylesheetHrefs } from "../dom/stylesheet-discovery.js";
|
|
1
3
|
const DEFAULT_PREFETCH_OPTIONS = {
|
|
2
4
|
strategy: "intent",
|
|
3
5
|
delay: 65,
|
|
@@ -30,7 +32,7 @@ class PrefetchManager {
|
|
|
30
32
|
if (this.options.strategy === "hover" || this.options.strategy === "intent") {
|
|
31
33
|
this.setupHoverListeners();
|
|
32
34
|
}
|
|
33
|
-
this.
|
|
35
|
+
this.observeLinks();
|
|
34
36
|
}
|
|
35
37
|
/**
|
|
36
38
|
* Cleans up all prefetch-related observers and event listeners.
|
|
@@ -68,7 +70,11 @@ class PrefetchManager {
|
|
|
68
70
|
headers: { Accept: "text/html" },
|
|
69
71
|
priority: "low"
|
|
70
72
|
});
|
|
71
|
-
if (!response.ok)
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
this.prefetched.delete(url.href);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
await assertHtmlPageResponse(response);
|
|
72
78
|
const html = await response.text();
|
|
73
79
|
this.htmlCache.set(url.href, html);
|
|
74
80
|
await this.prefetchStylesheets(html, url);
|
|
@@ -117,9 +123,16 @@ class PrefetchManager {
|
|
|
117
123
|
fetch(url.href, {
|
|
118
124
|
headers: { Accept: "text/html" },
|
|
119
125
|
priority: "low"
|
|
120
|
-
}).then((response) => {
|
|
121
|
-
if (response.ok)
|
|
122
|
-
|
|
126
|
+
}).then(async (response) => {
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
await assertHtmlPageResponse(response);
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
return response.text();
|
|
123
136
|
}).then((freshHtml) => {
|
|
124
137
|
if (freshHtml) {
|
|
125
138
|
this.htmlCache.set(url.href, freshHtml);
|
|
@@ -222,10 +235,9 @@ class PrefetchManager {
|
|
|
222
235
|
const target = event.target;
|
|
223
236
|
const link = target.closest(this.options.linkSelector);
|
|
224
237
|
if (!link) return null;
|
|
225
|
-
if (link
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (!href || href.startsWith("#") || href.startsWith("javascript:")) return null;
|
|
238
|
+
if (!shouldPrefetchLink(link, { noPrefetchAttribute: this.options.noPrefetchAttribute })) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
229
241
|
return link;
|
|
230
242
|
}
|
|
231
243
|
/**
|
|
@@ -307,32 +319,9 @@ class PrefetchManager {
|
|
|
307
319
|
}
|
|
308
320
|
}
|
|
309
321
|
/**
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
* Called once during initialization. Eager links are prefetched immediately;
|
|
313
|
-
* viewport-strategy links are registered with the IntersectionObserver.
|
|
322
|
+
* Observes links for prefetching within `root`.
|
|
314
323
|
*/
|
|
315
|
-
|
|
316
|
-
const links = document.querySelectorAll(this.options.linkSelector);
|
|
317
|
-
for (const link of links) {
|
|
318
|
-
if (link.hasAttribute(this.options.noPrefetchAttribute)) continue;
|
|
319
|
-
const strategy = this.getLinkStrategy(link);
|
|
320
|
-
if (strategy === "eager") {
|
|
321
|
-
this.scheduleIdlePrefetch(link.href, true);
|
|
322
|
-
} else if (this.observer && strategy === "viewport") {
|
|
323
|
-
this.observer.observe(link);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Observes newly added links after DOM mutations.
|
|
329
|
-
*
|
|
330
|
-
* Should be called after client-side navigation or dynamic content updates
|
|
331
|
-
* to ensure new links are tracked for prefetching.
|
|
332
|
-
*
|
|
333
|
-
* @param root - The root element to search for links (defaults to document)
|
|
334
|
-
*/
|
|
335
|
-
observeNewLinks(root = document) {
|
|
324
|
+
observeLinks(root = document) {
|
|
336
325
|
const links = root.querySelectorAll(this.options.linkSelector);
|
|
337
326
|
for (const link of links) {
|
|
338
327
|
if (link.hasAttribute(this.options.noPrefetchAttribute)) continue;
|
|
@@ -358,19 +347,12 @@ class PrefetchManager {
|
|
|
358
347
|
async prefetchStylesheets(html, url) {
|
|
359
348
|
const parser = new DOMParser();
|
|
360
349
|
const doc = parser.parseFromString(`<base href="${url.href}">${html}`, "text/html");
|
|
361
|
-
const existingHrefs = /* @__PURE__ */ new Set([
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
for (const link of newStylesheets) {
|
|
368
|
-
if (!existingHrefs.has(link.href)) {
|
|
369
|
-
existingHrefs.add(link.href);
|
|
370
|
-
this.prefetchedStylesheets.add(link.href);
|
|
371
|
-
stylesheetFetches.push(this.prefetchStylesheet(link.href));
|
|
372
|
-
}
|
|
373
|
-
}
|
|
350
|
+
const existingHrefs = /* @__PURE__ */ new Set([...getCurrentStylesheetHrefs(), ...this.prefetchedStylesheets]);
|
|
351
|
+
const newStylesheetHrefs = discoverNewStylesheetLinks(doc, existingHrefs).map((link) => link.href);
|
|
352
|
+
const stylesheetFetches = newStylesheetHrefs.map((href) => {
|
|
353
|
+
this.prefetchedStylesheets.add(href);
|
|
354
|
+
return this.prefetchStylesheet(href);
|
|
355
|
+
});
|
|
374
356
|
await Promise.allSettled(stylesheetFetches);
|
|
375
357
|
}
|
|
376
358
|
async prefetchStylesheet(href) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { applyViewTransitionNames, clearViewTransitionNames } from "
|
|
1
|
+
import { applyViewTransitionNames, clearViewTransitionNames } from "@ecopages/core/client/view-transitions";
|
|
2
2
|
class ViewTransitionManager {
|
|
3
3
|
enabled;
|
|
4
4
|
constructor(enabled) {
|
package/src/client/types.d.ts
CHANGED
|
@@ -90,6 +90,12 @@ export interface EcoBeforeSwapEvent extends EcoNavigationEvent {
|
|
|
90
90
|
/** Event fired after the DOM swap completes */
|
|
91
91
|
export interface EcoAfterSwapEvent extends EcoNavigationEvent {
|
|
92
92
|
}
|
|
93
|
+
/** Custom event map for navigation lifecycle */
|
|
94
|
+
export interface EcoRouterEventMap {
|
|
95
|
+
'eco:before-swap': CustomEvent<EcoBeforeSwapEvent>;
|
|
96
|
+
'eco:after-swap': CustomEvent<EcoAfterSwapEvent>;
|
|
97
|
+
'eco:page-load': CustomEvent<EcoNavigationEvent>;
|
|
98
|
+
}
|
|
93
99
|
/** Default document-level `<html>` attributes synchronized during navigation swaps. */
|
|
94
100
|
export declare const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC: string[];
|
|
95
101
|
/** Default configuration options */
|
package/src/index.d.ts
CHANGED
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
* Client-side navigation and view transitions for Ecopages
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
9
|
-
export { EcoRouter, createRouter } from './client/eco-router.js';
|
|
10
|
-
export { DomSwapper, ScrollManager, ViewTransitionManager } from './client/services/index.js';
|
|
6
|
+
export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, type BrowserRouterNavigateOptions, type EcoAfterSwapEvent, type EcoBeforeSwapEvent, type EcoNavigationEvent, type EcoRouterEventMap, type EcoRouterOptions, } from './client/index.js';
|
|
7
|
+
export { DomSwapper } from './client/dom/dom-swapper.js';
|
|
8
|
+
export { ViewTransitionManager } from './client/services/view-transition-manager.js';
|
package/src/index.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from "./types.js";
|
|
2
1
|
import {
|
|
3
|
-
|
|
2
|
+
createRouter,
|
|
3
|
+
DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
4
|
+
DEFAULT_OPTIONS,
|
|
5
|
+
EcoRouter,
|
|
4
6
|
syncDocumentElementAttributes
|
|
5
|
-
} from "./client/
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
} from "./client/index.js";
|
|
8
|
+
import { DomSwapper } from "./client/dom/dom-swapper.js";
|
|
9
|
+
import { ViewTransitionManager } from "./client/services/view-transition-manager.js";
|
|
8
10
|
export {
|
|
9
11
|
DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
10
12
|
DEFAULT_OPTIONS,
|
|
11
13
|
DomSwapper,
|
|
12
14
|
EcoRouter,
|
|
13
|
-
ScrollManager,
|
|
14
15
|
ViewTransitionManager,
|
|
15
16
|
createRouter,
|
|
16
|
-
defaultDocumentElementAttributesToSync,
|
|
17
17
|
syncDocumentElementAttributes
|
|
18
18
|
};
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DOM morphing service for client-side navigation.
|
|
3
|
-
* Uses Idiomorph for body morphing and Turbo-style surgical updates for head.
|
|
4
|
-
* @module dom-swapper
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Handles DOM manipulation during client-side page transitions.
|
|
8
|
-
*
|
|
9
|
-
* Uses a hybrid approach inspired by Turbo:
|
|
10
|
-
* - Surgical head updates (no morphing) to prevent FOUC
|
|
11
|
-
* - Idiomorph for efficient body diffing
|
|
12
|
-
*/
|
|
13
|
-
export declare class DomSwapper {
|
|
14
|
-
private persistAttribute;
|
|
15
|
-
private pendingHeadScripts;
|
|
16
|
-
private pendingRerunScripts;
|
|
17
|
-
private rerunNonce;
|
|
18
|
-
constructor(persistAttribute: string);
|
|
19
|
-
/**
|
|
20
|
-
* Parses HTML string into a Document, injecting a temporary base tag for URL resolution.
|
|
21
|
-
*/
|
|
22
|
-
parseHTML(html: string, url?: URL): Document;
|
|
23
|
-
/**
|
|
24
|
-
* Preloads new stylesheets from target document to prevent FOUC.
|
|
25
|
-
*
|
|
26
|
-
* Discovers stylesheet links in the target document that aren't present in the
|
|
27
|
-
* current document, creates corresponding link elements, and waits for all to
|
|
28
|
-
* load before resolving. This follows Turbo's approach of waiting for stylesheets
|
|
29
|
-
* before any DOM updates.
|
|
30
|
-
*/
|
|
31
|
-
preloadStylesheets(newDocument: Document): Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
* Updates document head using Turbo-style surgical updates.
|
|
34
|
-
*
|
|
35
|
-
* This approach avoids morphing the head element entirely, which prevents
|
|
36
|
-
* browser repaints that cause FOUC. Instead, it:
|
|
37
|
-
* - Updates the document title
|
|
38
|
-
* - Merges meta tags (adds new, updates changed)
|
|
39
|
-
* - Leaves stylesheets untouched (they're preloaded separately)
|
|
40
|
-
* - Handles script re-execution for marked scripts
|
|
41
|
-
* - Injects new scripts from the incoming page that are absent from the current head
|
|
42
|
-
*/
|
|
43
|
-
morphHead(newDocument: Document): void;
|
|
44
|
-
/**
|
|
45
|
-
* Replays queued `data-eco-rerun` scripts after the body swap completes.
|
|
46
|
-
*
|
|
47
|
-
* Scripts are intentionally flushed after the new body is in place so DOM-
|
|
48
|
-
* dependent bootstraps bind against the incoming page rather than the page
|
|
49
|
-
* being replaced.
|
|
50
|
-
*/
|
|
51
|
-
flushRerunScripts(): void;
|
|
52
|
-
/**
|
|
53
|
-
* Returns whether pending rerun scripts require a full body replacement
|
|
54
|
-
* instead of morphing, so DOM-dependent bootstraps bind against fresh markup.
|
|
55
|
-
*/
|
|
56
|
-
shouldReplaceBodyForRerunScripts(): boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Detects custom elements without shadow DOM (light-DOM custom elements).
|
|
59
|
-
* These need full replacement rather than morphing, because morphdom would
|
|
60
|
-
* strip JS-generated content from their light DOM children.
|
|
61
|
-
*/
|
|
62
|
-
private isLightDomCustomElement;
|
|
63
|
-
/**
|
|
64
|
-
* Queues a custom element for deferred replacement instead of replacing it
|
|
65
|
-
* inline during the morphdom walk.
|
|
66
|
-
*
|
|
67
|
-
* Replacing elements during morphdom traversal mutates the live DOM tree,
|
|
68
|
-
* which can cause morphdom to skip siblings or process stale nodes.
|
|
69
|
-
* Deferring the replacement until after morphdom finishes avoids this.
|
|
70
|
-
*
|
|
71
|
-
* @returns Always `false` to tell morphdom to skip updating this element.
|
|
72
|
-
*/
|
|
73
|
-
private replaceCustomElement;
|
|
74
|
-
private materializeCustomElement;
|
|
75
|
-
/**
|
|
76
|
-
* Replaces all deferred custom elements after morphdom has finished traversing.
|
|
77
|
-
*
|
|
78
|
-
* Each element is recreated via `document.createElement` so the browser fires
|
|
79
|
-
* the full custom element lifecycle (`disconnectedCallback` on the old instance,
|
|
80
|
-
* `connectedCallback` on the new one). Elements that were already removed during
|
|
81
|
-
* the morph pass are skipped via the `isConnected` guard.
|
|
82
|
-
*/
|
|
83
|
-
private flushDeferredCustomElementReplacements;
|
|
84
|
-
/**
|
|
85
|
-
* Morphs document body using morphdom.
|
|
86
|
-
* Preserves persisted elements and hydrated custom elements.
|
|
87
|
-
* Light-DOM custom elements are fully replaced to trigger proper
|
|
88
|
-
* disconnectedCallback → connectedCallback lifecycle.
|
|
89
|
-
*/
|
|
90
|
-
morphBody(newDocument: Document): void;
|
|
91
|
-
/**
|
|
92
|
-
* Replaces body content in a single operation.
|
|
93
|
-
* Preserves persisted elements by moving them to the new body.
|
|
94
|
-
* Use when View Transitions are disabled.
|
|
95
|
-
*/
|
|
96
|
-
replaceBody(newDocument: Document): void;
|
|
97
|
-
/**
|
|
98
|
-
* Collects all `data-eco-rerun` scripts from the incoming document.
|
|
99
|
-
*
|
|
100
|
-
* These scripts are re-executed after each navigation so their side-effects
|
|
101
|
-
* (event listeners, DOM bootstraps) bind against the new page content.
|
|
102
|
-
*/
|
|
103
|
-
private collectRerunScripts;
|
|
104
|
-
/**
|
|
105
|
-
* Removes head scripts that are no longer present in the incoming document.
|
|
106
|
-
*
|
|
107
|
-
* Persisted scripts and executable inline scripts with stable identifiers
|
|
108
|
-
* are kept to avoid breaking long-lived runtime state.
|
|
109
|
-
*/
|
|
110
|
-
private removeStaleHeadScripts;
|
|
111
|
-
/**
|
|
112
|
-
* Determines whether an inline head script should survive navigation.
|
|
113
|
-
*
|
|
114
|
-
* Only identified (`data-eco-script-id` or `id`), executable inline scripts
|
|
115
|
-
* are persisted. External scripts and rerun scripts are never persisted here
|
|
116
|
-
* because they have their own lifecycle management.
|
|
117
|
-
*/
|
|
118
|
-
private shouldPersistExecutableInlineHeadScript;
|
|
119
|
-
/**
|
|
120
|
-
* Returns whether a script is non-executable (e.g. `type="application/json"`).
|
|
121
|
-
*
|
|
122
|
-
* Non-executable scripts are data carriers (JSON-LD, page data) that can be
|
|
123
|
-
* safely replaced without side-effects, unlike executable scripts that would
|
|
124
|
-
* re-run their bootstrap logic.
|
|
125
|
-
*/
|
|
126
|
-
private isNonExecutableHeadScript;
|
|
127
|
-
/**
|
|
128
|
-
* Compares two head scripts for structural equality (key, content, attributes).
|
|
129
|
-
*
|
|
130
|
-
* Used to skip replacing non-executable data scripts when their content
|
|
131
|
-
* has not changed between navigations.
|
|
132
|
-
*/
|
|
133
|
-
private areHeadScriptsEquivalent;
|
|
134
|
-
/**
|
|
135
|
-
* Derives a stable identity key for a head script.
|
|
136
|
-
*
|
|
137
|
-
* Priority: `data-eco-script-id` / `id` > `src` > trimmed inline content.
|
|
138
|
-
* Returns `null` for empty anonymous inline scripts that cannot be tracked.
|
|
139
|
-
*/
|
|
140
|
-
private getHeadScriptKey;
|
|
141
|
-
/**
|
|
142
|
-
* Finds an existing head script that matches the given script's identity key.
|
|
143
|
-
*/
|
|
144
|
-
private findExistingHeadScript;
|
|
145
|
-
/**
|
|
146
|
-
* Finds an existing rerun script in the given root by `data-eco-script-id` or
|
|
147
|
-
* by matching `src` and `textContent`.
|
|
148
|
-
*/
|
|
149
|
-
private findExistingRerunScript;
|
|
150
|
-
/**
|
|
151
|
-
* Returns whether a rerun script is an external ES module (`type="module"` with `src`).
|
|
152
|
-
*
|
|
153
|
-
* Module scripts are cached by URL, so re-execution requires cache-busting
|
|
154
|
-
* via a query parameter nonce.
|
|
155
|
-
*/
|
|
156
|
-
private isExternalModuleRerunScript;
|
|
157
|
-
private getRegisteredRerunScript;
|
|
158
|
-
/**
|
|
159
|
-
* Appends a nonce query parameter to a script URL to bust the browser's
|
|
160
|
-
* module cache and force re-execution on navigation.
|
|
161
|
-
*/
|
|
162
|
-
private createRerunScriptUrl;
|
|
163
|
-
/**
|
|
164
|
-
* Manually attaches declarative shadow DOM templates.
|
|
165
|
-
* Browsers only process `<template shadowrootmode>` during initial parse.
|
|
166
|
-
*/
|
|
167
|
-
private processDeclarativeShadowDOM;
|
|
168
|
-
}
|