@ecopages/browser-router 0.2.0-beta.2 → 0.2.0-beta.20
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 +8 -2
- package/package.json +7 -7
- package/src/client/document-element-sync.d.ts +0 -7
- package/src/client/document-element-sync.js +0 -3
- package/src/client/dom/body-morpher.d.ts +24 -0
- package/src/client/dom/body-morpher.js +103 -0
- package/src/client/dom/dom-swapper.d.ts +30 -0
- package/src/client/dom/dom-swapper.js +51 -0
- package/src/client/dom/head-updater.d.ts +42 -0
- package/src/client/dom/head-updater.js +173 -0
- package/src/client/dom/html-parser.d.ts +4 -0
- package/src/client/dom/html-parser.js +8 -0
- package/src/client/dom/stylesheet-discovery.d.ts +12 -0
- package/src/client/dom/stylesheet-discovery.js +20 -0
- package/src/client/dom/stylesheet-preloader.d.ts +10 -0
- package/src/client/dom/stylesheet-preloader.js +38 -0
- package/src/client/eco-router.d.ts +1 -33
- package/src/client/eco-router.js +61 -211
- package/src/client/index.d.ts +3 -0
- package/src/client/index.js +13 -0
- package/src/client/navigation-commit.d.ts +21 -0
- package/src/client/navigation-commit.js +114 -0
- package/src/client/navigation-fetch.d.ts +8 -0
- package/src/client/navigation-fetch.js +24 -0
- package/src/client/services/prefetch-manager.d.ts +2 -14
- package/src/client/services/prefetch-manager.js +29 -47
- package/src/client/services/view-transition-manager.js +1 -1
- package/src/client/types.d.ts +6 -0
- package/src/index.d.ts +3 -5
- package/src/index.js +7 -7
- package/src/client/services/dom-swapper.d.ts +0 -168
- package/src/client/services/dom-swapper.js +0 -529
- 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/view-transition-utils.d.ts +0 -14
- package/src/client/view-transition-utils.js +0 -60
- package/src/types.d.ts +0 -15
- package/src/types.js +0 -5
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ For advanced cases, browser-router also exports low-level document sync tooling
|
|
|
60
60
|
```ts
|
|
61
61
|
import {
|
|
62
62
|
createRouter,
|
|
63
|
-
|
|
63
|
+
DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
64
64
|
syncDocumentElementAttributes,
|
|
65
65
|
} from '@ecopages/browser-router';
|
|
66
66
|
|
|
@@ -68,7 +68,7 @@ const router = createRouter();
|
|
|
68
68
|
|
|
69
69
|
document.addEventListener('eco:before-swap', (event) => {
|
|
70
70
|
syncDocumentElementAttributes(document, event.detail.newDocument, [
|
|
71
|
-
...
|
|
71
|
+
...DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC,
|
|
72
72
|
'data-theme',
|
|
73
73
|
]);
|
|
74
74
|
});
|
|
@@ -76,6 +76,12 @@ document.addEventListener('eco:before-swap', (event) => {
|
|
|
76
76
|
|
|
77
77
|
Loading the router script is the opt-in point for browser-router-managed navigation on that page shell. Pages without the router script continue to use normal document navigation.
|
|
78
78
|
|
|
79
|
+
## Router singleton
|
|
80
|
+
|
|
81
|
+
`createRouter()` returns a single active router per browser tab. The first call creates, starts, and stores the instance on `window.__ecopages_browser_router__`. Later calls return that same instance and ignore new options.
|
|
82
|
+
|
|
83
|
+
Call `router.stop()` before replacing the router script in long-lived sessions (for example after `data-eco-rerun` layout reloads) if you need a fresh instance with different options.
|
|
84
|
+
|
|
79
85
|
## Configuration
|
|
80
86
|
|
|
81
87
|
| Option | Type | Default | Description |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/browser-router",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.20",
|
|
4
4
|
"description": "Client-side router for Ecopages with view transitions support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -18,24 +18,24 @@
|
|
|
18
18
|
"default": "./src/index.js"
|
|
19
19
|
},
|
|
20
20
|
"./client": {
|
|
21
|
-
"types": "./src/client/
|
|
22
|
-
"default": "./src/client/
|
|
21
|
+
"types": "./src/client/index.d.ts",
|
|
22
|
+
"default": "./src/client/index.js"
|
|
23
23
|
},
|
|
24
24
|
"./client.ts": {
|
|
25
|
-
"types": "./src/client/
|
|
26
|
-
"default": "./src/client/
|
|
25
|
+
"types": "./src/client/index.d.ts",
|
|
26
|
+
"default": "./src/client/index.js"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
30
30
|
"type": "git",
|
|
31
|
-
"url": "https://github.com/ecopages/ecopages.git",
|
|
31
|
+
"url": "git+https://github.com/ecopages/ecopages.git",
|
|
32
32
|
"directory": "packages/browser-router"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"morphdom": "^2.7.8"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@ecopages/core": "0.2.0-beta.
|
|
38
|
+
"@ecopages/core": "0.2.0-beta.20"
|
|
39
39
|
},
|
|
40
40
|
"types": "./src/index.d.ts"
|
|
41
41
|
}
|
|
@@ -2,13 +2,6 @@
|
|
|
2
2
|
* Helpers for synchronizing selected root `<html>` attributes during client-side navigation.
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
|
-
/**
|
|
6
|
-
* Default root `<html>` attributes that browser-router treats as document-owned.
|
|
7
|
-
*
|
|
8
|
-
* These attributes are synchronized from the incoming document during navigation.
|
|
9
|
-
* Other root attributes are preserved unless explicitly included.
|
|
10
|
-
*/
|
|
11
|
-
export declare const defaultDocumentElementAttributesToSync: string[];
|
|
12
5
|
/**
|
|
13
6
|
* Synchronizes a selected set of root `<html>` attributes from an incoming document
|
|
14
7
|
* onto the current live document.
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC } from "./types.js";
|
|
2
|
-
const defaultDocumentElementAttributesToSync = DEFAULT_DOCUMENT_ELEMENT_ATTRIBUTES_TO_SYNC;
|
|
3
1
|
function syncDocumentElementAttributes(currentDocument, newDocument, attributes) {
|
|
4
2
|
const currentHtml = currentDocument.documentElement;
|
|
5
3
|
const nextHtml = newDocument.documentElement;
|
|
@@ -15,6 +13,5 @@ function syncDocumentElementAttributes(currentDocument, newDocument, attributes)
|
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
15
|
export {
|
|
18
|
-
defaultDocumentElementAttributesToSync,
|
|
19
16
|
syncDocumentElementAttributes
|
|
20
17
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manually attaches declarative shadow DOM templates.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Browsers only process `<template shadowrootmode>` during initial parse.
|
|
6
|
+
*/
|
|
7
|
+
export declare function processDeclarativeShadowDOM(root: Element | Document | ShadowRoot): void;
|
|
8
|
+
/**
|
|
9
|
+
* Morphs document body using morphdom.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Preserves persisted elements and hydrated custom elements.
|
|
13
|
+
* Light-DOM custom elements are fully replaced to trigger proper
|
|
14
|
+
* disconnectedCallback → connectedCallback lifecycle.
|
|
15
|
+
*/
|
|
16
|
+
export declare function morphBody(newDocument: Document, persistAttribute: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* Replaces body content in a single operation.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Preserves persisted elements by moving them to the new body.
|
|
22
|
+
* Use when View Transitions are disabled.
|
|
23
|
+
*/
|
|
24
|
+
export declare function replaceBody(newDocument: Document, persistAttribute: string): void;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import morphdom from "morphdom";
|
|
2
|
+
const DEFAULT_PERSIST_ATTR = "data-eco-persist";
|
|
3
|
+
function isPersisted(element, persistAttribute) {
|
|
4
|
+
return element.hasAttribute(persistAttribute) || element.hasAttribute(DEFAULT_PERSIST_ATTR);
|
|
5
|
+
}
|
|
6
|
+
function isHydratedCustomElement(element) {
|
|
7
|
+
return element.localName.includes("-") && element.shadowRoot !== null;
|
|
8
|
+
}
|
|
9
|
+
function getBodyMorphKey(element, persistAttribute) {
|
|
10
|
+
if (!(element instanceof Element)) {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
return element.getAttribute(persistAttribute) || element.getAttribute(DEFAULT_PERSIST_ATTR) || void 0;
|
|
14
|
+
}
|
|
15
|
+
function isLightDomCustomElement(element) {
|
|
16
|
+
return element.localName.includes("-") && element.shadowRoot === null;
|
|
17
|
+
}
|
|
18
|
+
function replaceCustomElement(fromEl, toEl, deferred) {
|
|
19
|
+
deferred.push({ from: fromEl, to: toEl.cloneNode(true) });
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
function materializeCustomElement(element) {
|
|
23
|
+
const materializedElement = document.createElement(element.localName);
|
|
24
|
+
for (const attr of element.attributes) {
|
|
25
|
+
materializedElement.setAttribute(attr.name, attr.value);
|
|
26
|
+
}
|
|
27
|
+
materializedElement.innerHTML = element.innerHTML;
|
|
28
|
+
return materializedElement;
|
|
29
|
+
}
|
|
30
|
+
function flushDeferredCustomElementReplacements(deferred) {
|
|
31
|
+
for (const { from, to } of deferred) {
|
|
32
|
+
if (!from.isConnected) continue;
|
|
33
|
+
const newEl = materializeCustomElement(to);
|
|
34
|
+
from.replaceWith(newEl);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function processDeclarativeShadowDOM(root) {
|
|
38
|
+
const templates = root.querySelectorAll("template[shadowrootmode], template[shadowroot]");
|
|
39
|
+
for (const template of templates) {
|
|
40
|
+
const mode = template.getAttribute("shadowrootmode") || template.getAttribute("shadowroot");
|
|
41
|
+
const parent = template.parentElement;
|
|
42
|
+
if (parent && !parent.shadowRoot) {
|
|
43
|
+
const shadowRoot = parent.attachShadow({ mode });
|
|
44
|
+
shadowRoot.appendChild(template.content);
|
|
45
|
+
template.remove();
|
|
46
|
+
processDeclarativeShadowDOM(shadowRoot);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function morphBody(newDocument, persistAttribute) {
|
|
51
|
+
const deferredReplacements = [];
|
|
52
|
+
morphdom(document.body, newDocument.body, {
|
|
53
|
+
getNodeKey: (node) => getBodyMorphKey(node, persistAttribute),
|
|
54
|
+
onBeforeNodeAdded: (node) => {
|
|
55
|
+
if (node instanceof Element && isLightDomCustomElement(node)) {
|
|
56
|
+
return materializeCustomElement(node);
|
|
57
|
+
}
|
|
58
|
+
return node;
|
|
59
|
+
},
|
|
60
|
+
onBeforeElUpdated: (fromEl, toEl) => {
|
|
61
|
+
if (isPersisted(fromEl, persistAttribute)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (isHydratedCustomElement(fromEl)) {
|
|
65
|
+
return replaceCustomElement(fromEl, toEl, deferredReplacements);
|
|
66
|
+
}
|
|
67
|
+
if (isLightDomCustomElement(fromEl)) {
|
|
68
|
+
return replaceCustomElement(fromEl, toEl, deferredReplacements);
|
|
69
|
+
}
|
|
70
|
+
if (fromEl.isEqualNode(toEl)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
flushDeferredCustomElementReplacements(deferredReplacements);
|
|
77
|
+
processDeclarativeShadowDOM(document.body);
|
|
78
|
+
}
|
|
79
|
+
function replaceBody(newDocument, persistAttribute) {
|
|
80
|
+
const persistedElements = document.body.querySelectorAll(`[${persistAttribute}], [${DEFAULT_PERSIST_ATTR}]`);
|
|
81
|
+
const persistedMap = /* @__PURE__ */ new Map();
|
|
82
|
+
for (const el of persistedElements) {
|
|
83
|
+
const key = el.getAttribute(persistAttribute) || el.getAttribute(DEFAULT_PERSIST_ATTR);
|
|
84
|
+
if (key) {
|
|
85
|
+
persistedMap.set(key, el);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const [key, oldEl] of persistedMap) {
|
|
89
|
+
const placeholder = newDocument.body.querySelector(
|
|
90
|
+
`[${persistAttribute}="${key}"], [${DEFAULT_PERSIST_ATTR}="${key}"]`
|
|
91
|
+
);
|
|
92
|
+
if (placeholder) {
|
|
93
|
+
placeholder.replaceWith(oldEl);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
document.body.replaceChildren(...Array.from(newDocument.body.childNodes));
|
|
97
|
+
processDeclarativeShadowDOM(document.body);
|
|
98
|
+
}
|
|
99
|
+
export {
|
|
100
|
+
morphBody,
|
|
101
|
+
processDeclarativeShadowDOM,
|
|
102
|
+
replaceBody
|
|
103
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles DOM manipulation during client-side page transitions.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Uses a hybrid approach inspired by Turbo:
|
|
6
|
+
* - Surgical head updates (no morphing) to prevent FOUC
|
|
7
|
+
* - Idiomorph for efficient body diffing
|
|
8
|
+
*/
|
|
9
|
+
export declare class DomSwapper {
|
|
10
|
+
private persistAttribute;
|
|
11
|
+
private pendingHeadScripts;
|
|
12
|
+
private pendingRerunScripts;
|
|
13
|
+
constructor(persistAttribute: string);
|
|
14
|
+
parseHTML(html: string, url?: URL): Document;
|
|
15
|
+
preloadStylesheets(newDocument: Document): Promise<void>;
|
|
16
|
+
morphHead(newDocument: Document): {
|
|
17
|
+
bodyStrategy: 'morph' | 'replace';
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Replays queued scripts after the body swap completes.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* Scripts are intentionally flushed after the new body is in place so DOM-
|
|
24
|
+
* dependent bootstraps bind against the incoming page rather than the page
|
|
25
|
+
* being replaced.
|
|
26
|
+
*/
|
|
27
|
+
flushRerunScripts(): void;
|
|
28
|
+
morphBody(newDocument: Document): void;
|
|
29
|
+
replaceBody(newDocument: Document): void;
|
|
30
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { morphBody, replaceBody } from "./body-morpher.js";
|
|
2
|
+
import {
|
|
3
|
+
flushHeadScripts,
|
|
4
|
+
flushRerunScripts as flushPendingRerunScripts,
|
|
5
|
+
morphHead
|
|
6
|
+
} from "./head-updater.js";
|
|
7
|
+
import { parseHTML } from "./html-parser.js";
|
|
8
|
+
import { preloadStylesheets } from "./stylesheet-preloader.js";
|
|
9
|
+
class DomSwapper {
|
|
10
|
+
persistAttribute;
|
|
11
|
+
pendingHeadScripts = [];
|
|
12
|
+
pendingRerunScripts = [];
|
|
13
|
+
constructor(persistAttribute) {
|
|
14
|
+
this.persistAttribute = persistAttribute;
|
|
15
|
+
}
|
|
16
|
+
parseHTML(html, url) {
|
|
17
|
+
return parseHTML(html, url);
|
|
18
|
+
}
|
|
19
|
+
async preloadStylesheets(newDocument) {
|
|
20
|
+
return preloadStylesheets(newDocument);
|
|
21
|
+
}
|
|
22
|
+
morphHead(newDocument) {
|
|
23
|
+
const result = morphHead(newDocument, this.persistAttribute);
|
|
24
|
+
this.pendingHeadScripts = result.pendingHeadScripts;
|
|
25
|
+
this.pendingRerunScripts = result.pendingRerunScripts;
|
|
26
|
+
return { bodyStrategy: result.bodyStrategy };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Replays queued scripts after the body swap completes.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Scripts are intentionally flushed after the new body is in place so DOM-
|
|
33
|
+
* dependent bootstraps bind against the incoming page rather than the page
|
|
34
|
+
* being replaced.
|
|
35
|
+
*/
|
|
36
|
+
flushRerunScripts() {
|
|
37
|
+
flushHeadScripts(this.pendingHeadScripts);
|
|
38
|
+
this.pendingHeadScripts = [];
|
|
39
|
+
flushPendingRerunScripts(this.pendingRerunScripts);
|
|
40
|
+
this.pendingRerunScripts = [];
|
|
41
|
+
}
|
|
42
|
+
morphBody(newDocument) {
|
|
43
|
+
morphBody(newDocument, this.persistAttribute);
|
|
44
|
+
}
|
|
45
|
+
replaceBody(newDocument) {
|
|
46
|
+
replaceBody(newDocument, this.persistAttribute);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export {
|
|
50
|
+
DomSwapper
|
|
51
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
};
|
|
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;
|
|
22
|
+
/**
|
|
23
|
+
* Updates document head using Turbo-style surgical updates.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* This approach avoids morphing the head element entirely, which prevents
|
|
27
|
+
* browser repaints that cause FOUC. Instead, it:
|
|
28
|
+
* - Updates the document title
|
|
29
|
+
* - Merges meta tags (adds new, updates changed)
|
|
30
|
+
* - Leaves stylesheets untouched (they're preloaded separately)
|
|
31
|
+
* - Handles script re-execution for marked scripts
|
|
32
|
+
* - Injects new scripts from the incoming page that are absent from the current head
|
|
33
|
+
*/
|
|
34
|
+
export declare function morphHead(newDocument: Document, persistAttribute: string): MorphHeadResult;
|
|
35
|
+
/**
|
|
36
|
+
* Replays queued head scripts after the body swap completes.
|
|
37
|
+
*/
|
|
38
|
+
export declare function flushHeadScripts(pendingHeadScripts: PendingHeadScript[]): void;
|
|
39
|
+
/**
|
|
40
|
+
* Replays queued `data-eco-rerun` scripts after the body swap completes.
|
|
41
|
+
*/
|
|
42
|
+
export declare function flushRerunScripts(pendingRerunScripts: PendingRerunScript[]): void;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RERUN_SRC_ATTR,
|
|
3
|
+
collectRerunScripts,
|
|
4
|
+
flushPendingRerunScripts,
|
|
5
|
+
isNonExecutableHeadScript,
|
|
6
|
+
shouldPersistExecutableInlineHeadScript
|
|
7
|
+
} from "@ecopages/core/client/navigation-scripts";
|
|
8
|
+
const DEFAULT_PERSIST_ATTR = "data-eco-persist";
|
|
9
|
+
function isPersisted(element, persistAttribute) {
|
|
10
|
+
return element.hasAttribute(persistAttribute) || element.hasAttribute(DEFAULT_PERSIST_ATTR);
|
|
11
|
+
}
|
|
12
|
+
function getHeadScriptKey(script) {
|
|
13
|
+
const scriptId = script instanceof HTMLScriptElement ? script.getAttribute("data-eco-script-id") || script.getAttribute("id") : script.scriptId;
|
|
14
|
+
if (scriptId) {
|
|
15
|
+
return `id:${scriptId}`;
|
|
16
|
+
}
|
|
17
|
+
const src = script instanceof HTMLScriptElement ? script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute("src") : script.src;
|
|
18
|
+
if (src) {
|
|
19
|
+
return `src:${src}`;
|
|
20
|
+
}
|
|
21
|
+
const textContent = (script.textContent ?? "").trim();
|
|
22
|
+
return textContent ? `inline:${textContent}` : null;
|
|
23
|
+
}
|
|
24
|
+
function findExistingHeadScript(script) {
|
|
25
|
+
const scriptKey = getHeadScriptKey(script);
|
|
26
|
+
if (!scriptKey) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
return Array.from(document.head.querySelectorAll("script")).find(
|
|
30
|
+
(candidate) => getHeadScriptKey(candidate) === scriptKey
|
|
31
|
+
) ?? null;
|
|
32
|
+
}
|
|
33
|
+
function areHeadScriptsEquivalent(nextScript, currentScript) {
|
|
34
|
+
if (getHeadScriptKey(nextScript) !== getHeadScriptKey(currentScript)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if ((nextScript.textContent ?? "") !== (currentScript.textContent ?? "")) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const nextAttributes = Array.from(nextScript.attributes).map((attribute) => [attribute.name, attribute.value]);
|
|
41
|
+
const currentAttributes = Array.from(currentScript.attributes).map((attribute) => [
|
|
42
|
+
attribute.name,
|
|
43
|
+
attribute.value
|
|
44
|
+
]);
|
|
45
|
+
if (nextAttributes.length !== currentAttributes.length) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return nextAttributes.every(
|
|
49
|
+
([name, value], index) => currentAttributes[index]?.[0] === name && currentAttributes[index]?.[1] === value
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
function removeStaleHeadScripts(newDocument, persistAttribute) {
|
|
53
|
+
const nextScriptKeys = new Set(
|
|
54
|
+
Array.from(newDocument.head.querySelectorAll("script")).map((script) => getHeadScriptKey(script)).filter((key) => key !== null)
|
|
55
|
+
);
|
|
56
|
+
for (const script of Array.from(document.head.querySelectorAll("script"))) {
|
|
57
|
+
const key = getHeadScriptKey(script);
|
|
58
|
+
if (!key || nextScriptKeys.has(key)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (isPersisted(script, persistAttribute)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (shouldPersistExecutableInlineHeadScript(script)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
script.remove();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function morphHead(newDocument, persistAttribute) {
|
|
71
|
+
const pendingHeadScripts = [];
|
|
72
|
+
const pendingRerunScripts = collectRerunScripts(newDocument);
|
|
73
|
+
removeStaleHeadScripts(newDocument, persistAttribute);
|
|
74
|
+
const newTitle = newDocument.head.querySelector("title");
|
|
75
|
+
if (newTitle && document.title !== newTitle.textContent) {
|
|
76
|
+
document.title = newTitle.textContent || "";
|
|
77
|
+
}
|
|
78
|
+
const newMetas = newDocument.head.querySelectorAll("meta[name], meta[property]");
|
|
79
|
+
for (const newMeta of newMetas) {
|
|
80
|
+
const name = newMeta.getAttribute("name");
|
|
81
|
+
const property = newMeta.getAttribute("property");
|
|
82
|
+
const content = newMeta.getAttribute("content");
|
|
83
|
+
const selector = name ? `meta[name="${name}"]` : `meta[property="${property}"]`;
|
|
84
|
+
const existingMeta = document.head.querySelector(selector);
|
|
85
|
+
if (existingMeta) {
|
|
86
|
+
if (existingMeta.getAttribute("content") !== content) {
|
|
87
|
+
existingMeta.setAttribute("content", content || "");
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
document.head.appendChild(newMeta.cloneNode(true));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const existingScriptSrcs = new Set(
|
|
94
|
+
Array.from(document.head.querySelectorAll("script[src]")).map((s) => s.getAttribute("src"))
|
|
95
|
+
);
|
|
96
|
+
const existingInlineContents = new Set(
|
|
97
|
+
Array.from(document.head.querySelectorAll("script:not([src])")).map((s) => (s.textContent ?? "").trim())
|
|
98
|
+
);
|
|
99
|
+
const allNewHeadScripts = newDocument.head.querySelectorAll("script");
|
|
100
|
+
for (const script of allNewHeadScripts) {
|
|
101
|
+
if (script.hasAttribute("data-eco-rerun")) continue;
|
|
102
|
+
const src = script.getAttribute("src");
|
|
103
|
+
const scriptId = script.getAttribute("data-eco-script-id") || script.getAttribute("id");
|
|
104
|
+
const existingScript = findExistingHeadScript(script);
|
|
105
|
+
if (scriptId && existingScript) {
|
|
106
|
+
if (!isNonExecutableHeadScript(script)) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (areHeadScriptsEquivalent(script, existingScript)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
pendingHeadScripts.push({
|
|
113
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
114
|
+
textContent: script.textContent ?? "",
|
|
115
|
+
src,
|
|
116
|
+
scriptId,
|
|
117
|
+
replaceExisting: true
|
|
118
|
+
});
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (src) {
|
|
122
|
+
if (existingScriptSrcs.has(src)) continue;
|
|
123
|
+
pendingHeadScripts.push({
|
|
124
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
125
|
+
textContent: script.textContent ?? "",
|
|
126
|
+
src,
|
|
127
|
+
scriptId,
|
|
128
|
+
replaceExisting: false
|
|
129
|
+
});
|
|
130
|
+
existingScriptSrcs.add(src);
|
|
131
|
+
} else {
|
|
132
|
+
const content = (script.textContent ?? "").trim();
|
|
133
|
+
if (!content || existingInlineContents.has(content)) continue;
|
|
134
|
+
pendingHeadScripts.push({
|
|
135
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
136
|
+
textContent: script.textContent ?? "",
|
|
137
|
+
src: null,
|
|
138
|
+
scriptId,
|
|
139
|
+
replaceExisting: false
|
|
140
|
+
});
|
|
141
|
+
existingInlineContents.add(content);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
bodyStrategy: pendingRerunScripts.length > 0 ? "replace" : "morph",
|
|
146
|
+
pendingHeadScripts,
|
|
147
|
+
pendingRerunScripts
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function flushHeadScripts(pendingHeadScripts) {
|
|
151
|
+
for (const script of pendingHeadScripts) {
|
|
152
|
+
const replacement = document.createElement("script");
|
|
153
|
+
for (const [name, value] of script.attributes) {
|
|
154
|
+
replacement.setAttribute(name, value);
|
|
155
|
+
}
|
|
156
|
+
replacement.textContent = script.textContent;
|
|
157
|
+
const existingScript = findExistingHeadScript(script);
|
|
158
|
+
if (script.replaceExisting && existingScript) {
|
|
159
|
+
existingScript.replaceWith(replacement);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
document.head.appendChild(replacement);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function flushRerunScripts(pendingRerunScripts) {
|
|
166
|
+
flushPendingRerunScripts(pendingRerunScripts);
|
|
167
|
+
}
|
|
168
|
+
export {
|
|
169
|
+
flushHeadScripts,
|
|
170
|
+
flushRerunScripts,
|
|
171
|
+
getHeadScriptKey,
|
|
172
|
+
morphHead
|
|
173
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared stylesheet href discovery for navigation prefetch and commit.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Returns resolved stylesheet hrefs currently in the document head.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getCurrentStylesheetHrefs(): Set<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Returns stylesheet links present in `source` but not in `existingHrefs`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function discoverNewStylesheetLinks(source: Document, existingHrefs?: Set<string>): HTMLLinkElement[];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function getCurrentStylesheetHrefs() {
|
|
2
|
+
return new Set(
|
|
3
|
+
Array.from(document.head.querySelectorAll('link[rel="stylesheet"]')).map((link) => link.href)
|
|
4
|
+
);
|
|
5
|
+
}
|
|
6
|
+
function discoverNewStylesheetLinks(source, existingHrefs = getCurrentStylesheetHrefs()) {
|
|
7
|
+
const discovered = [];
|
|
8
|
+
for (const link of source.head.querySelectorAll('link[rel="stylesheet"]')) {
|
|
9
|
+
if (existingHrefs.has(link.href)) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
existingHrefs.add(link.href);
|
|
13
|
+
discovered.push(link);
|
|
14
|
+
}
|
|
15
|
+
return discovered;
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
discoverNewStylesheetLinks,
|
|
19
|
+
getCurrentStylesheetHrefs
|
|
20
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Preloads new stylesheets from target document to prevent FOUC.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Discovers stylesheet links in the target document that aren't present in the
|
|
6
|
+
* current document, creates corresponding link elements, and waits for all to
|
|
7
|
+
* load before resolving. This follows Turbo's approach of waiting for stylesheets
|
|
8
|
+
* before any DOM updates.
|
|
9
|
+
*/
|
|
10
|
+
export declare function preloadStylesheets(newDocument: Document): Promise<void>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { discoverNewStylesheetLinks, getCurrentStylesheetHrefs } from "./stylesheet-discovery.js";
|
|
2
|
+
async function preloadStylesheets(newDocument) {
|
|
3
|
+
const newStylesheetLinks = discoverNewStylesheetLinks(newDocument, getCurrentStylesheetHrefs());
|
|
4
|
+
if (newStylesheetLinks.length === 0) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const TIMEOUT = 5e3;
|
|
8
|
+
const loadPromises = newStylesheetLinks.map((link) => {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
const newLink = document.createElement("link");
|
|
11
|
+
newLink.rel = "stylesheet";
|
|
12
|
+
newLink.media = link.media || "all";
|
|
13
|
+
const timeoutId = setTimeout(() => {
|
|
14
|
+
cleanup();
|
|
15
|
+
resolve();
|
|
16
|
+
}, TIMEOUT);
|
|
17
|
+
const cleanup = () => {
|
|
18
|
+
clearTimeout(timeoutId);
|
|
19
|
+
newLink.onload = null;
|
|
20
|
+
newLink.onerror = null;
|
|
21
|
+
};
|
|
22
|
+
newLink.onload = () => {
|
|
23
|
+
cleanup();
|
|
24
|
+
resolve();
|
|
25
|
+
};
|
|
26
|
+
newLink.onerror = () => {
|
|
27
|
+
cleanup();
|
|
28
|
+
resolve();
|
|
29
|
+
};
|
|
30
|
+
newLink.href = link.href;
|
|
31
|
+
document.head.appendChild(newLink);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
await Promise.all(loadPromises);
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
preloadStylesheets
|
|
38
|
+
};
|