@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
|
@@ -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,21 @@
|
|
|
1
|
+
import type { EcoNavigationOwner } from '@ecopages/core/client';
|
|
2
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/client';
|
|
3
|
+
export type NavigationReloadDecision = {
|
|
4
|
+
kind: 'stale';
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'reload';
|
|
7
|
+
href: string;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'aborted';
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Cleans up the outgoing document owner and decides whether to hard-reload.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveNavigationReloadDecision(input: {
|
|
15
|
+
shouldCleanupCurrentOwner: boolean;
|
|
16
|
+
currentDocumentOwner: EcoNavigationOwner;
|
|
17
|
+
navigationRuntime: ReturnType<typeof getEcoNavigationRuntime>;
|
|
18
|
+
isStaleNavigation: () => boolean;
|
|
19
|
+
allowFullDocumentFallback: boolean;
|
|
20
|
+
urlHref: string;
|
|
21
|
+
}): Promise<NavigationReloadDecision>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/client';
|
|
2
|
+
/**
|
|
3
|
+
* Cleans up the outgoing document owner and decides whether to hard-reload.
|
|
4
|
+
*/
|
|
5
|
+
export async function resolveNavigationReloadDecision(input) {
|
|
6
|
+
if (input.shouldCleanupCurrentOwner) {
|
|
7
|
+
await input.navigationRuntime.cleanupOwner(input.currentDocumentOwner);
|
|
8
|
+
}
|
|
9
|
+
if (input.isStaleNavigation()) {
|
|
10
|
+
return { kind: 'stale' };
|
|
11
|
+
}
|
|
12
|
+
if (input.allowFullDocumentFallback) {
|
|
13
|
+
return { kind: 'reload', href: input.urlHref };
|
|
14
|
+
}
|
|
15
|
+
return { kind: 'aborted' };
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EcoNavigationEvent } from '@ecopages/core/client';
|
|
2
|
+
import type { DomSwapper } from './dom/dom-swapper.js';
|
|
3
|
+
import type { PrefetchManager } from './services/prefetch-manager.js';
|
|
4
|
+
import type { EcoRouterOptions } from './types.js';
|
|
5
|
+
export type NavigationCommitOptions = {
|
|
6
|
+
html?: string;
|
|
7
|
+
isStaleNavigation?: () => boolean;
|
|
8
|
+
allowFullDocumentFallback?: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
12
|
+
*/
|
|
13
|
+
export declare class NavigationCommit {
|
|
14
|
+
private readonly domSwapper;
|
|
15
|
+
private readonly prefetchManager;
|
|
16
|
+
private readonly options;
|
|
17
|
+
constructor(domSwapper: DomSwapper, prefetchManager: PrefetchManager | null, options: Required<EcoRouterOptions>);
|
|
18
|
+
commit(url: URL, direction: EcoNavigationEvent['direction'], newDocument: Document, options?: NavigationCommitOptions): Promise<boolean>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/client';
|
|
2
|
+
import { commitNavigationPage } from '@ecopages/core/client';
|
|
3
|
+
import { manageWindowScroll } from '@ecopages/core/client';
|
|
4
|
+
import { resolveNavigationReloadDecision } from './navigation-commit-reload.js';
|
|
5
|
+
import { syncDocumentElementAttributes } from './document-element-sync.js';
|
|
6
|
+
function resolveCommitOwnerContext(navigationRuntime, newDocument) {
|
|
7
|
+
const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, 'browser-router');
|
|
8
|
+
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, 'browser-router');
|
|
9
|
+
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
10
|
+
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner &&
|
|
11
|
+
currentDocumentOwner !== 'browser-router' &&
|
|
12
|
+
activeOwner === currentDocumentOwner;
|
|
13
|
+
return { currentDocumentOwner, shouldCleanupCurrentOwner };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
17
|
+
*/
|
|
18
|
+
export class NavigationCommit {
|
|
19
|
+
domSwapper;
|
|
20
|
+
prefetchManager;
|
|
21
|
+
options;
|
|
22
|
+
constructor(domSwapper, prefetchManager, options) {
|
|
23
|
+
this.domSwapper = domSwapper;
|
|
24
|
+
this.prefetchManager = prefetchManager;
|
|
25
|
+
this.options = options;
|
|
26
|
+
}
|
|
27
|
+
async commit(url, direction, newDocument, options = {}) {
|
|
28
|
+
const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
|
|
29
|
+
const previousUrl = new URL(window.location.href);
|
|
30
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
31
|
+
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
32
|
+
const { currentDocumentOwner, shouldCleanupCurrentOwner } = resolveCommitOwnerContext(navigationRuntime, newDocument);
|
|
33
|
+
const result = await commitNavigationPage({
|
|
34
|
+
url,
|
|
35
|
+
direction,
|
|
36
|
+
newDocument,
|
|
37
|
+
isStale: isStaleNavigation,
|
|
38
|
+
viewTransitionsEnabled: this.options.viewTransitions,
|
|
39
|
+
persistAttribute: this.options.persistAttribute,
|
|
40
|
+
onReloadRequested: async () => {
|
|
41
|
+
const reloadDecision = await resolveNavigationReloadDecision({
|
|
42
|
+
shouldCleanupCurrentOwner,
|
|
43
|
+
currentDocumentOwner,
|
|
44
|
+
navigationRuntime,
|
|
45
|
+
isStaleNavigation,
|
|
46
|
+
allowFullDocumentFallback,
|
|
47
|
+
urlHref: url.href,
|
|
48
|
+
});
|
|
49
|
+
if (reloadDecision.kind === 'reload')
|
|
50
|
+
this.options.reloadDocument(reloadDecision.href);
|
|
51
|
+
},
|
|
52
|
+
beforeTransition: async () => {
|
|
53
|
+
if (shouldCleanupCurrentOwner)
|
|
54
|
+
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
55
|
+
},
|
|
56
|
+
beforeCommit: () => {
|
|
57
|
+
if (this.options.updateHistory && direction === 'forward') {
|
|
58
|
+
window.history.pushState({}, '', url.href);
|
|
59
|
+
}
|
|
60
|
+
else if (direction === 'replace') {
|
|
61
|
+
window.history.replaceState({}, '', url.href);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
swap: () => {
|
|
65
|
+
syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
|
|
66
|
+
this.domSwapper.morphBody(newDocument);
|
|
67
|
+
},
|
|
68
|
+
onSwapped: () => {
|
|
69
|
+
navigationRuntime.adoptDocumentOwner(newDocument, 'browser-router');
|
|
70
|
+
},
|
|
71
|
+
afterSwap: () => manageWindowScroll(url, previousUrl, {
|
|
72
|
+
scrollBehavior: this.options.scrollBehavior,
|
|
73
|
+
smoothScroll: this.options.smoothScroll,
|
|
74
|
+
}),
|
|
75
|
+
afterLifecycle: () => {
|
|
76
|
+
this.prefetchManager?.observeLinks();
|
|
77
|
+
if (options.html)
|
|
78
|
+
this.prefetchManager?.cacheVisitedPage(url.href, options.html);
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
return result === 'committed';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -7,6 +7,7 @@ export type PrefetchStrategy = 'viewport' | 'hover' | 'intent';
|
|
|
7
7
|
export interface PrefetchOptions {
|
|
8
8
|
strategy: PrefetchStrategy;
|
|
9
9
|
delay: number;
|
|
10
|
+
reloadAttribute: string;
|
|
10
11
|
noPrefetchAttribute: string;
|
|
11
12
|
respectDataSaver: boolean;
|
|
12
13
|
linkSelector: string;
|
|
@@ -15,7 +16,6 @@ export declare class PrefetchManager {
|
|
|
15
16
|
private options;
|
|
16
17
|
private prefetched;
|
|
17
18
|
private prefetchedStylesheets;
|
|
18
|
-
private htmlCache;
|
|
19
19
|
private observer;
|
|
20
20
|
private hoverTimeouts;
|
|
21
21
|
constructor(options: Partial<PrefetchOptions>);
|
|
@@ -102,6 +102,7 @@ export declare class PrefetchManager {
|
|
|
102
102
|
* hash links, javascript: URLs).
|
|
103
103
|
*/
|
|
104
104
|
private getLinkFromEvent;
|
|
105
|
+
private isEligiblePrefetchLink;
|
|
105
106
|
/**
|
|
106
107
|
* Resolves the prefetch strategy for a link element.
|
|
107
108
|
*
|
|
@@ -142,21 +143,9 @@ export declare class PrefetchManager {
|
|
|
142
143
|
*/
|
|
143
144
|
private cancelHoverPrefetch;
|
|
144
145
|
/**
|
|
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)
|
|
146
|
+
* Observes links for prefetching within `root`.
|
|
158
147
|
*/
|
|
159
|
-
|
|
148
|
+
observeLinks(root?: Element | Document): void;
|
|
160
149
|
/**
|
|
161
150
|
* Prefetches stylesheets discovered in HTML content.
|
|
162
151
|
*
|