@ecopages/browser-router 0.2.1 → 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.
Files changed (40) hide show
  1. package/README.md +25 -8
  2. package/package.json +11 -8
  3. package/src/client/document-element-sync.d.ts +0 -7
  4. package/src/client/document-element-sync.js +28 -18
  5. package/src/client/dom/body-morpher.d.ts +27 -0
  6. package/src/client/dom/body-morpher.js +166 -0
  7. package/src/client/dom/dom-swapper.d.ts +7 -0
  8. package/src/client/dom/dom-swapper.js +15 -0
  9. package/src/client/dom/stylesheet-discovery.d.ts +12 -0
  10. package/src/client/dom/stylesheet-discovery.js +24 -0
  11. package/src/client/eco-router-perform-navigation.d.ts +13 -0
  12. package/src/client/eco-router-perform-navigation.js +24 -0
  13. package/src/client/eco-router.d.ts +2 -35
  14. package/src/client/eco-router.js +329 -479
  15. package/src/client/index.d.ts +3 -0
  16. package/src/client/index.js +3 -0
  17. package/src/client/navigation-commit-reload.d.ts +21 -0
  18. package/src/client/navigation-commit-reload.js +16 -0
  19. package/src/client/navigation-commit.d.ts +19 -0
  20. package/src/client/navigation-commit.js +83 -0
  21. package/src/client/services/prefetch-manager.d.ts +10 -18
  22. package/src/client/services/prefetch-manager.js +353 -360
  23. package/src/client/types.d.ts +16 -13
  24. package/src/client/types.js +21 -20
  25. package/src/index.d.ts +2 -5
  26. package/src/index.js +7 -18
  27. package/CHANGELOG.md +0 -25
  28. package/src/client/services/dom-swapper.d.ts +0 -166
  29. package/src/client/services/dom-swapper.js +0 -497
  30. package/src/client/services/index.d.ts +0 -8
  31. package/src/client/services/index.js +0 -10
  32. package/src/client/services/scroll-manager.d.ts +0 -19
  33. package/src/client/services/scroll-manager.js +0 -36
  34. package/src/client/services/view-transition-manager.d.ts +0 -29
  35. package/src/client/services/view-transition-manager.js +0 -54
  36. package/src/client/view-transition-utils.d.ts +0 -14
  37. package/src/client/view-transition-utils.js +0 -60
  38. package/src/styles.css +0 -218
  39. package/src/types.d.ts +0 -15
  40. 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,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, } 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;
@@ -14,7 +15,7 @@ export interface PrefetchOptions {
14
15
  export declare class PrefetchManager {
15
16
  private options;
16
17
  private prefetched;
17
- private htmlCache;
18
+ private prefetchedStylesheets;
18
19
  private observer;
19
20
  private hoverTimeouts;
20
21
  constructor(options: Partial<PrefetchOptions>);
@@ -101,6 +102,7 @@ export declare class PrefetchManager {
101
102
  * hash links, javascript: URLs).
102
103
  */
103
104
  private getLinkFromEvent;
105
+ private isEligiblePrefetchLink;
104
106
  /**
105
107
  * Resolves the prefetch strategy for a link element.
106
108
  *
@@ -141,30 +143,20 @@ export declare class PrefetchManager {
141
143
  */
142
144
  private cancelHoverPrefetch;
143
145
  /**
144
- * Begins observing all existing links on the page.
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)
146
+ * Observes links for prefetching within `root`.
157
147
  */
158
- observeNewLinks(root?: Element | Document): void;
148
+ observeLinks(root?: Element | Document): void;
159
149
  /**
160
150
  * Prefetches stylesheets discovered in HTML content.
161
151
  *
162
- * Parses the HTML to find stylesheet links, then creates preload hints
163
- * for stylesheets not already present in the current document. This ensures
164
- * styles are cached before navigation to prevent FOUC.
152
+ * Parses the HTML to find stylesheet links, then warms the HTTP cache for
153
+ * stylesheets not already present in the current document. This keeps future
154
+ * navigation CSS warm without injecting `preload` hints that browsers expect
155
+ * the current page to consume immediately.
165
156
  *
166
157
  * @param html - The raw HTML string to parse
167
158
  * @param url - The base URL for resolving relative stylesheet paths
168
159
  */
169
160
  private prefetchStylesheets;
161
+ private prefetchStylesheet;
170
162
  }