@ecopages/browser-router 0.2.1 → 0.3.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.
- package/README.md +27 -8
- 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/stylesheet-discovery.d.ts +12 -0
- package/src/client/dom/stylesheet-discovery.js +24 -0
- package/src/client/eco-router-perform-navigation.d.ts +20 -0
- package/src/client/eco-router-perform-navigation.js +31 -0
- package/src/client/eco-router.d.ts +11 -41
- package/src/client/eco-router.js +330 -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 +17 -0
- package/src/client/navigation-commit.js +82 -0
- package/src/client/services/prefetch-manager.d.ts +15 -39
- package/src/client/services/prefetch-manager.js +327 -360
- package/src/client/types.d.ts +16 -16
- package/src/client/types.js +21 -20
- package/src/index.d.ts +1 -5
- package/src/index.js +6 -18
- package/CHANGELOG.md +0 -25
- package/src/client/services/dom-swapper.d.ts +0 -166
- package/src/client/services/dom-swapper.js +0 -497
- 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/eco-router.js
CHANGED
|
@@ -1,497 +1,348 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
canInterceptLink(event, link) {
|
|
47
|
-
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return null;
|
|
48
|
-
if (event.button !== 0) return null;
|
|
49
|
-
const target = link.getAttribute("target");
|
|
50
|
-
if (target && target !== "_self") return null;
|
|
51
|
-
if (link.hasAttribute(this.options.reloadAttribute)) return null;
|
|
52
|
-
if (link.hasAttribute("download")) return null;
|
|
53
|
-
const href = link.getAttribute("href");
|
|
54
|
-
if (!href) return null;
|
|
55
|
-
if (href.startsWith("#")) return null;
|
|
56
|
-
if (href.startsWith("javascript:")) return null;
|
|
57
|
-
const url = new URL(href, window.location.origin);
|
|
58
|
-
if (!this.isSameOrigin(url)) return null;
|
|
59
|
-
if (url.hash && url.pathname === window.location.pathname && url.search === window.location.search) {
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
return href;
|
|
63
|
-
}
|
|
64
|
-
getRecoveredPointerHref() {
|
|
65
|
-
const href = recoverPendingNavigationHref(
|
|
66
|
-
this.pendingPointerNavigation,
|
|
67
|
-
this.pendingNavigations > 0,
|
|
68
|
-
performance.now()
|
|
69
|
-
);
|
|
70
|
-
if (!href) {
|
|
71
|
-
this.pendingPointerNavigation = null;
|
|
72
|
-
}
|
|
73
|
-
return href;
|
|
74
|
-
}
|
|
75
|
-
getRecoveredHoverHref() {
|
|
76
|
-
const href = recoverPendingNavigationHref(
|
|
77
|
-
this.pendingHoverNavigation,
|
|
78
|
-
this.pendingNavigations > 0,
|
|
79
|
-
performance.now()
|
|
80
|
-
);
|
|
81
|
-
if (!href) {
|
|
82
|
-
this.pendingHoverNavigation = null;
|
|
83
|
-
}
|
|
84
|
-
return href;
|
|
85
|
-
}
|
|
86
|
-
isAnotherNavigationRuntimeActive() {
|
|
87
|
-
const ownerState = getEcoNavigationRuntime(window).getOwnerState();
|
|
88
|
-
return ownerState.owner !== "none" && ownerState.owner !== "browser-router" && ownerState.canHandleSpaNavigation;
|
|
89
|
-
}
|
|
90
|
-
getDocumentOwner(doc) {
|
|
91
|
-
return getEcoNavigationRuntime(window).resolveDocumentOwner(doc, "browser-router");
|
|
92
|
-
}
|
|
93
|
-
adoptDocumentOwner(doc) {
|
|
94
|
-
getEcoNavigationRuntime(window).adoptDocumentOwner(doc, "browser-router");
|
|
95
|
-
}
|
|
96
|
-
syncDocumentElementAttributes(newDocument) {
|
|
97
|
-
syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
|
|
98
|
-
}
|
|
99
|
-
reloadDocument(url) {
|
|
100
|
-
window.location.assign(url.href);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Commits a fully fetched document into the live page.
|
|
104
|
-
*
|
|
105
|
-
* When browser-router accepts a handoff from another runtime, it delays source
|
|
106
|
-
* runtime cleanup until the incoming document has been prepared and is ready to
|
|
107
|
-
* commit. That ordering avoids the blank-page window we previously hit when a
|
|
108
|
-
* delegated navigation went stale after the source runtime had already torn
|
|
109
|
-
* itself down.
|
|
110
|
-
*/
|
|
111
|
-
async commitDocumentNavigation(url, direction, newDocument, options = {}) {
|
|
112
|
-
const previousUrl = new URL(window.location.href);
|
|
113
|
-
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
114
|
-
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
115
|
-
const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, "browser-router");
|
|
116
|
-
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, "browser-router");
|
|
117
|
-
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
118
|
-
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner && currentDocumentOwner !== "browser-router" && activeOwner === currentDocumentOwner;
|
|
119
|
-
let shouldReload = false;
|
|
120
|
-
const beforeSwapEvent = {
|
|
121
|
-
url,
|
|
122
|
-
direction,
|
|
123
|
-
newDocument,
|
|
124
|
-
reload: () => {
|
|
125
|
-
shouldReload = true;
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
document.dispatchEvent(new CustomEvent("eco:before-swap", { detail: beforeSwapEvent }));
|
|
129
|
-
if (isStaleNavigation()) return;
|
|
130
|
-
if (shouldReload) {
|
|
131
|
-
if (shouldCleanupCurrentOwner) {
|
|
132
|
-
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
133
|
-
}
|
|
134
|
-
if (isStaleNavigation()) return;
|
|
135
|
-
this.reloadDocument(url);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
const useViewTransitions = this.options.viewTransitions;
|
|
139
|
-
await this.domSwapper.preloadStylesheets(newDocument);
|
|
140
|
-
if (isStaleNavigation()) return;
|
|
141
|
-
if (shouldCleanupCurrentOwner) {
|
|
142
|
-
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
143
|
-
}
|
|
144
|
-
if (isStaleNavigation()) return;
|
|
145
|
-
const commitSwap = () => {
|
|
146
|
-
if (isStaleNavigation()) return;
|
|
147
|
-
if (this.options.updateHistory && direction === "forward") {
|
|
148
|
-
window.history.pushState({}, "", url.href);
|
|
149
|
-
} else if (direction === "replace") {
|
|
150
|
-
window.history.replaceState({}, "", url.href);
|
|
151
|
-
}
|
|
152
|
-
this.syncDocumentElementAttributes(newDocument);
|
|
153
|
-
this.domSwapper.morphHead(newDocument);
|
|
154
|
-
if (useViewTransitions && !this.domSwapper.shouldReplaceBodyForRerunScripts()) {
|
|
155
|
-
this.domSwapper.morphBody(newDocument);
|
|
156
|
-
} else {
|
|
157
|
-
this.domSwapper.replaceBody(newDocument);
|
|
158
|
-
}
|
|
159
|
-
this.domSwapper.flushRerunScripts();
|
|
160
|
-
this.scrollManager.handleScroll(url, previousUrl);
|
|
161
|
-
};
|
|
162
|
-
if (useViewTransitions) {
|
|
163
|
-
await this.viewTransitionManager.transition(commitSwap);
|
|
164
|
-
} else {
|
|
165
|
-
commitSwap();
|
|
1
|
+
/**
|
|
2
|
+
* Client-side router for Ecopages with morphdom-based DOM diffing.
|
|
3
|
+
* @module eco-router
|
|
4
|
+
*/
|
|
5
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/client';
|
|
6
|
+
import { getAnchorFromNavigationEvent, isStaticAssetHref, recoverPendingNavigationHref, } from '@ecopages/core/client';
|
|
7
|
+
import { getNavigableHrefFromClick } from '@ecopages/core/client';
|
|
8
|
+
import { decideQueuedNavigationReplay } from '@ecopages/core/client';
|
|
9
|
+
import { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from './types.js';
|
|
10
|
+
import { PrefetchManager } from './services/prefetch-manager.js';
|
|
11
|
+
import { NavigationCommit } from './navigation-commit.js';
|
|
12
|
+
import { runPerformNavigationAttempt, shouldIgnorePerformNavigationError } from './eco-router-perform-navigation.js';
|
|
13
|
+
/**
|
|
14
|
+
* Intercepts same-origin link clicks and performs client-side navigation
|
|
15
|
+
* using morphdom for efficient DOM diffing. Supports View Transitions API.
|
|
16
|
+
*/
|
|
17
|
+
export class EcoRouter {
|
|
18
|
+
options;
|
|
19
|
+
unregisterNavigationRuntime = null;
|
|
20
|
+
started = false;
|
|
21
|
+
pendingNavigations = 0;
|
|
22
|
+
pendingPointerNavigation = null;
|
|
23
|
+
queuedNavigationHref = null;
|
|
24
|
+
prefetchManager = null;
|
|
25
|
+
navigationCommit;
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
this.options = {
|
|
28
|
+
...DEFAULT_OPTIONS,
|
|
29
|
+
...options,
|
|
30
|
+
documentElementAttributesToSync: [
|
|
31
|
+
...(options.documentElementAttributesToSync ?? DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC),
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
if (this.options.prefetch !== false) {
|
|
35
|
+
this.prefetchManager = new PrefetchManager({
|
|
36
|
+
...this.options.prefetch,
|
|
37
|
+
reloadAttribute: this.options.reloadAttribute,
|
|
38
|
+
linkSelector: this.options.linkSelector,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.handleClick = this.handleClick.bind(this);
|
|
42
|
+
this.handlePointerDown = this.handlePointerDown.bind(this);
|
|
43
|
+
this.handlePopState = this.handlePopState.bind(this);
|
|
44
|
+
this.navigationCommit = new NavigationCommit(this.prefetchManager, this.options);
|
|
166
45
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
document.dispatchEvent(new CustomEvent("eco:after-swap", { detail: afterSwapEvent }));
|
|
174
|
-
this.prefetchManager?.observeNewLinks();
|
|
175
|
-
if (options.html) {
|
|
176
|
-
this.prefetchManager?.cacheVisitedPage(url.href, options.html);
|
|
46
|
+
getRecoveredPointerHref() {
|
|
47
|
+
const href = recoverPendingNavigationHref(this.pendingPointerNavigation, this.pendingNavigations > 0, performance.now());
|
|
48
|
+
if (!href) {
|
|
49
|
+
this.pendingPointerNavigation = null;
|
|
50
|
+
}
|
|
51
|
+
return href;
|
|
177
52
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
new CustomEvent("eco:page-load", {
|
|
182
|
-
detail: { url, direction }
|
|
183
|
-
})
|
|
184
|
-
);
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Starts the router and begins intercepting navigation.
|
|
189
|
-
*
|
|
190
|
-
* Attaches click handlers for links and popstate handlers for browser
|
|
191
|
-
* back/forward buttons. Also starts the prefetch manager if configured.
|
|
192
|
-
*/
|
|
193
|
-
start() {
|
|
194
|
-
if (this.started) {
|
|
195
|
-
return;
|
|
53
|
+
isAnotherNavigationRuntimeActive() {
|
|
54
|
+
const ownerState = getEcoNavigationRuntime(window).getOwnerState();
|
|
55
|
+
return (ownerState.owner !== 'none' && ownerState.owner !== 'browser-router' && ownerState.canHandleSpaNavigation);
|
|
196
56
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
this.unregisterNavigationRuntime?.();
|
|
207
|
-
this.unregisterNavigationRuntime = navigationRuntime.register({
|
|
208
|
-
owner: "browser-router",
|
|
209
|
-
navigate: async (request) => {
|
|
210
|
-
await this.performNavigation(
|
|
211
|
-
new URL(request.href, window.location.origin),
|
|
212
|
-
request.direction ?? "forward"
|
|
213
|
-
);
|
|
214
|
-
return true;
|
|
215
|
-
},
|
|
216
|
-
handoffNavigation: async (request) => {
|
|
217
|
-
const { isStaleNavigation, complete } = this.beginNavigationTransaction();
|
|
218
|
-
if (isStaleNavigation()) return true;
|
|
219
|
-
try {
|
|
220
|
-
await this.commitDocumentNavigation(
|
|
221
|
-
new URL(request.finalHref ?? request.href, window.location.origin),
|
|
222
|
-
request.direction ?? "forward",
|
|
223
|
-
request.document,
|
|
224
|
-
{ html: request.html, isStaleNavigation }
|
|
225
|
-
);
|
|
226
|
-
} finally {
|
|
227
|
-
complete();
|
|
57
|
+
/**
|
|
58
|
+
* Starts the router and begins intercepting navigation.
|
|
59
|
+
*
|
|
60
|
+
* Attaches click handlers for links and popstate handlers for browser
|
|
61
|
+
* back/forward buttons. Also starts the prefetch manager if configured.
|
|
62
|
+
*/
|
|
63
|
+
start() {
|
|
64
|
+
if (this.started) {
|
|
65
|
+
return;
|
|
228
66
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
67
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
68
|
+
navigationRuntime.cancelCurrentNavigationTransaction();
|
|
69
|
+
this.pendingNavigations = 0;
|
|
70
|
+
this.queuedNavigationHref = null;
|
|
71
|
+
this.pendingPointerNavigation = null;
|
|
72
|
+
document.addEventListener('pointerdown', this.handlePointerDown, true);
|
|
73
|
+
document.addEventListener('click', this.handleClick, true);
|
|
74
|
+
window.addEventListener('popstate', this.handlePopState);
|
|
75
|
+
this.prefetchManager?.start();
|
|
76
|
+
this.unregisterNavigationRuntime?.();
|
|
77
|
+
this.unregisterNavigationRuntime = navigationRuntime.register({
|
|
78
|
+
owner: 'browser-router',
|
|
79
|
+
navigate: async (request) => {
|
|
80
|
+
await this.performNavigation(new URL(request.href, window.location.origin), request.direction ?? 'forward');
|
|
81
|
+
return true;
|
|
82
|
+
},
|
|
83
|
+
handoffNavigation: async (request) => {
|
|
84
|
+
const { isStaleNavigation, complete } = this.beginNavigationTransaction();
|
|
85
|
+
if (isStaleNavigation())
|
|
86
|
+
return true;
|
|
87
|
+
try {
|
|
88
|
+
await this.navigationCommit.commit(new URL(request.finalHref ?? request.href, window.location.origin), request.direction ?? 'forward', request.document, { html: request.html, isStaleNavigation });
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
complete();
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
},
|
|
95
|
+
reloadCurrentPage: async (request) => {
|
|
96
|
+
if (this.pendingNavigations > 0) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const currentUrl = window.location.href;
|
|
100
|
+
if (request?.clearCache) {
|
|
101
|
+
this.prefetchManager?.invalidate(currentUrl);
|
|
102
|
+
}
|
|
103
|
+
return await this.performNavigation(new URL(currentUrl, window.location.origin), 'replace', {
|
|
104
|
+
bypassPrefetchCache: !!request?.clearCache,
|
|
105
|
+
allowFullDocumentFallback: false,
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
cleanupBeforeHandoff: async () => {
|
|
109
|
+
this.cancelNavigationTransaction();
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
getEcoNavigationRuntime(window).adoptDocumentOwner(document, 'browser-router');
|
|
113
|
+
// Cache the initial page for instant back-navigation
|
|
114
|
+
const initialHtml = document.documentElement.outerHTML;
|
|
115
|
+
this.prefetchManager?.cacheVisitedPage(window.location.href, initialHtml);
|
|
116
|
+
this.started = true;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Stops the router and cleans up all event listeners.
|
|
120
|
+
* After calling this, navigation will fall back to full page reloads.
|
|
121
|
+
*/
|
|
122
|
+
stop() {
|
|
123
|
+
if (!this.started) {
|
|
124
|
+
return;
|
|
236
125
|
}
|
|
237
|
-
await this.performNavigation(new URL(currentUrl, window.location.origin), "replace");
|
|
238
|
-
},
|
|
239
|
-
cleanupBeforeHandoff: async () => {
|
|
240
126
|
this.cancelNavigationTransaction();
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
this.cancelNavigationTransaction();
|
|
257
|
-
document.removeEventListener("mouseover", this.handleHoverIntent, true);
|
|
258
|
-
document.removeEventListener("pointerover", this.handleHoverIntent, true);
|
|
259
|
-
document.removeEventListener("mousemove", this.handleHoverIntent, true);
|
|
260
|
-
document.removeEventListener("pointermove", this.handleHoverIntent, true);
|
|
261
|
-
document.removeEventListener("pointerdown", this.handlePointerDown, true);
|
|
262
|
-
document.removeEventListener("click", this.handleClick, true);
|
|
263
|
-
window.removeEventListener("popstate", this.handlePopState);
|
|
264
|
-
this.prefetchManager?.stop();
|
|
265
|
-
this.unregisterNavigationRuntime?.();
|
|
266
|
-
this.unregisterNavigationRuntime = null;
|
|
267
|
-
this.started = false;
|
|
268
|
-
this.pendingHoverNavigation = null;
|
|
269
|
-
this.pendingPointerNavigation = null;
|
|
270
|
-
this.queuedNavigationHref = null;
|
|
271
|
-
const win = window;
|
|
272
|
-
if (win[ACTIVE_ROUTER_KEY] === this) {
|
|
273
|
-
delete win[ACTIVE_ROUTER_KEY];
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Programmatic navigation.
|
|
278
|
-
* Falls back to full page reload for cross-origin URLs.
|
|
279
|
-
* @param href - The URL to navigate to
|
|
280
|
-
* @param options - Navigation options
|
|
281
|
-
* @param options.replace - If true, replaces the current history entry instead of pushing
|
|
282
|
-
*/
|
|
283
|
-
async navigate(href, options = {}) {
|
|
284
|
-
const url = new URL(href, window.location.origin);
|
|
285
|
-
if (!this.isSameOrigin(url)) {
|
|
286
|
-
window.location.href = href;
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
await this.performNavigation(url, options.replace ? "replace" : "forward");
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Manually prefetch a URL.
|
|
293
|
-
* @param href - The URL to prefetch
|
|
294
|
-
*/
|
|
295
|
-
async prefetch(href) {
|
|
296
|
-
if (!this.prefetchManager) {
|
|
297
|
-
console.warn("[ecopages] Prefetching is disabled. Enable it in router options.");
|
|
298
|
-
return;
|
|
127
|
+
document.removeEventListener('pointerdown', this.handlePointerDown, true);
|
|
128
|
+
document.removeEventListener('click', this.handleClick, true);
|
|
129
|
+
window.removeEventListener('popstate', this.handlePopState);
|
|
130
|
+
this.prefetchManager?.stop();
|
|
131
|
+
this.unregisterNavigationRuntime?.();
|
|
132
|
+
this.unregisterNavigationRuntime = null;
|
|
133
|
+
this.started = false;
|
|
134
|
+
this.pendingPointerNavigation = null;
|
|
135
|
+
this.queuedNavigationHref = null;
|
|
136
|
+
const win = window;
|
|
137
|
+
if (win[ACTIVE_ROUTER_KEY] === this) {
|
|
138
|
+
delete win[ACTIVE_ROUTER_KEY];
|
|
139
|
+
}
|
|
299
140
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Programmatic navigation.
|
|
143
|
+
* Falls back to full page reload for cross-origin URLs.
|
|
144
|
+
* @param href - The URL to navigate to
|
|
145
|
+
* @param options - Navigation options
|
|
146
|
+
* @param options.replace - If true, replaces the current history entry instead of pushing
|
|
147
|
+
*/
|
|
148
|
+
async navigate(href, options = {}) {
|
|
149
|
+
const url = new URL(href, window.location.origin);
|
|
150
|
+
if (url.origin !== window.location.origin) {
|
|
151
|
+
this.options.reloadDocument(href);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (isStaticAssetHref(href)) {
|
|
155
|
+
this.options.reloadDocument(url.href);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
await this.performNavigation(url, options.replace ? 'replace' : 'forward');
|
|
316
159
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Manually prefetch a URL.
|
|
162
|
+
* @param href - The URL to prefetch
|
|
163
|
+
*/
|
|
164
|
+
async prefetch(href) {
|
|
165
|
+
if (!this.prefetchManager) {
|
|
166
|
+
console.warn('[ecopages] Prefetching is disabled. Enable it in router options.');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
return this.prefetchManager.prefetch(href);
|
|
324
170
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
171
|
+
/**
|
|
172
|
+
* Intercepts link clicks for client-side navigation.
|
|
173
|
+
*
|
|
174
|
+
* Filters out clicks with modifier keys (opens new tab), non-left clicks,
|
|
175
|
+
* external links, download links, and links with the reload attribute.
|
|
176
|
+
*
|
|
177
|
+
* Uses `event.composedPath()` to correctly detect clicks on anchors inside
|
|
178
|
+
* Shadow DOM boundaries (Web Components).
|
|
179
|
+
*/
|
|
180
|
+
handlePointerDown(event) {
|
|
181
|
+
const link = getAnchorFromNavigationEvent(event, this.options.linkSelector);
|
|
182
|
+
if (!link) {
|
|
183
|
+
this.pendingPointerNavigation = null;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const href = getNavigableHrefFromClick(event, link, { reloadAttribute: this.options.reloadAttribute });
|
|
187
|
+
this.pendingPointerNavigation = href
|
|
188
|
+
? {
|
|
189
|
+
href,
|
|
190
|
+
timestamp: performance.now(),
|
|
191
|
+
}
|
|
192
|
+
: null;
|
|
193
|
+
if (href && this.pendingNavigations > 0) {
|
|
194
|
+
this.queuedNavigationHref = href;
|
|
195
|
+
}
|
|
330
196
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
197
|
+
handleClick(event) {
|
|
198
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
199
|
+
const link = getAnchorFromNavigationEvent(event, this.options.linkSelector);
|
|
200
|
+
const href = link
|
|
201
|
+
? getNavigableHrefFromClick(event, link, { reloadAttribute: this.options.reloadAttribute })
|
|
202
|
+
: this.getRecoveredPointerHref();
|
|
203
|
+
this.pendingPointerNavigation = null;
|
|
204
|
+
if (!href)
|
|
205
|
+
return;
|
|
206
|
+
this.queuedNavigationHref = null;
|
|
207
|
+
if (this.isAnotherNavigationRuntimeActive()) {
|
|
208
|
+
event.preventDefault();
|
|
209
|
+
event.stopImmediatePropagation();
|
|
210
|
+
void navigationRuntime.requestNavigation({
|
|
211
|
+
href,
|
|
212
|
+
direction: 'forward',
|
|
213
|
+
source: 'browser-router',
|
|
214
|
+
});
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const url = new URL(href, window.location.origin);
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
if (this.pendingNavigations > 0) {
|
|
220
|
+
this.queuedNavigationHref = href;
|
|
221
|
+
this.cancelNavigationTransaction();
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
this.performNavigation(url, 'forward');
|
|
334
225
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Handles browser back/forward navigation.
|
|
228
|
+
* Triggered by the History API's popstate event.
|
|
229
|
+
*/
|
|
230
|
+
handlePopState(_event) {
|
|
231
|
+
if (this.isAnotherNavigationRuntimeActive())
|
|
232
|
+
return;
|
|
233
|
+
const url = new URL(window.location.href);
|
|
234
|
+
this.performNavigation(url, 'back');
|
|
341
235
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
345
|
-
const link = this.getLinkFromEvent(event);
|
|
346
|
-
const href = link ? this.canInterceptLink(event, link) : this.getRecoveredPointerHref() ?? this.getRecoveredHoverHref();
|
|
347
|
-
this.pendingPointerNavigation = null;
|
|
348
|
-
this.pendingHoverNavigation = null;
|
|
349
|
-
if (!href) return;
|
|
350
|
-
this.queuedNavigationHref = null;
|
|
351
|
-
if (this.isAnotherNavigationRuntimeActive()) {
|
|
352
|
-
event.preventDefault();
|
|
353
|
-
event.stopImmediatePropagation();
|
|
354
|
-
void navigationRuntime.requestNavigation({
|
|
355
|
-
href,
|
|
356
|
-
direction: "forward",
|
|
357
|
-
source: "browser-router"
|
|
358
|
-
});
|
|
359
|
-
return;
|
|
236
|
+
cancelNavigationTransaction() {
|
|
237
|
+
getEcoNavigationRuntime(window).cancelCurrentNavigationTransaction();
|
|
360
238
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
239
|
+
beginNavigationTransaction() {
|
|
240
|
+
const transaction = getEcoNavigationRuntime(window).beginNavigationTransaction();
|
|
241
|
+
return {
|
|
242
|
+
isStaleNavigation: () => !transaction.isCurrent(),
|
|
243
|
+
signal: transaction.signal,
|
|
244
|
+
complete: () => transaction.complete(),
|
|
245
|
+
};
|
|
367
246
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
queuedNavigationHref = this.queuedNavigationHref;
|
|
441
|
-
this.queuedNavigationHref = null;
|
|
442
|
-
}
|
|
443
|
-
if (queuedNavigationHref && queuedNavigationHref !== window.location.pathname + window.location.search) {
|
|
444
|
-
const ownerState = navigationRuntime.getOwnerState();
|
|
445
|
-
if (ownerState.owner !== "none" && ownerState.owner !== "browser-router" && ownerState.canHandleSpaNavigation) {
|
|
446
|
-
void navigationRuntime.requestNavigation({
|
|
447
|
-
href: queuedNavigationHref,
|
|
448
|
-
direction: "forward",
|
|
449
|
-
source: "browser-router"
|
|
450
|
-
});
|
|
451
|
-
} else {
|
|
452
|
-
void this.performNavigation(new URL(queuedNavigationHref, window.location.origin), "forward");
|
|
247
|
+
/**
|
|
248
|
+
* Executes the core navigation flow.
|
|
249
|
+
*
|
|
250
|
+
* Orchestrates fetching, DOM swapping, and lifecycle events:
|
|
251
|
+
*
|
|
252
|
+
* 1. **Fetch** - Retrieves HTML (from cache or network)
|
|
253
|
+
* 2. **eco:before-swap** - Allows listeners to force a full reload
|
|
254
|
+
* 3. **History update** - Updates URL before DOM swap so Web Components
|
|
255
|
+
* see the correct URL in their `connectedCallback`
|
|
256
|
+
* 4. **Stylesheet preload** - Prevents FOUC by loading styles first
|
|
257
|
+
* 5. **DOM swap** - Morphs head/body, optionally with View Transition
|
|
258
|
+
* 6. **Lifecycle events** - Dispatches `eco:after-swap` and `eco:page-load`
|
|
259
|
+
*
|
|
260
|
+
* Falls back to full page reload on network errors.
|
|
261
|
+
*
|
|
262
|
+
* @param url - The target URL to navigate to
|
|
263
|
+
* @param direction - Navigation direction ('forward', 'back', or 'replace')
|
|
264
|
+
*/
|
|
265
|
+
async performNavigation(url, direction, options = {}) {
|
|
266
|
+
const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
|
|
267
|
+
this.pendingNavigations++;
|
|
268
|
+
const { isStaleNavigation, signal, complete } = this.beginNavigationTransaction();
|
|
269
|
+
let queuedNavigationHref = null;
|
|
270
|
+
let committed = false;
|
|
271
|
+
try {
|
|
272
|
+
committed = await runPerformNavigationAttempt({
|
|
273
|
+
url,
|
|
274
|
+
direction,
|
|
275
|
+
bypassPrefetchCache: options.bypassPrefetchCache || !this.prefetchManager,
|
|
276
|
+
allowFullDocumentFallback,
|
|
277
|
+
navigationCommit: this.navigationCommit,
|
|
278
|
+
isStaleNavigation,
|
|
279
|
+
signal,
|
|
280
|
+
});
|
|
281
|
+
return committed;
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
if (shouldIgnorePerformNavigationError(error, isStaleNavigation)) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
console.error('[ecopages] Navigation failed:', error);
|
|
288
|
+
if (allowFullDocumentFallback) {
|
|
289
|
+
this.options.reloadDocument(url.href);
|
|
290
|
+
}
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
finally {
|
|
294
|
+
complete();
|
|
295
|
+
this.pendingNavigations--;
|
|
296
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
297
|
+
if (!navigationRuntime.hasPendingNavigationTransaction()) {
|
|
298
|
+
queuedNavigationHref = this.queuedNavigationHref;
|
|
299
|
+
this.queuedNavigationHref = null;
|
|
300
|
+
}
|
|
301
|
+
const ownerState = navigationRuntime.getOwnerState();
|
|
302
|
+
const replay = decideQueuedNavigationReplay({
|
|
303
|
+
queuedHref: queuedNavigationHref,
|
|
304
|
+
committedHref: window.location.href,
|
|
305
|
+
routeToCoordinator: ownerState.owner !== 'none' &&
|
|
306
|
+
ownerState.owner !== 'browser-router' &&
|
|
307
|
+
ownerState.canHandleSpaNavigation,
|
|
308
|
+
});
|
|
309
|
+
if (replay.kind === 'coordinator-navigate') {
|
|
310
|
+
void navigationRuntime.requestNavigation({
|
|
311
|
+
href: replay.href,
|
|
312
|
+
direction: 'forward',
|
|
313
|
+
source: 'browser-router',
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
else if (replay.kind === 'local-navigate') {
|
|
317
|
+
void this.performNavigation(new URL(replay.href, window.location.origin), 'forward');
|
|
318
|
+
}
|
|
453
319
|
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
/**
|
|
458
|
-
* Fetches the HTML content of a page.
|
|
459
|
-
* @param url - The URL to fetch
|
|
460
|
-
* @param signal - AbortSignal for cancelling the request
|
|
461
|
-
* @throws Error if the response is not ok
|
|
462
|
-
*/
|
|
463
|
-
async fetchPage(url, signal) {
|
|
464
|
-
if (this.prefetchManager) {
|
|
465
|
-
const cachedHtml = this.prefetchManager.getCachedHtml(url.href);
|
|
466
|
-
if (cachedHtml) {
|
|
467
|
-
return cachedHtml;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
const response = await fetch(url.href, {
|
|
471
|
-
signal,
|
|
472
|
-
headers: {
|
|
473
|
-
Accept: "text/html"
|
|
474
|
-
}
|
|
475
|
-
});
|
|
476
|
-
if (!response.ok) {
|
|
477
|
-
throw new Error(`Failed to fetch page: ${response.status}`);
|
|
478
320
|
}
|
|
479
|
-
return response.text();
|
|
480
|
-
}
|
|
481
321
|
}
|
|
482
|
-
const ACTIVE_ROUTER_KEY =
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
322
|
+
const ACTIVE_ROUTER_KEY = '__ecopages_browser_router__';
|
|
323
|
+
/**
|
|
324
|
+
* Creates and starts a router instance.
|
|
325
|
+
*
|
|
326
|
+
* Returns the router already active on the window when there is one, so options passed
|
|
327
|
+
* on later calls are ignored. Call `stop()` on the active router first to replace it with
|
|
328
|
+
* one that has different options.
|
|
329
|
+
*
|
|
330
|
+
* @remarks
|
|
331
|
+
* Re-running the layout script (for example via `data-eco-rerun` after a browser-router
|
|
332
|
+
* page commit) therefore reuses the live router instead of registering a second set of
|
|
333
|
+
* click listeners and coordinator registrations.
|
|
334
|
+
*
|
|
335
|
+
* @param options - Configuration options for the router
|
|
336
|
+
* @returns The active EcoRouter instance, started
|
|
337
|
+
*/
|
|
338
|
+
export function createRouter(options) {
|
|
339
|
+
const win = window;
|
|
340
|
+
const existingRouter = win[ACTIVE_ROUTER_KEY];
|
|
341
|
+
if (existingRouter) {
|
|
342
|
+
return existingRouter;
|
|
343
|
+
}
|
|
344
|
+
const router = new EcoRouter(options);
|
|
345
|
+
win[ACTIVE_ROUTER_KEY] = router;
|
|
346
|
+
router.start();
|
|
347
|
+
return router;
|
|
493
348
|
}
|
|
494
|
-
export {
|
|
495
|
-
EcoRouter,
|
|
496
|
-
createRouter
|
|
497
|
-
};
|