@ecopages/browser-router 0.3.0 → 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 CHANGED
@@ -10,7 +10,9 @@ Client-side navigation and view transitions for Ecopages. It intercepts same-ori
10
10
  - **View Transitions**: Optional integration with the View Transition API.
11
11
  - **Lifecycle events**: Hook into navigation with `eco:before-swap`, `eco:after-swap`, `eco:page-load`.
12
12
 
13
- The core navigation pipeline shares document fetching, parsing, prefetch cache, head reconciliation, stylesheet staging, script activation, named view transitions, and lifecycle events with `@ecopages/react-router`. Browser-router always morphs the incoming body, preserving persisted elements across navigation regardless of view-transition settings or rerun scripts. View transitions only wrap the morph; scroll restoration uses the shared post-swap hook.
13
+ The core navigation pipeline shares document fetching, parsing, head reconciliation, stylesheet staging, script activation, named view transitions, and lifecycle events with `@ecopages/react-router`. Browser-router always morphs the incoming body, preserving persisted elements across navigation regardless of view-transition settings or rerun scripts. View transitions only wrap the morph; scroll restoration uses the shared post-swap hook.
14
+
15
+ Browser-router keeps its own cache of prefetched and visited pages: a revisit renders the cached HTML immediately, and the cached copy is not refreshed in the background. Set `prefetch: false` to turn the cache off, so every navigation fetches fresh HTML. `@ecopages/react-router` does not read this cache and always fetches fresh HTML.
14
16
 
15
17
  ## Compatibility
16
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/browser-router",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Client-side router for Ecopages with view transitions support",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -38,7 +38,7 @@
38
38
  "morphdom": "^2.7.8"
39
39
  },
40
40
  "peerDependencies": {
41
- "@ecopages/core": "0.3.0"
41
+ "@ecopages/core": "0.3.1"
42
42
  },
43
43
  "types": "./src/index.d.ts"
44
44
  }
@@ -9,5 +9,12 @@ export type PerformNavigationAttemptInput = {
9
9
  isStaleNavigation: () => boolean;
10
10
  signal: AbortSignal;
11
11
  };
12
+ /**
13
+ * Fetches the target page and commits it.
14
+ *
15
+ * @remarks
16
+ * The shared navigation cache outlives the router, so a router without a prefetch manager must set
17
+ * `bypassPrefetchCache`. Otherwise the first fetched copy of a page is served for the rest of the session.
18
+ */
12
19
  export declare function runPerformNavigationAttempt(input: PerformNavigationAttemptInput): Promise<boolean>;
13
20
  export declare function shouldIgnorePerformNavigationError(error: unknown, isStaleNavigation: () => boolean): boolean;
@@ -1,4 +1,11 @@
1
1
  import { fetchNavigationPage } from '@ecopages/core/client';
