@ecopages/browser-router 0.2.0-beta.31 → 0.2.0-beta.32

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.
@@ -1,13 +1,3 @@
1
- import { EcoRouter, createRouter } from "./eco-router.js";
2
- import { syncDocumentElementAttributes } from "./document-element-sync.js";
3
- import {
4
- DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
5
- DEFAULT_OPTIONS
6
- } from "./types.js";
7
- export {
8
- DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
9
- DEFAULT_OPTIONS,
10
- EcoRouter,
11
- createRouter,
12
- syncDocumentElementAttributes
13
- };
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';
@@ -1,114 +1,119 @@
1
- import { getEcoNavigationRuntime } from "@ecopages/core/router/navigation-coordinator";
2
- import { manageWindowScroll } from "@ecopages/core/client/scroll";
3
- import { syncDocumentElementAttributes } from "./document-element-sync.js";
4
- class NavigationCommit {
5
- domSwapper;
6
- viewTransitionManager;
7
- prefetchManager;
8
- options;
9
- constructor(domSwapper, viewTransitionManager, prefetchManager, options) {
10
- this.domSwapper = domSwapper;
11
- this.viewTransitionManager = viewTransitionManager;
12
- this.prefetchManager = prefetchManager;
13
- this.options = options;
14
- }
15
- async commit(url, direction, newDocument, options = {}) {
16
- const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
17
- const previousUrl = new URL(window.location.href);
18
- const navigationRuntime = getEcoNavigationRuntime(window);
19
- const isStaleNavigation = options.isStaleNavigation ?? (() => false);
20
- const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, "browser-router");
21
- const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, "browser-router");
22
- const activeOwner = navigationRuntime.getOwnerState().owner;
23
- const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner && currentDocumentOwner !== "browser-router" && activeOwner === currentDocumentOwner;
24
- let shouldReload = false;
25
- const beforeSwapEvent = {
26
- url,
27
- direction,
28
- newDocument,
29
- reload: () => {
30
- shouldReload = true;
31
- }
32
- };
33
- document.dispatchEvent(new CustomEvent("eco:before-swap", { detail: beforeSwapEvent }));
34
- if (isStaleNavigation()) {
35
- return false;
1
+ import { getEcoNavigationRuntime } from '@ecopages/core/router/navigation-coordinator';
2
+ import { manageWindowScroll } from '@ecopages/core/client/scroll';
3
+ import { syncDocumentElementAttributes } from './document-element-sync.js';
4
+ /**
5
+ * Commits a fetched document into the live page and dispatches navigation lifecycle events.
6
+ */
7
+ export class NavigationCommit {
8
+ domSwapper;
9
+ viewTransitionManager;
10
+ prefetchManager;
11
+ options;
12
+ constructor(domSwapper, viewTransitionManager, prefetchManager, options) {
13
+ this.domSwapper = domSwapper;
14
+ this.viewTransitionManager = viewTransitionManager;
15
+ this.prefetchManager = prefetchManager;
16
+ this.options = options;
36
17
  }
37
- if (shouldReload) {
38
- if (shouldCleanupCurrentOwner) {
39
- await navigationRuntime.cleanupOwner(currentDocumentOwner);
40
- }
41
- if (isStaleNavigation()) {
42
- return false;
43
- }
44
- if (allowFullDocumentFallback) {
45
- this.reloadDocument(url.href);
46
- }
47
- return false;
18
+ async commit(url, direction, newDocument, options = {}) {
19
+ const allowFullDocumentFallback = options.allowFullDocumentFallback ?? true;
20
+ const previousUrl = new URL(window.location.href);
21
+ const navigationRuntime = getEcoNavigationRuntime(window);
22
+ const isStaleNavigation = options.isStaleNavigation ?? (() => false);
23
+ const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, 'browser-router');
24
+ const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, 'browser-router');
25
+ const activeOwner = navigationRuntime.getOwnerState().owner;
26
+ const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner &&
27
+ currentDocumentOwner !== 'browser-router' &&
28
+ activeOwner === currentDocumentOwner;
29
+ let shouldReload = false;
30
+ const beforeSwapEvent = {
31
+ url,
32
+ direction,
33
+ newDocument,
34
+ reload: () => {
35
+ shouldReload = true;
36
+ },
37
+ };
38
+ document.dispatchEvent(new CustomEvent('eco:before-swap', { detail: beforeSwapEvent }));
39
+ if (isStaleNavigation()) {
40
+ return false;
41
+ }
42
+ if (shouldReload) {
43
+ if (shouldCleanupCurrentOwner) {
44
+ await navigationRuntime.cleanupOwner(currentDocumentOwner);
45
+ }
46
+ if (isStaleNavigation()) {
47
+ return false;
48
+ }
49
+ if (allowFullDocumentFallback) {
50
+ this.reloadDocument(url.href);
51
+ }
52
+ return false;
53
+ }
54
+ const useViewTransitions = this.options.viewTransitions;
55
+ await this.domSwapper.preloadStylesheets(newDocument);
56
+ if (isStaleNavigation()) {
57
+ return false;
58
+ }
59
+ if (shouldCleanupCurrentOwner) {
60
+ await navigationRuntime.cleanupOwner(currentDocumentOwner);
61
+ }
62
+ if (isStaleNavigation()) {
63
+ return false;
64
+ }
65
+ const commitSwap = () => {
66
+ if (isStaleNavigation())
67
+ return;
68
+ if (this.options.updateHistory && direction === 'forward') {
69
+ window.history.pushState({}, '', url.href);
70
+ }
71
+ else if (direction === 'replace') {
72
+ window.history.replaceState({}, '', url.href);
73
+ }
74
+ syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
75
+ const { bodyStrategy } = this.domSwapper.morphHead(newDocument);
76
+ if (useViewTransitions && bodyStrategy === 'morph') {
77
+ this.domSwapper.morphBody(newDocument);
78
+ }
79
+ else {
80
+ this.domSwapper.replaceBody(newDocument);
81
+ }
82
+ this.domSwapper.flushRerunScripts();
83
+ manageWindowScroll(url, previousUrl, {
84
+ scrollBehavior: this.options.scrollBehavior,
85
+ smoothScroll: this.options.smoothScroll,
86
+ });
87
+ };
88
+ if (useViewTransitions) {
89
+ await this.viewTransitionManager.transition(commitSwap);
90
+ }
91
+ else {
92
+ commitSwap();
93
+ }
94
+ if (isStaleNavigation()) {
95
+ return false;
96
+ }
97
+ navigationRuntime.adoptDocumentOwner(newDocument, 'browser-router');
98
+ const afterSwapEvent = {
99
+ url,
100
+ direction,
101
+ };
102
+ document.dispatchEvent(new CustomEvent('eco:after-swap', { detail: afterSwapEvent }));
103
+ this.prefetchManager?.observeLinks();
104
+ if (options.html) {
105
+ this.prefetchManager?.cacheVisitedPage(url.href, options.html);
106
+ }
107
+ requestAnimationFrame(() => {
108
+ if (isStaleNavigation())
109
+ return;
110
+ document.dispatchEvent(new CustomEvent('eco:page-load', {
111
+ detail: { url, direction },
112
+ }));
113
+ });
114
+ return true;
48
115
  }
49
- const useViewTransitions = this.options.viewTransitions;
50
- await this.domSwapper.preloadStylesheets(newDocument);
51
- if (isStaleNavigation()) {
52
- return false;
116
+ reloadDocument(href) {
117
+ window.location.assign(href);
53
118
  }
54
- if (shouldCleanupCurrentOwner) {
55
- await navigationRuntime.cleanupOwner(currentDocumentOwner);
56
- }
57
- if (isStaleNavigation()) {
58
- return false;
59
- }
60
- const commitSwap = () => {
61
- if (isStaleNavigation()) return;
62
- if (this.options.updateHistory && direction === "forward") {
63
- window.history.pushState({}, "", url.href);
64
- } else if (direction === "replace") {
65
- window.history.replaceState({}, "", url.href);
66
- }
67
- syncDocumentElementAttributes(document, newDocument, this.options.documentElementAttributesToSync);
68
- const { bodyStrategy } = this.domSwapper.morphHead(newDocument);
69
- if (useViewTransitions && bodyStrategy === "morph") {
70
- this.domSwapper.morphBody(newDocument);
71
- } else {
72
- this.domSwapper.replaceBody(newDocument);
73
- }
74
- this.domSwapper.flushRerunScripts();
75
- manageWindowScroll(url, previousUrl, {
76
- scrollBehavior: this.options.scrollBehavior,
77
- smoothScroll: this.options.smoothScroll
78
- });
79
- };
80
- if (useViewTransitions) {
81
- await this.viewTransitionManager.transition(commitSwap);
82
- } else {
83
- commitSwap();
84
- }
85
- if (isStaleNavigation()) {
86
- return false;
87
- }
88
- navigationRuntime.adoptDocumentOwner(newDocument, "browser-router");
89
- const afterSwapEvent = {
90
- url,
91
- direction
92
- };
93
- document.dispatchEvent(new CustomEvent("eco:after-swap", { detail: afterSwapEvent }));
94
- this.prefetchManager?.observeLinks();
95
- if (options.html) {
96
- this.prefetchManager?.cacheVisitedPage(url.href, options.html);
97
- }
98
- requestAnimationFrame(() => {
99
- if (isStaleNavigation()) return;
100
- document.dispatchEvent(
101
- new CustomEvent("eco:page-load", {
102
- detail: { url, direction }
103
- })
104
- );
105
- });
106
- return true;
107
- }
108
- reloadDocument(href) {
109
- window.location.assign(href);
110
- }
111
119
  }
112
- export {
113
- NavigationCommit
114
- };
@@ -1,24 +1,24 @@
1
- import { assertHtmlPageResponse } from "@ecopages/core/router/link-navigation-policy";
2
- async function fetchNavigationPage(url, signal, options = {}) {
3
- if (!options.bypassCache && options.getCachedHtml) {
4
- const cachedHtml = options.getCachedHtml(url.href);
5
- if (cachedHtml) {
6
- return cachedHtml;
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
+ }
7
11
  }
8
- }
9
- const response = await fetch(url.href, {
10
- signal,
11
- cache: "no-store",
12
- headers: {
13
- Accept: "text/html"
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}`);
14
21
  }
15
- });
16
- if (!response.ok) {
17
- throw new Error(`Failed to fetch page: ${response.status}`);
18
- }
19
- await assertHtmlPageResponse(response);
20
- return response.text();
22
+ await assertHtmlPageResponse(response);
23
+ return response.text();
21
24
  }
22
- export {
23
- fetchNavigationPage
24
- };