@ecopages/browser-router 0.2.1-beta.0 → 0.3.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 +24 -7
- package/package.json +11 -8
- package/src/client/document-element-sync.d.ts +0 -7
- package/src/client/document-element-sync.js +28 -18
- package/src/client/dom/body-morpher.d.ts +27 -0
- package/src/client/dom/body-morpher.js +166 -0
- package/src/client/dom/dom-swapper.d.ts +7 -0
- package/src/client/dom/dom-swapper.js +15 -0
- package/src/client/dom/stylesheet-discovery.d.ts +12 -0
- package/src/client/dom/stylesheet-discovery.js +24 -0
- package/src/client/eco-router-perform-navigation.d.ts +13 -0
- package/src/client/eco-router-perform-navigation.js +24 -0
- package/src/client/eco-router.d.ts +2 -35
- package/src/client/eco-router.js +329 -479
- package/src/client/index.d.ts +3 -0
- package/src/client/index.js +3 -0
- package/src/client/navigation-commit-reload.d.ts +21 -0
- package/src/client/navigation-commit-reload.js +16 -0
- package/src/client/navigation-commit.d.ts +19 -0
- package/src/client/navigation-commit.js +83 -0
- package/src/client/services/prefetch-manager.d.ts +4 -15
- package/src/client/services/prefetch-manager.js +353 -370
- package/src/client/types.d.ts +16 -13
- package/src/client/types.js +21 -20
- package/src/index.d.ts +2 -5
- package/src/index.js +7 -18
- 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/services/view-transition-manager.d.ts +0 -29
- package/src/client/services/view-transition-manager.js +0 -54
- package/src/client/view-transition-utils.d.ts +0 -14
- package/src/client/view-transition-utils.js +0 -60
- package/src/styles.css +0 -218
- package/src/types.d.ts +0 -15
- package/src/types.js +0 -5
package/src/client/types.d.ts
CHANGED
|
@@ -59,6 +59,12 @@ export interface EcoRouterOptions {
|
|
|
59
59
|
/**
|
|
60
60
|
* Whether to use the View Transition API for animations.
|
|
61
61
|
* Falls back to instant swap if not supported.
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* When enabled, injects `html { view-transition-name: none }` so the UA root
|
|
65
|
+
* group does not flash lighter on dark UIs. `startViewTransition` runs only when
|
|
66
|
+
* the current or incoming page has `data-view-transition` elements. Use
|
|
67
|
+
* `viewTransitions: false` to disable entirely.
|
|
62
68
|
* @default true
|
|
63
69
|
*/
|
|
64
70
|
viewTransitions?: boolean;
|
|
@@ -73,23 +79,20 @@ export interface EcoRouterOptions {
|
|
|
73
79
|
* @default { strategy: 'intent', delay: 65, noPrefetchAttribute: 'data-eco-no-prefetch', respectDataSaver: true }
|
|
74
80
|
*/
|
|
75
81
|
prefetch?: PrefetchConfig | false;
|
|
82
|
+
/**
|
|
83
|
+
* Loads a URL as a full document, replacing the current page.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* Runs for every navigation the router hands back to the browser: cross-origin and
|
|
87
|
+
* static asset targets of `navigate()`, a `before-swap` listener calling `reload()`,
|
|
88
|
+
* a document owned by a runtime that cannot take over, and a failed fetch or swap.
|
|
89
|
+
* @default (href) => window.location.assign(href)
|
|
90
|
+
*/
|
|
91
|
+
reloadDocument?: (href: string) => void;
|
|
76
92
|
}
|
|
77
93
|
export type BrowserRouterNavigateOptions = {
|
|
78
94
|
direction?: 'forward' | 'back' | 'replace';
|
|
79
95
|
};
|
|
80
|
-
/** Events emitted during the navigation lifecycle */
|
|
81
|
-
export interface EcoNavigationEvent {
|
|
82
|
-
url: URL;
|
|
83
|
-
direction: 'forward' | 'back' | 'replace';
|
|
84
|
-
}
|
|
85
|
-
/** Event fired before the DOM swap occurs */
|
|
86
|
-
export interface EcoBeforeSwapEvent extends EcoNavigationEvent {
|
|
87
|
-
newDocument: Document;
|
|
88
|
-
reload: () => void;
|
|
89
|
-
}
|
|
90
|
-
/** Event fired after the DOM swap completes */
|
|
91
|
-
export interface EcoAfterSwapEvent extends EcoNavigationEvent {
|
|
92
|
-
}
|
|
93
96
|
/** Default document-level `<html>` attributes synchronized during navigation swaps. */
|
|
94
97
|
export declare const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC: string[];
|
|
95
98
|
/** Default configuration options */
|
package/src/client/types.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from
|
|
1
|
+
import { ECO_DOCUMENT_OWNER_ATTRIBUTE } from '@ecopages/core/client';
|
|
2
|
+
import { ECO_PERSIST_ATTRIBUTE } from '@ecopages/core/client';
|
|
3
|
+
/** Default prefetch configuration */
|
|
2
4
|
const DEFAULT_PREFETCH_CONFIG = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
strategy: 'intent',
|
|
6
|
+
delay: 65,
|
|
7
|
+
noPrefetchAttribute: 'data-eco-no-prefetch',
|
|
8
|
+
respectDataSaver: true,
|
|
7
9
|
};
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
DEFAULT_OPTIONS
|
|
10
|
+
/** Default document-level `<html>` attributes synchronized during navigation swaps. */
|
|
11
|
+
export const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC = ['lang', 'dir', ECO_DOCUMENT_OWNER_ATTRIBUTE];
|
|
12
|
+
/** Default configuration options */
|
|
13
|
+
export const DEFAULT_OPTIONS = {
|
|
14
|
+
linkSelector: 'a[href]',
|
|
15
|
+
documentElementAttributesToSync: DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
16
|
+
persistAttribute: ECO_PERSIST_ATTRIBUTE,
|
|
17
|
+
reloadAttribute: 'data-eco-reload',
|
|
18
|
+
updateHistory: true,
|
|
19
|
+
scrollBehavior: 'top',
|
|
20
|
+
viewTransitions: true,
|
|
21
|
+
smoothScroll: false,
|
|
22
|
+
prefetch: DEFAULT_PREFETCH_CONFIG,
|
|
23
|
+
reloadDocument: (href) => window.location.assign(href),
|
|
23
24
|
};
|
package/src/index.d.ts
CHANGED
|
@@ -3,8 +3,5 @@
|
|
|
3
3
|
* Client-side navigation and view transitions for Ecopages
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
7
|
-
export {
|
|
8
|
-
export { defaultDocumentElementAttributesToSync, syncDocumentElementAttributes, } from './client/document-element-sync.js';
|
|
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 EcoRouterOptions, } from './client/index.js';
|
|
7
|
+
export { DomSwapper } from './client/dom/dom-swapper.js';
|
package/src/index.js
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
10
|
-
DEFAULT_OPTIONS,
|
|
11
|
-
DomSwapper,
|
|
12
|
-
EcoRouter,
|
|
13
|
-
ScrollManager,
|
|
14
|
-
ViewTransitionManager,
|
|
15
|
-
createRouter,
|
|
16
|
-
defaultDocumentElementAttributesToSync,
|
|
17
|
-
syncDocumentElementAttributes
|
|
18
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @ecopages/browser-router
|
|
3
|
+
* Client-side navigation and view transitions for Ecopages
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, } from './client/index.js';
|
|
7
|
+
export { DomSwapper } from './client/dom/dom-swapper.js';
|
|
@@ -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
|
-
}
|