2
+ /**
3
+ * Fetches the target page and commits it.
4
+ *
5
+ * @remarks
6
+ * The shared navigation cache outlives the router, so a router without a prefetch manager must set
7
+ * `bypassPrefetchCache`. Otherwise the first fetched copy of a page is served for the rest of the session.
8
+ */
2
9
  export async function runPerformNavigationAttempt(input) {
3
10
  const page = await fetchNavigationPage(input.url, {
4
11
  signal: input.signal,
@@ -14,7 +14,6 @@ export declare class EcoRouter {
14
14
  private pendingNavigations;
15
15
  private pendingPointerNavigation;
16
16
  private queuedNavigationHref;
17
- private domSwapper;
18
17
  private prefetchManager;
19
18
  private readonly navigationCommit;
20
19
  constructor(options?: EcoRouterOptions);
@@ -88,12 +87,16 @@ export declare class EcoRouter {
88
87
  /**
89
88
  * Creates and starts a router instance.
90
89
  *
91
- * Stops the previously active router (if any) before creating a new one so
92
- * click listeners and coordinator registrations from earlier instances are
93
- * cleaned up on re-execution (e.g. when the layout script is re-run via
94
- * `data-eco-rerun` after a browser-router page commit).
90
+ * Returns the router already active on the window when there is one, so options passed
91
+ * on later calls are ignored. Call `stop()` on the active router first to replace it with
92
+ * one that has different options.
93
+ *
94
+ * @remarks
95
+ * Re-running the layout script (for example via `data-eco-rerun` after a browser-router
96
+ * page commit) therefore reuses the live router instead of registering a second set of
97
+ * click listeners and coordinator registrations.
95
98
  *
96
99
  * @param options - Configuration options for the router
97
- * @returns A started EcoRouter instance
100
+ * @returns The active EcoRouter instance, started
98
101
  */
99
102
  export declare function createRouter(options?: EcoRouterOptions): EcoRouter;
@@ -7,7 +7,6 @@ import { getAnchorFromNavigationEvent, isStaticAssetHref, recoverPendingNavigati
7
7
  import { getNavigableHrefFromClick } from '@ecopages/core/client';
8
8
  import { decideQueuedNavigationReplay } from '@ecopages/core/client';
9
9
  import { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from './types.js';
10
- import { DomSwapper } from './dom/dom-swapper.js';
11
10
  import { PrefetchManager } from './services/prefetch-manager.js';
12
11
  import { NavigationCommit } from './navigation-commit.js';
13
12
  import { runPerformNavigationAttempt, shouldIgnorePerformNavigationError } from './eco-router-perform-navigation.js';
@@ -22,7 +21,6 @@ export class EcoRouter {
22
21
  pendingNavigations = 0;
23
22
  pendingPointerNavigation = null;
24
23
  queuedNavigationHref = null;
25
- domSwapper;
26
24
  prefetchManager = null;
27
25
  navigationCommit;
28
26
  constructor(options = {}) {
@@ -33,7 +31,6 @@ export class EcoRouter {
33
31
  ...(options.documentElementAttributesToSync ?? DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC),
34
32
  ],
35
33
  };
36
- this.domSwapper = new DomSwapper(this.options.persistAttribute);
37
34
  if (this.options.prefetch !== false) {
38
35
  this.prefetchManager = new PrefetchManager({
39
36
  ...this.options.prefetch,
@@ -44,7 +41,7 @@ export class EcoRouter {
44
41
  this.handleClick = this.handleClick.bind(this);
45
42
  this.handlePointerDown = this.handlePointerDown.bind(this);
46
43
  this.handlePopState = this.handlePopState.bind(this);
47
- this.navigationCommit = new NavigationCommit(this.domSwapper, this.prefetchManager, this.options);
44
+ this.navigationCommit = new NavigationCommit(this.prefetchManager, this.options);
48
45
  }
49
46
  getRecoveredPointerHref() {
50
47
  const href = recoverPendingNavigationHref(this.pendingPointerNavigation, this.pendingNavigations > 0, performance.now());
@@ -275,7 +272,7 @@ export class EcoRouter {
275
272
  committed = await runPerformNavigationAttempt({
276
273
  url,
277
274
  direction,
278
- bypassPrefetchCache: options.bypassPrefetchCache,
275
+ bypassPrefetchCache: options.bypassPrefetchCache || !this.prefetchManager,
279
276
  allowFullDocumentFallback,
280
277
  navigationCommit: this.navigationCommit,
281
278
  isStaleNavigation,
@@ -326,13 +323,17 @@ const ACTIVE_ROUTER_KEY = '__ecopages_browser_router__';
326
323
  /**
327
324
  * Creates and starts a router instance.
328
325
  *
329
- * Stops the previously active router (if any) before creating a new one so
330
- * click listeners and coordinator registrations from earlier instances are
331
- * cleaned up on re-execution (e.g. when the layout script is re-run via
332
- * `data-eco-rerun` after a browser-router page commit).
326
+ * Returns the router already active on the window when there is one, so options passed
327
+ * on later calls are ignored. Call `stop()` on the active router first to replace it with
328
+ * one that has different options.
329
+ *
330
+ * @remarks
331
+ * Re-running the layout script (for example via `data-eco-rerun` after a browser-router
332
+ * page commit) therefore reuses the live router instead of registering a second set of
333
+ * click listeners and coordinator registrations.
333
334
  *
334
335
  * @param options - Configuration options for the router
335
- * @returns A started EcoRouter instance
336
+ * @returns The active EcoRouter instance, started
336
337
  */
337
338
  export function createRouter(options) {
338
339
  const win = window;
@@ -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 EcoRouterOptions, } from './types.js';
3
+ export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, type EcoRouterOptions } from './types.js';
@@ -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, } from './types.js';
3
+ export { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS } from './types.js';
@@ -1,5 +1,4 @@
1
1
  import type { EcoNavigationEvent } from '@ecopages/core/client';
2
- import type { DomSwapper } from './dom/dom-swapper.js';
3
2
  import type { PrefetchManager } from './services/prefetch-manager.js';
4
3
  import type { EcoRouterOptions } from './types.js';
5
4
  export type NavigationCommitOptions = {
@@ -11,9 +10,8 @@ export type NavigationCommitOptions = {
11
10
  * Commits a fetched document into the live page and dispatches navigation lifecycle events.
12
11
  */
13
12
  export declare class NavigationCommit {
14
- private readonly domSwapper;
15
13
  private readonly prefetchManager;
16
14
  private readonly options;
17
- constructor(domSwapper: DomSwapper, prefetchManager: PrefetchManager | null, options: Required<EcoRouterOptions>);
15
+ constructor(prefetchManager: PrefetchManager | null, options: Required<EcoRouterOptions>);
18
16
  commit(url: URL, direction: EcoNavigationEvent['direction'], newDocument: Document, options?: NavigationCommitOptions): Promise<boolean>;
19
17
  }
@@ -1,6 +1,7 @@
1
1
  import { getEcoNavigationRuntime } from '@ecopages/core/client';
2
2
  import { commitNavigationPage } from '@ecopages/core/client';
3
3
  import { manageWindowScroll } from '@ecopages/core/client';
4
+ import { morphBody } from './dom/body-morpher.js';
4
5
  import { resolveNavigationReloadDecision } from './navigation-commit-reload.js';
5
6
  import { syncDocumentElementAttributes } from './document-element-sync.js';
6
7
  function resolveCommitOwnerContext(navigationRuntime, newDocument) {
@@ -16,11 +17,9 @@ function resolveCommitOwnerContext(navigationRuntime, newDocument) {
16
17
  * Commits a fetched document into the live page and dispatches navigation lifecycle events.
17
18
  */
18
19
  export class NavigationCommit {
19
- domSwapper;
20
20
  prefetchManager;
21
21
  options;
22
- constructor(domSwapper, prefetchManager, options) {
23
- this.domSwapper = domSwapper;
22
+ constructor(prefetchManager, options) {
24
23
  this.prefetchManager = prefetchManager;
25
24
  this.options = options;
26
25
  }
@@ -63,7 +62,7 @@ export class NavigationCommit {
63
62
  },
64
63
  swap: () => {
65
64
  syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
66
- this.domSwapper.morphBody(newDocument);
65
+ morphBody(newDocument, this.options.persistAttribute);
67
66
  },
68
67
  onSwapped: () => {
69
68
  navigationRuntime.adoptDocumentOwner(newDocument, 'browser-router');
@@ -42,34 +42,18 @@ export declare class PrefetchManager {
42
42
  * @param href - The URL to prefetch
43
43
  */
44
44
  prefetch(href: string): Promise<void>;
45
- /**
46
- * Retrieves cached HTML for a URL.
47
- *
48
- * Returns cached content without consuming it, enabling stale-while-revalidate:
49
- * - Returns cached HTML immediately for instant navigation
50
- * - Use cacheVisitedPage() after navigation to update cache in background
51
- *
52
- * @param href - The URL to look up
53
- * @returns The cached HTML string, or null if not cached
54
- */
55
- getCachedHtml(href: string): string | null;
56
45
  invalidate(href: string): void;
57
46
  /**
58
- * Caches HTML content for a visited page and triggers background revalidation.
47
+ * Caches the HTML of a visited page so a revisit renders it immediately.
59
48
  *
60
- * Implements stale-while-revalidate pattern:
61
- * - Immediately caches the provided HTML for instant revisits
62
- * - Fetches fresh content in background for next visit (unless it's the current page)
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.
63
52
  *
64
53
  * @param href - The URL of the visited page
65
- * @param html - The HTML content to cache initially
54
+ * @param html - The HTML content to cache
66
55
  */
67
56
  cacheVisitedPage(href: string, html: string): void;
68
- /**
69
- * Checks if a URL has already been prefetched.
70
- * @param href - The URL to check
71
- */
72
- isPrefetched(href: string): boolean;
73
57
  /**
74
58
  * Determines if prefetching should be enabled based on network conditions.
75
59
  *
@@ -3,7 +3,7 @@
3
3
  * Uses a single IntersectionObserver and hover detection for optimal performance.
4
4
  * @module prefetch-manager
5
5
  */
6
- import { cacheNavigationHtml, fetchNavigationPage, getCachedNavigationHtml, invalidateNavigationHtml, } from '@ecopages/core/client';
6
+ import { cacheNavigationHtml, fetchNavigationPage, invalidateNavigationHtml } from '@ecopages/core/client';
7
7
  import { isSamePageHref, shouldPrefetchLink } from '@ecopages/core/client';
8
8
  import { discoverNewStylesheetLinks, getCurrentStylesheetHrefs } from '../dom/stylesheet-discovery.js';
9
9
  const DEFAULT_PREFETCH_OPTIONS = {
@@ -85,33 +85,20 @@ export class PrefetchManager {
85
85
  this.prefetched.delete(url.href);
86
86
  }
87
87
  }
88
- /**
89
- * Retrieves cached HTML for a URL.
90
- *
91
- * Returns cached content without consuming it, enabling stale-while-revalidate:
92
- * - Returns cached HTML immediately for instant navigation
93
- * - Use cacheVisitedPage() after navigation to update cache in background
94
- *
95
- * @param href - The URL to look up
96
- * @returns The cached HTML string, or null if not cached
97
- */
98
- getCachedHtml(href) {
99
- return getCachedNavigationHtml(href);
100
- }
101
88
  invalidate(href) {
102
89
  const url = new URL(href, window.location.origin);
103
90
  invalidateNavigationHtml(url);
104
91
  this.prefetched.delete(url.href);
105
92
  }
106
93
  /**
107
- * Caches HTML content for a visited page and triggers background revalidation.
94
+ * Caches the HTML of a visited page so a revisit renders it immediately.
108
95
  *
109
- * Implements stale-while-revalidate pattern:
110
- * - Immediately caches the provided HTML for instant revisits
111
- * - Fetches fresh content in background for next visit (unless it's the current page)
96
+ * @remarks
97
+ * The cached copy is not refreshed in the background. The page on screen was just fetched or loaded, so
98
+ * fetching it again would add a request to every page load and navigation without a stale copy to fix.
112
99
  *
113
100
  * @param href - The URL of the visited page
114
- * @param html - The HTML content to cache initially
101
+ * @param html - The HTML content to cache
115
102
  */
116
103
  cacheVisitedPage(href, html) {
117
104
  const url = new URL(href, window.location.origin);
@@ -119,19 +106,6 @@ export class PrefetchManager {
119
106
  return;
120
107
  cacheNavigationHtml(url, html);
121
108
  this.prefetched.add(url.href);
122
- if (isSamePageHref(url.href))
123
- return;
124
- setTimeout(() => {
125
- void fetchNavigationPage(url, { bypassCache: true, priority: 'low' }).catch(() => { });
126
- }, 100);
127
- }
128
- /**
129
- * Checks if a URL has already been prefetched.
130
- * @param href - The URL to check
131
- */
132
- isPrefetched(href) {
133
- const url = new URL(href, window.location.origin);
134
- return this.prefetched.has(url.href);
135
109
  }
136
110
  /**
137
111
  * Determines if prefetching should be enabled based on network conditions.
@@ -90,9 +90,6 @@ export interface EcoRouterOptions {
90
90
  */
91
91
  reloadDocument?: (href: string) => void;
92
92
  }
93
- export type BrowserRouterNavigateOptions = {
94
- direction?: 'forward' | 'back' | 'replace';
95
- };
96
93
  /** Default document-level `<html>` attributes synchronized during navigation swaps. */
97
94
  export declare const DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC: string[];
98
95
  /** Default configuration options */
package/src/index.d.ts CHANGED
@@ -3,5 +3,4 @@
3
3
  * Client-side navigation and view transitions for Ecopages
4
4
  * @module
5
5
  */
6
- export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, type BrowserRouterNavigateOptions, type EcoRouterOptions, } from './client/index.js';
7
- export { DomSwapper } from './client/dom/dom-swapper.js';
6
+ export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, type EcoRouterOptions, } from './client/index.js';
package/src/index.js CHANGED
@@ -4,4 +4,3 @@
4
4
  * @module
5
5
  */
6
6
  export { createRouter, DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC, DEFAULT_OPTIONS, EcoRouter, syncDocumentElementAttributes, } from './client/index.js';
7
- export { DomSwapper } from './client/dom/dom-swapper.js';
@@ -1,7 +0,0 @@
1
- /** Applies browser-router's DOM body swap. Core owns document navigation work. */
2
- export declare class DomSwapper {
3
- private persistAttribute;
4
- constructor(persistAttribute: string);
5
- parseHTML(html: string, url?: URL): Document;
6
- morphBody(newDocument: Document): void;
7
- }
@@ -1,15 +0,0 @@
1
- import { parseNavigationDocument } from '@ecopages/core/client';
2
- import { morphBody } from './body-morpher.js';
3
- /** Applies browser-router's DOM body swap. Core owns document navigation work. */
4
- export class DomSwapper {
5
- persistAttribute;
6
- constructor(persistAttribute) {
7
- this.persistAttribute = persistAttribute;
8
- }
9
- parseHTML(html, url) {
10
- return parseNavigationDocument(html, url ?? new URL(window.location.href));
11
- }
12
- morphBody(newDocument) {
13
- morphBody(newDocument, this.persistAttribute);
14
- }
15
- }