@ecopages/browser-router 0.2.0-rc.1 → 0.2.0-rc.10
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/package.json +6 -3
- package/src/client/dom/head-updater-incoming.d.ts +4 -0
- package/src/client/dom/head-updater-incoming.js +23 -0
- package/src/client/dom/head-updater-script-identity.d.ts +14 -0
- package/src/client/dom/head-updater-script-identity.js +53 -0
- package/src/client/dom/head-updater-scripts.d.ts +10 -0
- package/src/client/dom/head-updater-scripts.js +81 -0
- package/src/client/dom/head-updater-types.d.ts +13 -0
- package/src/client/dom/head-updater-types.js +1 -0
- package/src/client/dom/head-updater.d.ts +4 -21
- package/src/client/dom/head-updater.js +7 -120
- package/src/client/eco-router-perform-navigation.d.ts +18 -0
- package/src/client/eco-router-perform-navigation.js +42 -0
- package/src/client/eco-router.js +12 -34
- package/src/client/navigation-commit-reload.d.ts +21 -0
- package/src/client/navigation-commit-reload.js +16 -0
- package/src/client/navigation-commit-swap.d.ts +17 -0
- package/src/client/navigation-commit-swap.js +29 -0
- package/src/client/navigation-commit.js +31 -36
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/browser-router",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.10",
|
|
4
4
|
"description": "Client-side router for Ecopages with view transitions support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -26,6 +26,9 @@
|
|
|
26
26
|
"default": "./src/client/index.js"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
29
32
|
"repository": {
|
|
30
33
|
"type": "git",
|
|
31
34
|
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
@@ -35,7 +38,7 @@
|
|
|
35
38
|
"morphdom": "^2.7.8"
|
|
36
39
|
},
|
|
37
40
|
"peerDependencies": {
|
|
38
|
-
"@ecopages/core": "0.2.0-rc.
|
|
41
|
+
"@ecopages/core": "0.2.0-rc.10"
|
|
39
42
|
},
|
|
40
43
|
"types": "./src/index.d.ts"
|
|
41
|
-
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies incoming document title and named meta tags to the live head.
|
|
3
|
+
*/
|
|
4
|
+
export function syncIncomingHeadMetadata(newDocument) {
|
|
5
|
+
const newTitle = newDocument.head.querySelector('title');
|
|
6
|
+
if (newTitle && document.title !== newTitle.textContent) {
|
|
7
|
+
document.title = newTitle.textContent || '';
|
|
8
|
+
}
|
|
9
|
+
for (const newMeta of newDocument.head.querySelectorAll('meta[name], meta[property]')) {
|
|
10
|
+
const name = newMeta.getAttribute('name');
|
|
11
|
+
const property = newMeta.getAttribute('property');
|
|
12
|
+
const content = newMeta.getAttribute('content');
|
|
13
|
+
const selector = name ? `meta[name="${name}"]` : `meta[property="${property}"]`;
|
|
14
|
+
const existingMeta = document.head.querySelector(selector);
|
|
15
|
+
if (existingMeta) {
|
|
16
|
+
if (existingMeta.getAttribute('content') !== content) {
|
|
17
|
+
existingMeta.setAttribute('content', content || '');
|
|
18
|
+
}
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
document.head.appendChild(newMeta.cloneNode(true));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PendingHeadScript } from './head-updater-types.js';
|
|
2
|
+
import type { PendingRerunScript } from '@ecopages/core/client/navigation-scripts';
|
|
3
|
+
type HeadScriptIdentity = Pick<PendingHeadScript | PendingRerunScript, 'scriptId' | 'src' | 'textContent'>;
|
|
4
|
+
/**
|
|
5
|
+
* Derives a stable identity key for a head script.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Priority: `data-eco-script-id` / `id` > `src` > trimmed inline content.
|
|
9
|
+
* Returns `null` for empty anonymous inline scripts that cannot be tracked.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getHeadScriptKey(script: HTMLScriptElement | HeadScriptIdentity): string | null;
|
|
12
|
+
export declare function findExistingHeadScript(script: HTMLScriptElement | HeadScriptIdentity): HTMLScriptElement | null;
|
|
13
|
+
export declare function areHeadScriptsEquivalent(nextScript: HTMLScriptElement, currentScript: HTMLScriptElement): boolean;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { RERUN_SRC_ATTR } from '@ecopages/core/client/navigation-scripts';
|
|
2
|
+
function readHeadScriptIdentity(script) {
|
|
3
|
+
if (!(script instanceof HTMLScriptElement)) {
|
|
4
|
+
return script;
|
|
5
|
+
}
|
|
6
|
+
return {
|
|
7
|
+
scriptId: script.getAttribute('data-eco-script-id') || script.getAttribute('id'),
|
|
8
|
+
src: script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute('src'),
|
|
9
|
+
textContent: script.textContent ?? '',
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Derives a stable identity key for a head script.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* Priority: `data-eco-script-id` / `id` > `src` > trimmed inline content.
|
|
17
|
+
* Returns `null` for empty anonymous inline scripts that cannot be tracked.
|
|
18
|
+
*/
|
|
19
|
+
export function getHeadScriptKey(script) {
|
|
20
|
+
const { scriptId, src, textContent } = readHeadScriptIdentity(script);
|
|
21
|
+
if (scriptId) {
|
|
22
|
+
return `id:${scriptId}`;
|
|
23
|
+
}
|
|
24
|
+
if (src) {
|
|
25
|
+
return `src:${src}`;
|
|
26
|
+
}
|
|
27
|
+
const inlineContent = textContent.trim();
|
|
28
|
+
return inlineContent ? `inline:${inlineContent}` : null;
|
|
29
|
+
}
|
|
30
|
+
export function findExistingHeadScript(script) {
|
|
31
|
+
const scriptKey = getHeadScriptKey(script);
|
|
32
|
+
if (!scriptKey) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return (Array.from(document.head.querySelectorAll('script')).find((candidate) => getHeadScriptKey(candidate) === scriptKey) ?? null);
|
|
36
|
+
}
|
|
37
|
+
export function areHeadScriptsEquivalent(nextScript, currentScript) {
|
|
38
|
+
if (getHeadScriptKey(nextScript) !== getHeadScriptKey(currentScript)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if ((nextScript.textContent ?? '') !== (currentScript.textContent ?? '')) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const nextAttributes = Array.from(nextScript.attributes).map((attribute) => [attribute.name, attribute.value]);
|
|
45
|
+
const currentAttributes = Array.from(currentScript.attributes).map((attribute) => [
|
|
46
|
+
attribute.name,
|
|
47
|
+
attribute.value,
|
|
48
|
+
]);
|
|
49
|
+
if (nextAttributes.length !== currentAttributes.length) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return nextAttributes.every(([name, value], index) => currentAttributes[index]?.[0] === name && currentAttributes[index]?.[1] === value);
|
|
53
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PendingHeadScript } from './head-updater-types.js';
|
|
2
|
+
export type HeadScriptDedupState = {
|
|
3
|
+
existingScriptSrcs: Set<string | null>;
|
|
4
|
+
existingInlineContents: Set<string>;
|
|
5
|
+
};
|
|
6
|
+
export declare function createHeadScriptDedupState(): HeadScriptDedupState;
|
|
7
|
+
/**
|
|
8
|
+
* Queues head scripts from the incoming document that are not yet present in the live head.
|
|
9
|
+
*/
|
|
10
|
+
export declare function collectPendingHeadScriptsFromIncoming(newDocument: Document, dedup: HeadScriptDedupState): PendingHeadScript[];
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { isNonExecutableHeadScript } from '@ecopages/core/client/navigation-scripts';
|
|
2
|
+
import { areHeadScriptsEquivalent, findExistingHeadScript } from './head-updater-script-identity.js';
|
|
3
|
+
export function createHeadScriptDedupState() {
|
|
4
|
+
return {
|
|
5
|
+
existingScriptSrcs: new Set(Array.from(document.head.querySelectorAll('script[src]')).map((script) => script.getAttribute('src'))),
|
|
6
|
+
existingInlineContents: new Set(Array.from(document.head.querySelectorAll('script:not([src])')).map((script) => (script.textContent ?? '').trim())),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function pushPendingHeadScript(pendingHeadScripts, script, options) {
|
|
10
|
+
pendingHeadScripts.push({
|
|
11
|
+
attributes: Array.from(script.attributes).map((attribute) => [attribute.name, attribute.value]),
|
|
12
|
+
textContent: script.textContent ?? '',
|
|
13
|
+
src: options.src,
|
|
14
|
+
scriptId: options.scriptId,
|
|
15
|
+
replaceExisting: options.replaceExisting,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function queueIdentifiedNonExecutableHeadScript(script, pendingHeadScripts, src, scriptId) {
|
|
19
|
+
const existingScript = findExistingHeadScript(script);
|
|
20
|
+
if (!existingScript) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (!isNonExecutableHeadScript(script)) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
if (areHeadScriptsEquivalent(script, existingScript)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
pushPendingHeadScript(pendingHeadScripts, script, {
|
|
30
|
+
src,
|
|
31
|
+
scriptId,
|
|
32
|
+
replaceExisting: true,
|
|
33
|
+
});
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
function queueExternalHeadScript(script, pendingHeadScripts, dedup, src, scriptId) {
|
|
37
|
+
if (dedup.existingScriptSrcs.has(src)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
pushPendingHeadScript(pendingHeadScripts, script, {
|
|
41
|
+
src,
|
|
42
|
+
scriptId,
|
|
43
|
+
replaceExisting: false,
|
|
44
|
+
});
|
|
45
|
+
dedup.existingScriptSrcs.add(src);
|
|
46
|
+
}
|
|
47
|
+
function queueInlineHeadScript(script, pendingHeadScripts, dedup, scriptId) {
|
|
48
|
+
const content = (script.textContent ?? '').trim();
|
|
49
|
+
if (!content || dedup.existingInlineContents.has(content)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
pushPendingHeadScript(pendingHeadScripts, script, {
|
|
53
|
+
src: null,
|
|
54
|
+
scriptId,
|
|
55
|
+
replaceExisting: false,
|
|
56
|
+
});
|
|
57
|
+
dedup.existingInlineContents.add(content);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Queues head scripts from the incoming document that are not yet present in the live head.
|
|
61
|
+
*/
|
|
62
|
+
export function collectPendingHeadScriptsFromIncoming(newDocument, dedup) {
|
|
63
|
+
const pendingHeadScripts = [];
|
|
64
|
+
for (const script of newDocument.head.querySelectorAll('script')) {
|
|
65
|
+
if (script.hasAttribute('data-eco-rerun')) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const src = script.getAttribute('src');
|
|
69
|
+
const scriptId = script.getAttribute('data-eco-script-id') || script.getAttribute('id');
|
|
70
|
+
if (scriptId && queueIdentifiedNonExecutableHeadScript(script, pendingHeadScripts, src, scriptId)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (src) {
|
|
74
|
+
queueExternalHeadScript(script, pendingHeadScripts, dedup, src, scriptId);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
queueInlineHeadScript(script, pendingHeadScripts, dedup, scriptId);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return pendingHeadScripts;
|
|
81
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PendingRerunScript } from '@ecopages/core/client/navigation-scripts';
|
|
2
|
+
export type PendingHeadScript = {
|
|
3
|
+
attributes: Array<[string, string]>;
|
|
4
|
+
textContent: string;
|
|
5
|
+
src: string | null;
|
|
6
|
+
scriptId: string | null;
|
|
7
|
+
replaceExisting: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type MorphHeadResult = {
|
|
10
|
+
bodyStrategy: 'morph' | 'replace';
|
|
11
|
+
pendingHeadScripts: PendingHeadScript[];
|
|
12
|
+
pendingRerunScripts: PendingRerunScript[];
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
src: string | null;
|
|
6
|
-
scriptId: string | null;
|
|
7
|
-
replaceExisting: boolean;
|
|
8
|
-
};
|
|
9
|
-
export type MorphHeadResult = {
|
|
10
|
-
bodyStrategy: 'morph' | 'replace';
|
|
11
|
-
pendingHeadScripts: PendingHeadScript[];
|
|
12
|
-
pendingRerunScripts: PendingRerunScript[];
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Derives a stable identity key for a head script.
|
|
16
|
-
*
|
|
17
|
-
* @remarks
|
|
18
|
-
* Priority: `data-eco-script-id` / `id` > `src` > trimmed inline content.
|
|
19
|
-
* Returns `null` for empty anonymous inline scripts that cannot be tracked.
|
|
20
|
-
*/
|
|
21
|
-
export declare function getHeadScriptKey(script: HTMLScriptElement | Pick<PendingHeadScript | PendingRerunScript, 'scriptId' | 'src' | 'textContent'>): string | null;
|
|
1
|
+
import type { PendingRerunScript } from '@ecopages/core/client/navigation-scripts';
|
|
2
|
+
import type { MorphHeadResult, PendingHeadScript } from './head-updater-types.js';
|
|
3
|
+
export { getHeadScriptKey } from './head-updater-script-identity.js';
|
|
4
|
+
export type { MorphHeadResult, PendingHeadScript } from './head-updater-types.js';
|
|
22
5
|
/**
|
|
23
6
|
* Updates document head using Turbo-style surgical updates.
|
|
24
7
|
*
|
|
@@ -1,55 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { collectRerunScripts, flushPendingRerunScripts, shouldPersistExecutableInlineHeadScript, } from '@ecopages/core/client/navigation-scripts';
|
|
2
|
+
import { syncIncomingHeadMetadata } from './head-updater-incoming.js';
|
|
3
|
+
import { collectPendingHeadScriptsFromIncoming, createHeadScriptDedupState } from './head-updater-scripts.js';
|
|
4
|
+
import { findExistingHeadScript, getHeadScriptKey } from './head-updater-script-identity.js';
|
|
5
|
+
export { getHeadScriptKey } from './head-updater-script-identity.js';
|
|
2
6
|
const DEFAULT_PERSIST_ATTR = 'data-eco-persist';
|
|
3
7
|
function isPersisted(element, persistAttribute) {
|
|
4
8
|
return element.hasAttribute(persistAttribute) || element.hasAttribute(DEFAULT_PERSIST_ATTR);
|
|
5
9
|
}
|
|
6
|
-
/**
|
|
7
|
-
* Derives a stable identity key for a head script.
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* Priority: `data-eco-script-id` / `id` > `src` > trimmed inline content.
|
|
11
|
-
* Returns `null` for empty anonymous inline scripts that cannot be tracked.
|
|
12
|
-
*/
|
|
13
|
-
export function getHeadScriptKey(script) {
|
|
14
|
-
const scriptId = script instanceof HTMLScriptElement
|
|
15
|
-
? script.getAttribute('data-eco-script-id') || script.getAttribute('id')
|
|
16
|
-
: script.scriptId;
|
|
17
|
-
if (scriptId) {
|
|
18
|
-
return `id:${scriptId}`;
|
|
19
|
-
}
|
|
20
|
-
const src = script instanceof HTMLScriptElement
|
|
21
|
-
? script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute('src')
|
|
22
|
-
: script.src;
|
|
23
|
-
if (src) {
|
|
24
|
-
return `src:${src}`;
|
|
25
|
-
}
|
|
26
|
-
const textContent = (script.textContent ?? '').trim();
|
|
27
|
-
return textContent ? `inline:${textContent}` : null;
|
|
28
|
-
}
|
|
29
|
-
function findExistingHeadScript(script) {
|
|
30
|
-
const scriptKey = getHeadScriptKey(script);
|
|
31
|
-
if (!scriptKey) {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
return (Array.from(document.head.querySelectorAll('script')).find((candidate) => getHeadScriptKey(candidate) === scriptKey) ?? null);
|
|
35
|
-
}
|
|
36
|
-
function areHeadScriptsEquivalent(nextScript, currentScript) {
|
|
37
|
-
if (getHeadScriptKey(nextScript) !== getHeadScriptKey(currentScript)) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
if ((nextScript.textContent ?? '') !== (currentScript.textContent ?? '')) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
const nextAttributes = Array.from(nextScript.attributes).map((attribute) => [attribute.name, attribute.value]);
|
|
44
|
-
const currentAttributes = Array.from(currentScript.attributes).map((attribute) => [
|
|
45
|
-
attribute.name,
|
|
46
|
-
attribute.value,
|
|
47
|
-
]);
|
|
48
|
-
if (nextAttributes.length !== currentAttributes.length) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return nextAttributes.every(([name, value], index) => currentAttributes[index]?.[0] === name && currentAttributes[index]?.[1] === value);
|
|
52
|
-
}
|
|
53
10
|
/**
|
|
54
11
|
* Removes head scripts that are no longer present in the incoming document.
|
|
55
12
|
*
|
|
@@ -88,80 +45,10 @@ function removeStaleHeadScripts(newDocument, persistAttribute) {
|
|
|
88
45
|
* - Injects new scripts from the incoming page that are absent from the current head
|
|
89
46
|
*/
|
|
90
47
|
export function morphHead(newDocument, persistAttribute) {
|
|
91
|
-
const pendingHeadScripts = [];
|
|
92
48
|
const pendingRerunScripts = collectRerunScripts(newDocument);
|
|
93
49
|
removeStaleHeadScripts(newDocument, persistAttribute);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
document.title = newTitle.textContent || '';
|
|
97
|
-
}
|
|
98
|
-
const newMetas = newDocument.head.querySelectorAll('meta[name], meta[property]');
|
|
99
|
-
for (const newMeta of newMetas) {
|
|
100
|
-
const name = newMeta.getAttribute('name');
|
|
101
|
-
const property = newMeta.getAttribute('property');
|
|
102
|
-
const content = newMeta.getAttribute('content');
|
|
103
|
-
const selector = name ? `meta[name="${name}"]` : `meta[property="${property}"]`;
|
|
104
|
-
const existingMeta = document.head.querySelector(selector);
|
|
105
|
-
if (existingMeta) {
|
|
106
|
-
if (existingMeta.getAttribute('content') !== content) {
|
|
107
|
-
existingMeta.setAttribute('content', content || '');
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
document.head.appendChild(newMeta.cloneNode(true));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const existingScriptSrcs = new Set(Array.from(document.head.querySelectorAll('script[src]')).map((s) => s.getAttribute('src')));
|
|
115
|
-
const existingInlineContents = new Set(Array.from(document.head.querySelectorAll('script:not([src])')).map((s) => (s.textContent ?? '').trim()));
|
|
116
|
-
const allNewHeadScripts = newDocument.head.querySelectorAll('script');
|
|
117
|
-
for (const script of allNewHeadScripts) {
|
|
118
|
-
if (script.hasAttribute('data-eco-rerun'))
|
|
119
|
-
continue;
|
|
120
|
-
const src = script.getAttribute('src');
|
|
121
|
-
const scriptId = script.getAttribute('data-eco-script-id') || script.getAttribute('id');
|
|
122
|
-
const existingScript = findExistingHeadScript(script);
|
|
123
|
-
if (scriptId && existingScript) {
|
|
124
|
-
if (!isNonExecutableHeadScript(script)) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
if (areHeadScriptsEquivalent(script, existingScript)) {
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
pendingHeadScripts.push({
|
|
131
|
-
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
132
|
-
textContent: script.textContent ?? '',
|
|
133
|
-
src,
|
|
134
|
-
scriptId,
|
|
135
|
-
replaceExisting: true,
|
|
136
|
-
});
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
if (src) {
|
|
140
|
-
if (existingScriptSrcs.has(src))
|
|
141
|
-
continue;
|
|
142
|
-
pendingHeadScripts.push({
|
|
143
|
-
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
144
|
-
textContent: script.textContent ?? '',
|
|
145
|
-
src,
|
|
146
|
-
scriptId,
|
|
147
|
-
replaceExisting: false,
|
|
148
|
-
});
|
|
149
|
-
existingScriptSrcs.add(src);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
const content = (script.textContent ?? '').trim();
|
|
153
|
-
if (!content || existingInlineContents.has(content))
|
|
154
|
-
continue;
|
|
155
|
-
pendingHeadScripts.push({
|
|
156
|
-
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
157
|
-
textContent: script.textContent ?? '',
|
|
158
|
-
src: null,
|
|
159
|
-
scriptId,
|
|
160
|
-
replaceExisting: false,
|
|
161
|
-
});
|
|
162
|
-
existingInlineContents.add(content);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
50
|
+
syncIncomingHeadMetadata(newDocument);
|
|
51
|
+
const pendingHeadScripts = collectPendingHeadScriptsFromIncoming(newDocument, createHeadScriptDedupState());
|
|
165
52
|
return {
|
|
166
53
|
bodyStrategy: pendingRerunScripts.length > 0 ? 'replace' : 'morph',
|
|
167
54
|
pendingHeadScripts,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { EcoNavigationEvent } from '@ecopages/core/router/navigation-lifecycle';
|
|
2
|
+
import type { NavigationCommit } from './navigation-commit.js';
|
|
3
|
+
import type { DomSwapper } from './dom/dom-swapper.js';
|
|
4
|
+
import type { PrefetchManager } from './services/prefetch-manager.js';
|
|
5
|
+
export type PerformNavigationAttemptInput = {
|
|
6
|
+
url: URL;
|
|
7
|
+
direction: EcoNavigationEvent['direction'];
|
|
8
|
+
bypassPrefetchCache?: boolean;
|
|
9
|
+
allowFullDocumentFallback?: boolean;
|
|
10
|
+
domSwapper: DomSwapper;
|
|
11
|
+
navigationCommit: NavigationCommit;
|
|
12
|
+
prefetchManager: PrefetchManager | null;
|
|
13
|
+
isStaleNavigation: () => boolean;
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
};
|
|
16
|
+
export declare function runPerformNavigationAttempt(input: PerformNavigationAttemptInput): Promise<boolean>;
|
|
17
|
+
export declare function shouldIgnorePerformNavigationError(error: unknown, isStaleNavigation: () => boolean): boolean;
|
|
18
|
+
export declare function replayQueuedBrowserRouterNavigation(queuedNavigationHref: string | null, performNavigation: (url: URL, direction: EcoNavigationEvent['direction']) => Promise<boolean>): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/router/navigation-coordinator';
|
|
2
|
+
import { fetchNavigationPage } from './navigation-fetch.js';
|
|
3
|
+
export async function runPerformNavigationAttempt(input) {
|
|
4
|
+
const html = await fetchNavigationPage(input.url, input.signal, {
|
|
5
|
+
bypassCache: input.bypassPrefetchCache,
|
|
6
|
+
getCachedHtml: input.prefetchManager ? (href) => input.prefetchManager.getCachedHtml(href) : undefined,
|
|
7
|
+
});
|
|
8
|
+
if (input.isStaleNavigation()) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const newDocument = input.domSwapper.parseHTML(html, input.url);
|
|
12
|
+
if (input.isStaleNavigation()) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return await input.navigationCommit.commit(input.url, input.direction, newDocument, {
|
|
16
|
+
html,
|
|
17
|
+
isStaleNavigation: input.isStaleNavigation,
|
|
18
|
+
allowFullDocumentFallback: input.allowFullDocumentFallback ?? true,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export function shouldIgnorePerformNavigationError(error, isStaleNavigation) {
|
|
22
|
+
if (isStaleNavigation()) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
return error instanceof Error && error.name === 'AbortError';
|
|
26
|
+
}
|
|
27
|
+
export function replayQueuedBrowserRouterNavigation(queuedNavigationHref, performNavigation) {
|
|
28
|
+
if (!queuedNavigationHref || queuedNavigationHref === window.location.pathname + window.location.search) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
32
|
+
const ownerState = navigationRuntime.getOwnerState();
|
|
33
|
+
if (ownerState.owner !== 'none' && ownerState.owner !== 'browser-router' && ownerState.canHandleSpaNavigation) {
|
|
34
|
+
void navigationRuntime.requestNavigation({
|
|
35
|
+
href: queuedNavigationHref,
|
|
36
|
+
direction: 'forward',
|
|
37
|
+
source: 'browser-router',
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
void performNavigation(new URL(queuedNavigationHref, window.location.origin), 'forward');
|
|
42
|
+
}
|
package/src/client/eco-router.js
CHANGED
|
@@ -10,7 +10,7 @@ import { DomSwapper } from './dom/dom-swapper.js';
|
|
|
10
10
|
import { PrefetchManager } from './services/prefetch-manager.js';
|
|
11
11
|
import { ViewTransitionManager } from './services/view-transition-manager.js';
|
|
12
12
|
import { NavigationCommit } from './navigation-commit.js';
|
|
13
|
-
import {
|
|
13
|
+
import { replayQueuedBrowserRouterNavigation, runPerformNavigationAttempt, shouldIgnorePerformNavigationError, } from './eco-router-perform-navigation.js';
|
|
14
14
|
/**
|
|
15
15
|
* Intercepts same-origin link clicks and performs client-side navigation
|
|
16
16
|
* using morphdom for efficient DOM diffing. Supports View Transitions API.
|
|
@@ -273,29 +273,21 @@ export class EcoRouter {
|
|
|
273
273
|
let queuedNavigationHref = null;
|
|
274
274
|
let committed = false;
|
|
275
275
|
try {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (isStaleNavigation()) {
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
const newDocument = this.domSwapper.parseHTML(html, url);
|
|
284
|
-
if (isStaleNavigation()) {
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
committed = await this.navigationCommit.commit(url, direction, newDocument, {
|
|
288
|
-
html,
|
|
289
|
-
isStaleNavigation,
|
|
276
|
+
committed = await runPerformNavigationAttempt({
|
|
277
|
+
url,
|
|
278
|
+
direction,
|
|
279
|
+
bypassPrefetchCache: options.bypassPrefetchCache,
|
|
290
280
|
allowFullDocumentFallback,
|
|
281
|
+
domSwapper: this.domSwapper,
|
|
282
|
+
navigationCommit: this.navigationCommit,
|
|
283
|
+
prefetchManager: this.prefetchManager,
|
|
284
|
+
isStaleNavigation,
|
|
285
|
+
signal,
|
|
291
286
|
});
|
|
292
287
|
return committed;
|
|
293
288
|
}
|
|
294
289
|
catch (error) {
|
|
295
|
-
if (isStaleNavigation
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
|
290
|
+
if (shouldIgnorePerformNavigationError(error, isStaleNavigation)) {
|
|
299
291
|
return false;
|
|
300
292
|
}
|
|
301
293
|
console.error('[ecopages] Navigation failed:', error);
|
|
@@ -312,21 +304,7 @@ export class EcoRouter {
|
|
|
312
304
|
queuedNavigationHref = this.queuedNavigationHref;
|
|
313
305
|
this.queuedNavigationHref = null;
|
|
314
306
|
}
|
|
315
|
-
|
|
316
|
-
const ownerState = navigationRuntime.getOwnerState();
|
|
317
|
-
if (ownerState.owner !== 'none' &&
|
|
318
|
-
ownerState.owner !== 'browser-router' &&
|
|
319
|
-
ownerState.canHandleSpaNavigation) {
|
|
320
|
-
void navigationRuntime.requestNavigation({
|
|
321
|
-
href: queuedNavigationHref,
|
|
322
|
-
direction: 'forward',
|
|
323
|
-
source: 'browser-router',
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
void this.performNavigation(new URL(queuedNavigationHref, window.location.origin), 'forward');
|
|
328
|
-
}
|
|
329
|
-
}
|
|
307
|
+
replayQueuedBrowserRouterNavigation(queuedNavigationHref, (replayUrl, replayDirection) => this.performNavigation(replayUrl, replayDirection));
|
|
330
308
|
}
|
|
331
309
|
}
|
|
332
310
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EcoNavigationOwner } from '@ecopages/core/router/navigation-coordinator';
|
|
2
|
+
import { getEcoNavigationRuntime } from '@ecopages/core/router/navigation-coordinator';
|
|
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/router/navigation-coordinator';
|
|
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/router/navigation-lifecycle';
|
|
2
|
+
import type { DomSwapper } from './dom/dom-swapper.js';
|
|
3
|
+
import type { EcoRouterOptions } from './types.js';
|
|
4
|
+
export type BrowserRouterCommitSwapInput = {
|
|
5
|
+
url: URL;
|
|
6
|
+
previousUrl: URL;
|
|
7
|
+
direction: EcoNavigationEvent['direction'];
|
|
8
|
+
newDocument: Document;
|
|
9
|
+
isStaleNavigation: () => boolean;
|
|
10
|
+
domSwapper: DomSwapper;
|
|
11
|
+
options: Required<EcoRouterOptions>;
|
|
12
|
+
useViewTransitions: boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Applies history, document metadata, head morph, and body swap for one navigation commit.
|
|
16
|
+
*/
|
|
17
|
+
export declare function runBrowserRouterCommitSwap(input: BrowserRouterCommitSwapInput): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { manageWindowScroll } from '@ecopages/core/client/scroll';
|
|
2
|
+
import { syncDocumentElementAttributes } from './document-element-sync.js';
|
|
3
|
+
/**
|
|
4
|
+
* Applies history, document metadata, head morph, and body swap for one navigation commit.
|
|
5
|
+
*/
|
|
6
|
+
export function runBrowserRouterCommitSwap(input) {
|
|
7
|
+
if (input.isStaleNavigation()) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (input.options.updateHistory && input.direction === 'forward') {
|
|
11
|
+
window.history.pushState({}, '', input.url.href);
|
|
12
|
+
}
|
|
13
|
+
else if (input.direction === 'replace') {
|
|
14
|
+
window.history.replaceState({}, '', input.url.href);
|
|
15
|
+
}
|
|
16
|
+
syncDocumentElementAttributes(document, input.newDocument, input.options.documentElementAttributesToSync);
|
|
17
|
+
const { bodyStrategy } = input.domSwapper.morphHead(input.newDocument);
|
|
18
|
+
if (input.useViewTransitions && bodyStrategy === 'morph') {
|
|
19
|
+
input.domSwapper.morphBody(input.newDocument);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
input.domSwapper.replaceBody(input.newDocument);
|
|
23
|
+
}
|
|
24
|
+
input.domSwapper.flushRerunScripts();
|
|
25
|
+
manageWindowScroll(input.url, input.previousUrl, {
|
|
26
|
+
scrollBehavior: input.options.scrollBehavior,
|
|
27
|
+
smoothScroll: input.options.smoothScroll,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { getEcoNavigationRuntime } from '@ecopages/core/router/navigation-coordinator';
|
|
2
2
|
import { completeNavigationLifecycle, dispatchBeforeSwap, } from '@ecopages/core/router/navigation-lifecycle';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { resolveNavigationReloadDecision } from './navigation-commit-reload.js';
|
|
4
|
+
import { runBrowserRouterCommitSwap } from './navigation-commit-swap.js';
|
|
5
|
+
function resolveCommitOwnerContext(navigationRuntime, newDocument) {
|
|
6
|
+
const currentDocumentOwner = navigationRuntime.resolveDocumentOwner(document, 'browser-router');
|
|
7
|
+
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, 'browser-router');
|
|
8
|
+
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
9
|
+
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner &&
|
|
10
|
+
currentDocumentOwner !== 'browser-router' &&
|
|
11
|
+
activeOwner === currentDocumentOwner;
|
|
12
|
+
return { currentDocumentOwner, shouldCleanupCurrentOwner };
|
|
13
|
+
}
|
|
5
14
|
/**
|
|
6
15
|
* Commits a fetched document into the live page and dispatches navigation lifecycle events.
|
|
7
16
|
*/
|
|
@@ -21,12 +30,7 @@ export class NavigationCommit {
|
|
|
21
30
|
const previousUrl = new URL(window.location.href);
|
|
22
31
|
const navigationRuntime = getEcoNavigationRuntime(window);
|
|
23
32
|
const isStaleNavigation = options.isStaleNavigation ?? (() => false);
|
|
24
|
-
const currentDocumentOwner = navigationRuntime
|
|
25
|
-
const newDocumentOwner = navigationRuntime.resolveDocumentOwner(newDocument, 'browser-router');
|
|
26
|
-
const activeOwner = navigationRuntime.getOwnerState().owner;
|
|
27
|
-
const shouldCleanupCurrentOwner = currentDocumentOwner !== newDocumentOwner &&
|
|
28
|
-
currentDocumentOwner !== 'browser-router' &&
|
|
29
|
-
activeOwner === currentDocumentOwner;
|
|
33
|
+
const { currentDocumentOwner, shouldCleanupCurrentOwner } = resolveCommitOwnerContext(navigationRuntime, newDocument);
|
|
30
34
|
const { requestedReload } = dispatchBeforeSwap(document, {
|
|
31
35
|
url,
|
|
32
36
|
direction,
|
|
@@ -36,14 +40,16 @@ export class NavigationCommit {
|
|
|
36
40
|
return false;
|
|
37
41
|
}
|
|
38
42
|
if (requestedReload) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
const reloadDecision = await resolveNavigationReloadDecision({
|
|
44
|
+
shouldCleanupCurrentOwner,
|
|
45
|
+
currentDocumentOwner,
|
|
46
|
+
navigationRuntime,
|
|
47
|
+
isStaleNavigation,
|
|
48
|
+
allowFullDocumentFallback,
|
|
49
|
+
urlHref: url.href,
|
|
50
|
+
});
|
|
51
|
+
if (reloadDecision.kind === 'reload') {
|
|
52
|
+
this.reloadDocument(reloadDecision.href);
|
|
47
53
|
}
|
|
48
54
|
return false;
|
|
49
55
|
}
|
|
@@ -59,26 +65,15 @@ export class NavigationCommit {
|
|
|
59
65
|
return false;
|
|
60
66
|
}
|
|
61
67
|
const commitSwap = () => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const { bodyStrategy } = this.domSwapper.morphHead(newDocument);
|
|
72
|
-
if (useViewTransitions && bodyStrategy === 'morph') {
|
|
73
|
-
this.domSwapper.morphBody(newDocument);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
this.domSwapper.replaceBody(newDocument);
|
|
77
|
-
}
|
|
78
|
-
this.domSwapper.flushRerunScripts();
|
|
79
|
-
manageWindowScroll(url, previousUrl, {
|
|
80
|
-
scrollBehavior: this.options.scrollBehavior,
|
|
81
|
-
smoothScroll: this.options.smoothScroll,
|
|
68
|
+
runBrowserRouterCommitSwap({
|
|
69
|
+
url,
|
|
70
|
+
previousUrl,
|
|
71
|
+
direction,
|
|
72
|
+
newDocument,
|
|
73
|
+
isStaleNavigation,
|
|
74
|
+
domSwapper: this.domSwapper,
|
|
75
|
+
options: this.options,
|
|
76
|
+
useViewTransitions,
|
|
82
77
|
});
|
|
83
78
|
};
|
|
84
79
|
if (useViewTransitions) {
|