@ecopages/browser-router 0.2.0-beta.4 → 0.2.0-beta.40
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 +12 -4
- package/package.json +7 -7
- 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 +24 -0
- package/src/client/dom/body-morpher.js +143 -0
- package/src/client/dom/dom-swapper.d.ts +30 -0
- package/src/client/dom/dom-swapper.js +52 -0
- package/src/client/dom/head-updater.d.ts +42 -0
- package/src/client/dom/head-updater.js +194 -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 +24 -0
- package/src/client/dom/stylesheet-preloader.d.ts +10 -0
- package/src/client/dom/stylesheet-preloader.js +44 -0
- package/src/client/eco-router.d.ts +2 -31
- package/src/client/eco-router.js +337 -439
- package/src/client/index.d.ts +3 -0
- package/src/client/index.js +3 -0
- package/src/client/navigation-commit.d.ts +22 -0
- package/src/client/navigation-commit.js +104 -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 +378 -370
- package/src/client/services/view-transition-manager.d.ts +6 -2
- package/src/client/services/view-transition-manager.js +76 -52
- package/src/client/types.d.ts +6 -13
- package/src/client/types.js +19 -20
- package/src/index.d.ts +3 -5
- package/src/index.js +8 -18
- package/src/styles.css +2 -6
- 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
|
@@ -0,0 +1,3 @@
|
|
|
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, type BrowserRouterNavigateOptions, type EcoRouterOptions, } from './types.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type EcoNavigationEvent } from '@ecopages/core/router/navigation-lifecycle';
|
|
2
|
+
import type { DomSwapper } from './dom/dom-swapper.js';
|
|
3
|
+
import type { PrefetchManager } from './services/prefetch-manager.js';
|
|
4
|
+
import type { ViewTransitionManager } from './services/view-transition-manager.js';
|
|
5
|
+
import type { EcoRouterOptions } from './types.js';
|
|
6
|
+
export type NavigationCommitOptions = {
|
|
7
|
+
html?: string;
|
|
8
|
+
isStaleNavigation?: () => boolean;
|
|
9
|
+
allowFullDocumentFallback?: boolean;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
13
|
+
*/
|
|
14
|
+
export declare class NavigationCommit {
|
|
15
|
+
private readonly domSwapper;
|
|
16
|
+
private readonly viewTransitionManager;
|
|
17
|
+
private readonly prefetchManager;
|
|
18
|
+
private readonly options;
|
|
19
|
+
constructor(domSwapper: DomSwapper, viewTransitionManager: ViewTransitionManager, prefetchManager: PrefetchManager | null, options: Required<EcoRouterOptions>);
|
|
20
|
+
commit(url: URL, direction: EcoNavigationEvent['direction'], newDocument: Document, options?: NavigationCommitOptions): Promise<boolean>;
|
|
21
|
+
private reloadDocument;
|
|
22
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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;
|
|
18
|
+
}
|
|
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, { incomingDocument: newDocument });
|
|
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;
|
|
100
|
+
}
|
|
101
|
+
reloadDocument(href) {
|
|
102
|
+
window.location.assign(href);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type FetchNavigationPageOptions = {
|
|
2
|
+
bypassCache?: boolean;
|
|
3
|
+
getCachedHtml?: (href: string) => string | null | undefined;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Fetches HTML for a client-side navigation request.
|
|
7
|
+
*/
|
|
8
|
+
export declare function fetchNavigationPage(url: URL, signal: AbortSignal, options?: FetchNavigationPageOptions): Promise<string>;
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
|
11
|
+
}
|
|
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}`);
|
|
21
|
+
}
|
|
22
|
+
await assertHtmlPageResponse(response);
|
|
23
|
+
return response.text();
|
|
24
|
+
}
|
|
@@ -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
|
*
|