@ecopages/browser-router 0.2.0-beta.31 → 0.2.0-beta.33
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 +1 -1
- package/package.json +2 -2
- package/src/client/document-element-sync.js +28 -15
- package/src/client/dom/body-morpher.js +123 -83
- package/src/client/dom/dom-swapper.js +51 -50
- package/src/client/dom/head-updater.js +177 -156
- package/src/client/dom/html-parser.js +7 -7
- package/src/client/dom/stylesheet-discovery.js +21 -17
- package/src/client/dom/stylesheet-preloader.js +42 -36
- package/src/client/eco-router.d.ts +1 -1
- package/src/client/eco-router.js +337 -329
- package/src/client/index.d.ts +1 -1
- package/src/client/index.js +3 -13
- package/src/client/navigation-commit.d.ts +2 -1
- package/src/client/navigation-commit.js +100 -110
- package/src/client/navigation-fetch.js +21 -21
- package/src/client/services/prefetch-manager.js +377 -351
- package/src/client/services/view-transition-manager.js +71 -52
- package/src/client/types.d.ts +0 -19
- package/src/client/types.js +19 -20
- package/src/index.d.ts +1 -1
- package/src/index.js +8 -18
package/src/client/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { EcoRouter, createRouter } from './eco-router.js';
|
|
2
2
|
export { syncDocumentElementAttributes } from './document-element-sync.js';
|
|
3
|
-
export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, type BrowserRouterNavigateOptions, type
|
|
3
|
+
export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, type BrowserRouterNavigateOptions, type EcoRouterOptions, } from './types.js';
|
package/src/client/index.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
5
|
-
DEFAULT_OPTIONS
|
|
6
|
-
} from "./types.js";
|
|
7
|
-
export {
|
|
8
|
-
DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
9
|
-
DEFAULT_OPTIONS,
|
|
10
|
-
EcoRouter,
|
|
11
|
-
createRouter,
|
|
12
|
-
syncDocumentElementAttributes
|
|
13
|
-
};
|
|
1
|
+
export { EcoRouter, createRouter } from './eco-router.js';
|
|
2
|
+
export { syncDocumentElementAttributes } from './document-element-sync.js';
|
|
3
|
+
export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, } from './types.js';
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type EcoNavigationEvent } from '@ecopages/core/router/navigation-lifecycle';
|
|
1
2
|
import type { DomSwapper } from './dom/dom-swapper.js';
|
|
2
3
|
import type { PrefetchManager } from './services/prefetch-manager.js';
|
|
3
4
|
import type { ViewTransitionManager } from './services/view-transition-manager.js';
|
|
4
|
-
import type {
|
|
5
|
+
import type { EcoRouterOptions } from './types.js';
|
|
5
6
|
export type NavigationCommitOptions = {
|
|
6
7
|
html?: string;
|
|
7
8
|
isStaleNavigation?: () => boolean;
|
|
@@ -1,114 +1,104 @@
|
|
|
1
|
-
import { getEcoNavigationRuntime } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
19
|
-
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
20
|
-
const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, "browser-router");
|
|
21
|
-
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, "browser-router");
|
|
22
|
-
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
23
|
-
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner && currentDocumentOwner !== "browser-router" && activeOwner === currentDocumentOwner;
|
|
24
|
-
let shouldReload = false;
|
|
25
|
-
const beforeSwapEvent = {
|
|
26
|
-
url,
|
|
27
|
-
direction,
|
|
28
|
-
newDocument,
|
|
29
|
-
reload: () => {
|
|
30
|
-
shouldReload = true;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
document.dispatchEvent(new CustomEvent("eco:before-swap", { detail: beforeSwapEvent }));
|
|
34
|
-
if (isStaleNavigation()) {
|
|
35
|
-
return false;
|
|
1
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/router/navigation-coordinator';
|
|
2
|
+
import { completeNavigationLifecycle, dispatchBeforeSwap, } from '@ecopages/core/router/navigation-lifecycle';
|
|
3
|
+
import { manageWindowScroll } from '@ecopages/core/client/scroll';
|
|
4
|
+
import { syncDocumentElementAttributes } from './document-element-sync.js';
|
|
5
|
+
/**
|
|
6
|
+
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
7
|
+
*/
|
|
8
|
+
export class NavigationCommit {
|
|
9
|
+
domSwapper;
|
|
10
|
+
viewTransitionManager;
|
|
11
|
+
prefetchManager;
|
|
12
|
+
options;
|
|
13
|
+
constructor(domSwapper, viewTransitionManager, prefetchManager, options) {
|
|
14
|
+
this.domSwapper = domSwapper;
|
|
15
|
+
this.viewTransitionManager = viewTransitionManager;
|
|
16
|
+
this.prefetchManager = prefetchManager;
|
|
17
|
+
this.options = options;
|
|
36
18
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
19
|
+
async commit(url, direction, newDocument, options = {}) {
|
|
20
|
+
const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
|
|
21
|
+
const previousUrl = new URL(window.location.href);
|
|
22
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
23
|
+
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
24
|
+
const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, 'browser-router');
|
|
25
|
+
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, 'browser-router');
|
|
26
|
+
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
27
|
+
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner &&
|
|
28
|
+
currentDocumentOwner !== 'browser-router' &&
|
|
29
|
+
activeOwner === currentDocumentOwner;
|
|
30
|
+
const { requestedReload } = dispatchBeforeSwap(document, {
|
|
31
|
+
url,
|
|
32
|
+
direction,
|
|
33
|
+
newDocument,
|
|
34
|
+
});
|
|
35
|
+
if (isStaleNavigation()) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (requestedReload) {
|
|
39
|
+
if (shouldCleanupCurrentOwner) {
|
|
40
|
+
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
41
|
+
}
|
|
42
|
+
if (isStaleNavigation()) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (allowFullDocumentFallback) {
|
|
46
|
+
this.reloadDocument(url.href);
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const useViewTransitions = this.options.viewTransitions;
|
|
51
|
+
await this.domSwapper.preloadStylesheets(newDocument);
|
|
52
|
+
if (isStaleNavigation()) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
if (shouldCleanupCurrentOwner) {
|
|
56
|
+
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
57
|
+
}
|
|
58
|
+
if (isStaleNavigation()) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
const commitSwap = () => {
|
|
62
|
+
if (isStaleNavigation())
|
|
63
|
+
return;
|
|
64
|
+
if (this.options.updateHistory && direction === 'forward') {
|
|
65
|
+
window.history.pushState({}, '', url.href);
|
|
66
|
+
}
|
|
67
|
+
else if (direction === 'replace') {
|
|
68
|
+
window.history.replaceState({}, '', url.href);
|
|
69
|
+
}
|
|
70
|
+
syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
|
|
71
|
+
const { bodyStrategy } = this.domSwapper.morphHead(newDocument);
|
|
72
|
+
if (useViewTransitions && bodyStrategy === 'morph') {
|
|
73
|
+
this.domSwapper.morphBody(newDocument);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
this.domSwapper.replaceBody(newDocument);
|
|
77
|
+
}
|
|
78
|
+
this.domSwapper.flushRerunScripts();
|
|
79
|
+
manageWindowScroll(url, previousUrl, {
|
|
80
|
+
scrollBehavior: this.options.scrollBehavior,
|
|
81
|
+
smoothScroll: this.options.smoothScroll,
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
if (useViewTransitions) {
|
|
85
|
+
await this.viewTransitionManager.transition(commitSwap);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
commitSwap();
|
|
89
|
+
}
|
|
90
|
+
if (isStaleNavigation()) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
navigationRuntime.adoptDocumentOwner(newDocument, 'browser-router');
|
|
94
|
+
completeNavigationLifecycle(document, { url, direction }, { isStale: isStaleNavigation });
|
|
95
|
+
this.prefetchManager?.observeLinks();
|
|
96
|
+
if (options.html) {
|
|
97
|
+
this.prefetchManager?.cacheVisitedPage(url.href, options.html);
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
48
100
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (isStaleNavigation()) {
|
|
52
|
-
return false;
|
|
101
|
+
reloadDocument(href) {
|
|
102
|
+
window.location.assign(href);
|
|
53
103
|
}
|
|
54
|
-
if (shouldCleanupCurrentOwner) {
|
|
55
|
-
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
56
|
-
}
|
|
57
|
-
if (isStaleNavigation()) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
const commitSwap = () => {
|
|
61
|
-
if (isStaleNavigation()) return;
|
|
62
|
-
if (this.options.updateHistory && direction === "forward") {
|
|
63
|
-
window.history.pushState({}, "", url.href);
|
|
64
|
-
} else if (direction === "replace") {
|
|
65
|
-
window.history.replaceState({}, "", url.href);
|
|
66
|
-
}
|
|
67
|
-
syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
|
|
68
|
-
const { bodyStrategy } = this.domSwapper.morphHead(newDocument);
|
|
69
|
-
if (useViewTransitions && bodyStrategy === "morph") {
|
|
70
|
-
this.domSwapper.morphBody(newDocument);
|
|
71
|
-
} else {
|
|
72
|
-
this.domSwapper.replaceBody(newDocument);
|
|
73
|
-
}
|
|
74
|
-
this.domSwapper.flushRerunScripts();
|
|
75
|
-
manageWindowScroll(url, previousUrl, {
|
|
76
|
-
scrollBehavior: this.options.scrollBehavior,
|
|
77
|
-
smoothScroll: this.options.smoothScroll
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
if (useViewTransitions) {
|
|
81
|
-
await this.viewTransitionManager.transition(commitSwap);
|
|
82
|
-
} else {
|
|
83
|
-
commitSwap();
|
|
84
|
-
}
|
|
85
|
-
if (isStaleNavigation()) {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
navigationRuntime.adoptDocumentOwner(newDocument, "browser-router");
|
|
89
|
-
const afterSwapEvent = {
|
|
90
|
-
url,
|
|
91
|
-
direction
|
|
92
|
-
};
|
|
93
|
-
document.dispatchEvent(new CustomEvent("eco:after-swap", { detail: afterSwapEvent }));
|
|
94
|
-
this.prefetchManager?.observeLinks();
|
|
95
|
-
if (options.html) {
|
|
96
|
-
this.prefetchManager?.cacheVisitedPage(url.href, options.html);
|
|
97
|
-
}
|
|
98
|
-
requestAnimationFrame(() => {
|
|
99
|
-
if (isStaleNavigation()) return;
|
|
100
|
-
document.dispatchEvent(
|
|
101
|
-
new CustomEvent("eco:page-load", {
|
|
102
|
-
detail: { url, direction }
|
|
103
|
-
})
|
|
104
|
-
);
|
|
105
|
-
});
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
reloadDocument(href) {
|
|
109
|
-
window.location.assign(href);
|
|
110
|
-
}
|
|
111
104
|
}
|
|
112
|
-
export {
|
|
113
|
-
NavigationCommit
|
|
114
|
-
};
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { assertHtmlPageResponse } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { assertHtmlPageResponse } from '@ecopages/core/router/link-navigation-policy';
|
|
2
|
+
/**
|
|
3
|
+
* Fetches HTML for a client-side navigation request.
|
|
4
|
+
*/
|
|
5
|
+
export async function fetchNavigationPage(url, signal, options = {}) {
|
|
6
|
+
if (!options.bypassCache && options.getCachedHtml) {
|
|
7
|
+
const cachedHtml = options.getCachedHtml(url.href);
|
|
8
|
+
if (cachedHtml) {
|
|
9
|
+
return cachedHtml;
|
|
10
|
+
}
|
|
7
11
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
const response = await fetch(url.href, {
|
|
13
|
+
signal,
|
|
14
|
+
cache: 'no-store',
|
|
15
|
+
headers: {
|
|
16
|
+
Accept: 'text/html',
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (!response.ok) {
|
|
20
|
+
throw new Error(`Failed to fetch page: ${response.status}`);
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
throw new Error(`Failed to fetch page: ${response.status}`);
|
|
18
|
-
}
|
|
19
|
-
await assertHtmlPageResponse(response);
|
|
20
|
-
return response.text();
|
|
22
|
+
await assertHtmlPageResponse(response);
|
|
23
|
+
return response.text();
|
|
21
24
|
}
|
|
22
|
-
export {
|
|
23
|
-
fetchNavigationPage
|
|
24
|
-
};
|