@ecopages/browser-router 0.2.0-beta.0 → 0.2.0-beta.10
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/package.json +3 -3
- package/src/client/eco-router.d.ts +0 -3
- package/src/client/eco-router.js +58 -61
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/browser-router",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.10",
|
|
4
4
|
"description": "Client-side router for Ecopages with view transitions support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -28,14 +28,14 @@
|
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|
|
31
|
-
"url": "https://github.com/ecopages/ecopages.git",
|
|
31
|
+
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
32
32
|
"directory": "packages/browser-router"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"morphdom": "^2.7.8"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@ecopages/core": "0.2.0-beta.
|
|
38
|
+
"@ecopages/core": "0.2.0-beta.10"
|
|
39
39
|
},
|
|
40
40
|
"types": "./src/index.d.ts"
|
|
41
41
|
}
|
|
@@ -13,7 +13,6 @@ export declare class EcoRouter {
|
|
|
13
13
|
private started;
|
|
14
14
|
private pendingNavigations;
|
|
15
15
|
private pendingPointerNavigation;
|
|
16
|
-
private pendingHoverNavigation;
|
|
17
16
|
private queuedNavigationHref;
|
|
18
17
|
private domSwapper;
|
|
19
18
|
private scrollManager;
|
|
@@ -23,7 +22,6 @@ export declare class EcoRouter {
|
|
|
23
22
|
private getLinkFromEvent;
|
|
24
23
|
private canInterceptLink;
|
|
25
24
|
private getRecoveredPointerHref;
|
|
26
|
-
private getRecoveredHoverHref;
|
|
27
25
|
private isAnotherNavigationRuntimeActive;
|
|
28
26
|
private getDocumentOwner;
|
|
29
27
|
private adoptDocumentOwner;
|
|
@@ -76,7 +74,6 @@ export declare class EcoRouter {
|
|
|
76
74
|
* Shadow DOM boundaries (Web Components).
|
|
77
75
|
*/
|
|
78
76
|
private handlePointerDown;
|
|
79
|
-
private handleHoverIntent;
|
|
80
77
|
private handleClick;
|
|
81
78
|
/**
|
|
82
79
|
* Handles browser back/forward navigation.
|
package/src/client/eco-router.js
CHANGED
|
@@ -12,7 +12,6 @@ class EcoRouter {
|
|
|
12
12
|
started = false;
|
|
13
13
|
pendingNavigations = 0;
|
|
14
14
|
pendingPointerNavigation = null;
|
|
15
|
-
pendingHoverNavigation = null;
|
|
16
15
|
queuedNavigationHref = null;
|
|
17
16
|
domSwapper;
|
|
18
17
|
scrollManager;
|
|
@@ -36,7 +35,6 @@ class EcoRouter {
|
|
|
36
35
|
});
|
|
37
36
|
}
|
|
38
37
|
this.handleClick = this.handleClick.bind(this);
|
|
39
|
-
this.handleHoverIntent = this.handleHoverIntent.bind(this);
|
|
40
38
|
this.handlePointerDown = this.handlePointerDown.bind(this);
|
|
41
39
|
this.handlePopState = this.handlePopState.bind(this);
|
|
42
40
|
}
|
|
@@ -72,17 +70,6 @@ class EcoRouter {
|
|
|
72
70
|
}
|
|
73
71
|
return href;
|
|
74
72
|
}
|
|
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
73
|
isAnotherNavigationRuntimeActive() {
|
|
87
74
|
const ownerState = getEcoNavigationRuntime(window).getOwnerState();
|
|
88
75
|
return ownerState.owner !== "none" && ownerState.owner !== "browser-router" && ownerState.canHandleSpaNavigation;
|
|
@@ -109,6 +96,7 @@ class EcoRouter {
|
|
|
109
96
|
* itself down.
|
|
110
97
|
*/
|
|
111
98
|
async commitDocumentNavigation(url, direction, newDocument, options = {}) {
|
|
99
|
+
const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
|
|
112
100
|
const previousUrl = new URL(window.location.href);
|
|
113
101
|
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
114
102
|
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
@@ -126,22 +114,32 @@ class EcoRouter {
|
|
|
126
114
|
}
|
|
127
115
|
};
|
|
128
116
|
document.dispatchEvent(new CustomEvent("eco:before-swap", { detail: beforeSwapEvent }));
|
|
129
|
-
if (isStaleNavigation())
|
|
117
|
+
if (isStaleNavigation()) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
130
120
|
if (shouldReload) {
|
|
131
121
|
if (shouldCleanupCurrentOwner) {
|
|
132
122
|
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
133
123
|
}
|
|
134
|
-
if (isStaleNavigation())
|
|
135
|
-
|
|
136
|
-
|
|
124
|
+
if (isStaleNavigation()) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
if (allowFullDocumentFallback) {
|
|
128
|
+
this.reloadDocument(url);
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
137
131
|
}
|
|
138
132
|
const useViewTransitions = this.options.viewTransitions;
|
|
139
133
|
await this.domSwapper.preloadStylesheets(newDocument);
|
|
140
|
-
if (isStaleNavigation())
|
|
134
|
+
if (isStaleNavigation()) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
141
137
|
if (shouldCleanupCurrentOwner) {
|
|
142
138
|
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
143
139
|
}
|
|
144
|
-
if (isStaleNavigation())
|
|
140
|
+
if (isStaleNavigation()) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
145
143
|
const commitSwap = () => {
|
|
146
144
|
if (isStaleNavigation()) return;
|
|
147
145
|
if (this.options.updateHistory && direction === "forward") {
|
|
@@ -164,7 +162,9 @@ class EcoRouter {
|
|
|
164
162
|
} else {
|
|
165
163
|
commitSwap();
|
|
166
164
|
}
|
|
167
|
-
if (isStaleNavigation())
|
|
165
|
+
if (isStaleNavigation()) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
168
|
navigationRuntime.adoptDocumentOwner(newDocument, "browser-router");
|
|
169
169
|
const afterSwapEvent = {
|
|
170
170
|
url,
|
|
@@ -183,6 +183,7 @@ class EcoRouter {
|
|
|
183
183
|
})
|
|
184
184
|
);
|
|
185
185
|
});
|
|
186
|
+
return true;
|
|
186
187
|
}
|
|
187
188
|
/**
|
|
188
189
|
* Starts the router and begins intercepting navigation.
|
|
@@ -195,10 +196,10 @@ class EcoRouter {
|
|
|
195
196
|
return;
|
|
196
197
|
}
|
|
197
198
|
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
199
|
+
navigationRuntime.cancelCurrentNavigationTransaction();
|
|
200
|
+
this.pendingNavigations = 0;
|
|
201
|
+
this.queuedNavigationHref = null;
|
|
202
|
+
this.pendingPointerNavigation = null;
|
|
202
203
|
document.addEventListener("pointerdown", this.handlePointerDown, true);
|
|
203
204
|
document.addEventListener("click", this.handleClick, true);
|
|
204
205
|
window.addEventListener("popstate", this.handlePopState);
|
|
@@ -229,12 +230,17 @@ class EcoRouter {
|
|
|
229
230
|
return true;
|
|
230
231
|
},
|
|
231
232
|
reloadCurrentPage: async (request) => {
|
|
232
|
-
if (this.pendingNavigations > 0)
|
|
233
|
+
if (this.pendingNavigations > 0) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
233
236
|
const currentUrl = window.location.pathname + window.location.search;
|
|
234
237
|
if (request?.clearCache) {
|
|
235
238
|
this.prefetchManager?.invalidate(currentUrl);
|
|
236
239
|
}
|
|
237
|
-
await this.performNavigation(new URL(currentUrl, window.location.origin), "replace"
|
|
240
|
+
return await this.performNavigation(new URL(currentUrl, window.location.origin), "replace", {
|
|
241
|
+
bypassPrefetchCache: !!request?.clearCache,
|
|
242
|
+
allowFullDocumentFallback: false
|
|
243
|
+
});
|
|
238
244
|
},
|
|
239
245
|
cleanupBeforeHandoff: async () => {
|
|
240
246
|
this.cancelNavigationTransaction();
|
|
@@ -254,10 +260,6 @@ class EcoRouter {
|
|
|
254
260
|
return;
|
|
255
261
|
}
|
|
256
262
|
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
263
|
document.removeEventListener("pointerdown", this.handlePointerDown, true);
|
|
262
264
|
document.removeEventListener("click", this.handleClick, true);
|
|
263
265
|
window.removeEventListener("popstate", this.handlePopState);
|
|
@@ -265,7 +267,6 @@ class EcoRouter {
|
|
|
265
267
|
this.unregisterNavigationRuntime?.();
|
|
266
268
|
this.unregisterNavigationRuntime = null;
|
|
267
269
|
this.started = false;
|
|
268
|
-
this.pendingHoverNavigation = null;
|
|
269
270
|
this.pendingPointerNavigation = null;
|
|
270
271
|
this.queuedNavigationHref = null;
|
|
271
272
|
const win = window;
|
|
@@ -323,29 +324,11 @@ class EcoRouter {
|
|
|
323
324
|
this.queuedNavigationHref = href;
|
|
324
325
|
}
|
|
325
326
|
}
|
|
326
|
-
handleHoverIntent(event) {
|
|
327
|
-
const link = this.getLinkFromEvent(event);
|
|
328
|
-
if (!link) {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
const href = this.canInterceptLink(event, link);
|
|
332
|
-
if (!href) {
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
this.pendingHoverNavigation = {
|
|
336
|
-
href,
|
|
337
|
-
timestamp: performance.now()
|
|
338
|
-
};
|
|
339
|
-
if (this.pendingNavigations > 0) {
|
|
340
|
-
this.queuedNavigationHref = href;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
327
|
handleClick(event) {
|
|
344
328
|
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
345
329
|
const link = this.getLinkFromEvent(event);
|
|
346
|
-
const href = link ? this.canInterceptLink(event, link) : this.getRecoveredPointerHref()
|
|
330
|
+
const href = link ? this.canInterceptLink(event, link) : this.getRecoveredPointerHref();
|
|
347
331
|
this.pendingPointerNavigation = null;
|
|
348
|
-
this.pendingHoverNavigation = null;
|
|
349
332
|
if (!href) return;
|
|
350
333
|
this.queuedNavigationHref = null;
|
|
351
334
|
if (this.isAnotherNavigationRuntimeActive()) {
|
|
@@ -412,26 +395,39 @@ class EcoRouter {
|
|
|
412
395
|
* @param url - The target URL to navigate to
|
|
413
396
|
* @param direction - Navigation direction ('forward', 'back', or 'replace')
|
|
414
397
|
*/
|
|
415
|
-
async performNavigation(url, direction) {
|
|
398
|
+
async performNavigation(url, direction, options = {}) {
|
|
399
|
+
const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
|
|
416
400
|
this.pendingNavigations++;
|
|
417
401
|
const { isStaleNavigation, signal, complete } = this.beginNavigationTransaction();
|
|
418
402
|
let queuedNavigationHref = null;
|
|
403
|
+
let committed = false;
|
|
419
404
|
try {
|
|
420
|
-
const html = await this.fetchPage(url, signal);
|
|
421
|
-
if (isStaleNavigation())
|
|
405
|
+
const html = await this.fetchPage(url, signal, { bypassCache: options.bypassPrefetchCache });
|
|
406
|
+
if (isStaleNavigation()) {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
422
409
|
const newDocument = this.domSwapper.parseHTML(html, url);
|
|
423
|
-
if (isStaleNavigation())
|
|
424
|
-
|
|
410
|
+
if (isStaleNavigation()) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
committed = await this.commitDocumentNavigation(url, direction, newDocument, {
|
|
425
414
|
html,
|
|
426
|
-
isStaleNavigation
|
|
415
|
+
isStaleNavigation,
|
|
416
|
+
allowFullDocumentFallback
|
|
427
417
|
});
|
|
418
|
+
return committed;
|
|
428
419
|
} catch (error) {
|
|
429
|
-
if (isStaleNavigation())
|
|
420
|
+
if (isStaleNavigation()) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
430
423
|
if (error instanceof Error && error.name === "AbortError") {
|
|
431
|
-
return;
|
|
424
|
+
return false;
|
|
432
425
|
}
|
|
433
426
|
console.error("[ecopages] Navigation failed:", error);
|
|
434
|
-
|
|
427
|
+
if (allowFullDocumentFallback) {
|
|
428
|
+
window.location.href = url.href;
|
|
429
|
+
}
|
|
430
|
+
return false;
|
|
435
431
|
} finally {
|
|
436
432
|
complete();
|
|
437
433
|
this.pendingNavigations--;
|
|
@@ -460,8 +456,8 @@ class EcoRouter {
|
|
|
460
456
|
* @param signal - AbortSignal for cancelling the request
|
|
461
457
|
* @throws Error if the response is not ok
|
|
462
458
|
*/
|
|
463
|
-
async fetchPage(url, signal) {
|
|
464
|
-
if (this.prefetchManager) {
|
|
459
|
+
async fetchPage(url, signal, options = {}) {
|
|
460
|
+
if (!options.bypassCache && this.prefetchManager) {
|
|
465
461
|
const cachedHtml = this.prefetchManager.getCachedHtml(url.href);
|
|
466
462
|
if (cachedHtml) {
|
|
467
463
|
return cachedHtml;
|
|
@@ -469,6 +465,7 @@ class EcoRouter {
|
|
|
469
465
|
}
|
|
470
466
|
const response = await fetch(url.href, {
|
|
471
467
|
signal,
|
|
468
|
+
cache: "no-store",
|
|
472
469
|
headers: {
|
|
473
470
|
Accept: "text/html"
|
|
474
471
|
}
|