@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
|
@@ -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,17 @@
|
|
|
1
|
+
import type { EcoNavigationEvent } from '@ecopages/core/client';
|
|
2
|
+
import type { PrefetchManager } from './services/prefetch-manager.js';
|
|
3
|
+
import type { EcoRouterOptions } from './types.js';
|
|
4
|
+
export type NavigationCommitOptions = {
|
|
5
|
+
html?: string;
|
|
6
|
+
isStaleNavigation?: () => boolean;
|
|
7
|
+
allowFullDocumentFallback?: boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
11
|
+
*/
|
|
12
|
+
export declare class NavigationCommit {
|
|
13
|
+
private readonly prefetchManager;
|
|
14
|
+
private readonly options;
|
|
15
|
+
constructor(prefetchManager: PrefetchManager | null, options: Required<EcoRouterOptions>);
|
|
16
|
+
commit(url: URL, direction: EcoNavigationEvent['direction'], newDocument: Document, options?: NavigationCommitOptions): Promise<boolean>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/client';
|
|
2
|
+
import { commitNavigationPage } from '@ecopages/core/client';
|
|
3
|
+
import { manageWindowScroll } from '@ecopages/core/client';
|
|
4
|
+
import { morphBody } from './dom/body-morpher.js';
|
|
5
|
+
import { resolveNavigationReloadDecision } from './navigation-commit-reload.js';
|
|
6
|
+
import { syncDocumentElementAttributes } from './document-element-sync.js';
|
|
7
|
+
function resolveCommitOwnerContext(navigationRuntime, newDocument) {
|
|
8
|
+
const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, 'browser-router');
|
|
9
|
+
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, 'browser-router');
|
|
10
|
+
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
11
|
+
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner &&
|
|
12
|
+
currentDocumentOwner !== 'browser-router' &&
|
|
13
|
+
activeOwner === currentDocumentOwner;
|
|
14
|
+
return { currentDocumentOwner, shouldCleanupCurrentOwner };
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
18
|
+
*/
|
|
19
|
+
export class NavigationCommit {
|
|
20
|
+
prefetchManager;
|
|
21
|
+
options;
|
|
22
|
+
constructor(prefetchManager, options) {
|
|
23
|
+
this.prefetchManager = prefetchManager;
|
|
24
|
+
this.options = options;
|
|
25
|
+
}
|
|
26
|
+
async commit(url, direction, newDocument, options = {}) {
|
|
27
|
+
const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
|
|
28
|
+
const previousUrl = new URL(window.location.href);
|
|
29
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
30
|
+
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
31
|
+
const { currentDocumentOwner, shouldCleanupCurrentOwner } = resolveCommitOwnerContext(navigationRuntime, newDocument);
|
|
32
|
+
const result = await commitNavigationPage({
|
|
33
|
+
url,
|
|
34
|
+
direction,
|
|
35
|
+
newDocument,
|
|
36
|
+
isStale: isStaleNavigation,
|
|
37
|
+
viewTransitionsEnabled: this.options.viewTransitions,
|
|
38
|
+
persistAttribute: this.options.persistAttribute,
|
|
39
|
+
onReloadRequested: async () => {
|
|
40
|
+
const reloadDecision = await resolveNavigationReloadDecision({
|
|
41
|
+
shouldCleanupCurrentOwner,
|
|
42
|
+
currentDocumentOwner,
|
|
43
|
+
navigationRuntime,
|
|
44
|
+
isStaleNavigation,
|
|
45
|
+
allowFullDocumentFallback,
|
|
46
|
+
urlHref: url.href,
|
|
47
|
+
});
|
|
48
|
+
if (reloadDecision.kind === 'reload')
|
|
49
|
+
this.options.reloadDocument(reloadDecision.href);
|
|
50
|
+
},
|
|
51
|
+
beforeTransition: async () => {
|
|
52
|
+
if (shouldCleanupCurrentOwner)
|
|
53
|
+
await navigationRuntime.cleanupOwner(currentDocumentOwner);
|
|
54
|
+
},
|
|
55
|
+
beforeCommit: () => {
|
|
56
|
+
if (this.options.updateHistory && direction === 'forward') {
|
|
57
|
+
window.history.pushState({}, '', url.href);
|
|
58
|
+
}
|
|
59
|
+
else if (direction === 'replace') {
|
|
60
|
+
window.history.replaceState({}, '', url.href);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
swap: () => {
|
|
64
|
+
syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
|
|
65
|
+
morphBody(newDocument, this.options.persistAttribute);
|
|
66
|
+
},
|
|
67
|
+
onSwapped: () => {
|
|
68
|
+
navigationRuntime.adoptDocumentOwner(newDocument, 'browser-router');
|
|
69
|
+
},
|
|
70
|
+
afterSwap: () => manageWindowScroll(url, previousUrl, {
|
|
71
|
+
scrollBehavior: this.options.scrollBehavior,
|
|
72
|
+
smoothScroll: this.options.smoothScroll,
|
|
73
|
+
}),
|
|
74
|
+
afterLifecycle: () => {
|
|
75
|
+
this.prefetchManager?.observeLinks();
|
|
76
|
+
if (options.html)
|
|
77
|
+
this.prefetchManager?.cacheVisitedPage(url.href, options.html);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return result === 'committed';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -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;
|
|
@@ -14,7 +15,7 @@ export interface PrefetchOptions {
|
|
|
14
15
|
export declare class PrefetchManager {
|
|
15
16
|
private options;
|
|
16
17
|
private prefetched;
|
|
17
|
-
private
|
|
18
|
+
private prefetchedStylesheets;
|
|
18
19
|
private observer;
|
|
19
20
|
private hoverTimeouts;
|
|
20
21
|
constructor(options: Partial<PrefetchOptions>);
|
|
@@ -41,34 +42,18 @@ export declare class PrefetchManager {
|
|
|
41
42
|
* @param href - The URL to prefetch
|
|
42
43
|
*/
|
|
43
44
|
prefetch(href: string): Promise<void>;
|
|
44
|
-
/**
|
|
45
|
-
* Retrieves cached HTML for a URL.
|
|
46
|
-
*
|
|
47
|
-
* Returns cached content without consuming it, enabling stale-while-revalidate:
|
|
48
|
-
* - Returns cached HTML immediately for instant navigation
|
|
49
|
-
* - Use cacheVisitedPage() after navigation to update cache in background
|
|
50
|
-
*
|
|
51
|
-
* @param href - The URL to look up
|
|
52
|
-
* @returns The cached HTML string, or null if not cached
|
|
53
|
-
*/
|
|
54
|
-
getCachedHtml(href: string): string | null;
|
|
55
45
|
invalidate(href: string): void;
|
|
56
46
|
/**
|
|
57
|
-
* Caches HTML
|
|
47
|
+
* Caches the HTML of a visited page so a revisit renders it immediately.
|
|
58
48
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* The cached copy is not refreshed in the background. The page on screen was just fetched or loaded, so
|
|
51
|
+
* fetching it again would add a request to every page load and navigation without a stale copy to fix.
|
|
62
52
|
*
|
|
63
53
|
* @param href - The URL of the visited page
|
|
64
|
-
* @param html - The HTML content to cache
|
|
54
|
+
* @param html - The HTML content to cache
|
|
65
55
|
*/
|
|
66
56
|
cacheVisitedPage(href: string, html: string): void;
|
|
67
|
-
/**
|
|
68
|
-
* Checks if a URL has already been prefetched.
|
|
69
|
-
* @param href - The URL to check
|
|
70
|
-
*/
|
|
71
|
-
isPrefetched(href: string): boolean;
|
|
72
57
|
/**
|
|
73
58
|
* Determines if prefetching should be enabled based on network conditions.
|
|
74
59
|
*
|
|
@@ -101,6 +86,7 @@ export declare class PrefetchManager {
|
|
|
101
86
|
* hash links, javascript: URLs).
|
|
102
87
|
*/
|
|
103
88
|
private getLinkFromEvent;
|
|
89
|
+
private isEligiblePrefetchLink;
|
|
104
90
|
/**
|
|
105
91
|
* Resolves the prefetch strategy for a link element.
|
|
106
92
|
*
|
|
@@ -141,30 +127,20 @@ export declare class PrefetchManager {
|
|
|
141
127
|
*/
|
|
142
128
|
private cancelHoverPrefetch;
|
|
143
129
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* Called once during initialization. Eager links are prefetched immediately;
|
|
147
|
-
* viewport-strategy links are registered with the IntersectionObserver.
|
|
148
|
-
*/
|
|
149
|
-
private observeExistingLinks;
|
|
150
|
-
/**
|
|
151
|
-
* Observes newly added links after DOM mutations.
|
|
152
|
-
*
|
|
153
|
-
* Should be called after client-side navigation or dynamic content updates
|
|
154
|
-
* to ensure new links are tracked for prefetching.
|
|
155
|
-
*
|
|
156
|
-
* @param root - The root element to search for links (defaults to document)
|
|
130
|
+
* Observes links for prefetching within `root`.
|
|
157
131
|
*/
|
|
158
|
-
|
|
132
|
+
observeLinks(root?: Element | Document): void;
|
|
159
133
|
/**
|
|
160
134
|
* Prefetches stylesheets discovered in HTML content.
|
|
161
135
|
*
|
|
162
|
-
* Parses the HTML to find stylesheet links, then
|
|
163
|
-
*
|
|
164
|
-
*
|
|
136
|
+
* Parses the HTML to find stylesheet links, then warms the HTTP cache for
|
|
137
|
+
* stylesheets not already present in the current document. This keeps future
|
|
138
|
+
* navigation CSS warm without injecting `preload` hints that browsers expect
|
|
139
|
+
* the current page to consume immediately.
|
|
165
140
|
*
|
|
166
141
|
* @param html - The raw HTML string to parse
|
|
167
142
|
* @param url - The base URL for resolving relative stylesheet paths
|
|
168
143
|
*/
|
|
169
144
|
private prefetchStylesheets;
|
|
145
|
+
private prefetchStylesheet;
|
|
170
146
|
}
|