@ecopages/browser-router 0.2.0-alpha.5 → 0.2.0-alpha.7
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/CHANGELOG.md +9 -7
- package/README.md +36 -36
- package/package.json +2 -2
- package/src/client/eco-router.d.ts +35 -1
- package/src/client/eco-router.js +335 -75
- package/src/client/eco-router.ts +430 -100
- package/src/client/services/dom-swapper.d.ts +23 -0
- package/src/client/services/dom-swapper.js +210 -38
- package/src/client/services/dom-swapper.ts +296 -45
- package/src/client/services/view-transition-manager.d.ts +7 -1
- package/src/client/services/view-transition-manager.js +10 -5
- package/src/client/services/view-transition-manager.ts +13 -7
- package/src/client/types.d.ts +3 -0
- package/src/client/types.ts +4 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import morphdom from "morphdom";
|
|
2
2
|
const DEFAULT_PERSIST_ATTR = "data-eco-persist";
|
|
3
|
+
const RERUN_SRC_ATTR = "data-eco-rerun-src";
|
|
3
4
|
function isPersisted(element, persistAttribute) {
|
|
4
5
|
return element.hasAttribute(persistAttribute) || element.hasAttribute(DEFAULT_PERSIST_ATTR);
|
|
5
6
|
}
|
|
@@ -8,6 +9,9 @@ function isHydratedCustomElement(element) {
|
|
|
8
9
|
}
|
|
9
10
|
class DomSwapper {
|
|
10
11
|
persistAttribute;
|
|
12
|
+
pendingHeadScripts = [];
|
|
13
|
+
pendingRerunScripts = [];
|
|
14
|
+
rerunNonce = 0;
|
|
11
15
|
constructor(persistAttribute) {
|
|
12
16
|
this.persistAttribute = persistAttribute;
|
|
13
17
|
}
|
|
@@ -78,6 +82,9 @@ class DomSwapper {
|
|
|
78
82
|
* - Injects new scripts from the incoming page that are absent from the current head
|
|
79
83
|
*/
|
|
80
84
|
morphHead(newDocument) {
|
|
85
|
+
this.pendingHeadScripts = [];
|
|
86
|
+
this.pendingRerunScripts = this.collectRerunScripts(newDocument);
|
|
87
|
+
this.removeStaleHeadScripts(newDocument);
|
|
81
88
|
const newTitle = newDocument.head.querySelector("title");
|
|
82
89
|
if (newTitle && document.title !== newTitle.textContent) {
|
|
83
90
|
document.title = newTitle.textContent || "";
|
|
@@ -97,25 +104,6 @@ class DomSwapper {
|
|
|
97
104
|
document.head.appendChild(newMeta.cloneNode(true));
|
|
98
105
|
}
|
|
99
106
|
}
|
|
100
|
-
const existingScriptIds = new Set(
|
|
101
|
-
Array.from(document.head.querySelectorAll("script[data-eco-script-id]")).map(
|
|
102
|
-
(s) => s.getAttribute("data-eco-script-id")
|
|
103
|
-
)
|
|
104
|
-
);
|
|
105
|
-
const rerunScripts = newDocument.head.querySelectorAll("script[data-eco-rerun]");
|
|
106
|
-
for (const script of rerunScripts) {
|
|
107
|
-
const scriptId = script.getAttribute("data-eco-script-id");
|
|
108
|
-
if (scriptId && !existingScriptIds.has(scriptId)) {
|
|
109
|
-
const newScript = document.createElement("script");
|
|
110
|
-
for (const attr of script.attributes) {
|
|
111
|
-
if (attr.name !== "data-eco-rerun") {
|
|
112
|
-
newScript.setAttribute(attr.name, attr.value);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
newScript.textContent = script.textContent;
|
|
116
|
-
document.head.appendChild(newScript);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
107
|
const existingScriptSrcs = new Set(
|
|
120
108
|
Array.from(document.head.querySelectorAll("script[src]")).map((s) => s.getAttribute("src"))
|
|
121
109
|
);
|
|
@@ -126,27 +114,98 @@ class DomSwapper {
|
|
|
126
114
|
for (const script of allNewHeadScripts) {
|
|
127
115
|
if (script.hasAttribute("data-eco-rerun")) continue;
|
|
128
116
|
const src = script.getAttribute("src");
|
|
117
|
+
const scriptId = script.getAttribute("data-eco-script-id") || script.getAttribute("id");
|
|
118
|
+
const existingScript = this.findExistingHeadScript(script);
|
|
119
|
+
if (scriptId && existingScript) {
|
|
120
|
+
if (!this.isNonExecutableHeadScript(script)) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (this.areHeadScriptsEquivalent(script, existingScript)) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
this.pendingHeadScripts.push({
|
|
127
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
128
|
+
textContent: script.textContent ?? "",
|
|
129
|
+
src,
|
|
130
|
+
scriptId,
|
|
131
|
+
replaceExisting: true
|
|
132
|
+
});
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
129
135
|
if (src) {
|
|
130
136
|
if (existingScriptSrcs.has(src)) continue;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
137
|
+
this.pendingHeadScripts.push({
|
|
138
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
139
|
+
textContent: script.textContent ?? "",
|
|
140
|
+
src,
|
|
141
|
+
scriptId,
|
|
142
|
+
replaceExisting: false
|
|
143
|
+
});
|
|
136
144
|
existingScriptSrcs.add(src);
|
|
137
145
|
} else {
|
|
138
146
|
const content = (script.textContent ?? "").trim();
|
|
139
147
|
if (!content || existingInlineContents.has(content)) continue;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
148
|
+
this.pendingHeadScripts.push({
|
|
149
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
150
|
+
textContent: script.textContent ?? "",
|
|
151
|
+
src: null,
|
|
152
|
+
scriptId,
|
|
153
|
+
replaceExisting: false
|
|
154
|
+
});
|
|
146
155
|
existingInlineContents.add(content);
|
|
147
156
|
}
|
|
148
157
|
}
|
|
149
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Replays queued `data-eco-rerun` scripts after the body swap completes.
|
|
161
|
+
*
|
|
162
|
+
* Scripts are intentionally flushed after the new body is in place so DOM-
|
|
163
|
+
* dependent bootstraps bind against the incoming page rather than the page
|
|
164
|
+
* being replaced.
|
|
165
|
+
*/
|
|
166
|
+
flushRerunScripts() {
|
|
167
|
+
for (const script of this.pendingHeadScripts) {
|
|
168
|
+
const replacement = document.createElement("script");
|
|
169
|
+
for (const [name, value] of script.attributes) {
|
|
170
|
+
replacement.setAttribute(name, value);
|
|
171
|
+
}
|
|
172
|
+
replacement.textContent = script.textContent;
|
|
173
|
+
const existingScript = this.findExistingHeadScript(script);
|
|
174
|
+
if (script.replaceExisting && existingScript) {
|
|
175
|
+
existingScript.replaceWith(replacement);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
document.head.appendChild(replacement);
|
|
179
|
+
}
|
|
180
|
+
this.pendingHeadScripts = [];
|
|
181
|
+
for (const script of this.pendingRerunScripts) {
|
|
182
|
+
const targetParent = script.parent === "body" ? document.body : document.head;
|
|
183
|
+
const replacement = document.createElement("script");
|
|
184
|
+
const shouldBustModuleSrc = this.isExternalModuleRerunScript(script);
|
|
185
|
+
for (const [name, value] of script.attributes) {
|
|
186
|
+
if (name === "data-eco-rerun") {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (name === "src" && shouldBustModuleSrc) {
|
|
190
|
+
replacement.setAttribute(RERUN_SRC_ATTR, value);
|
|
191
|
+
replacement.setAttribute("src", this.createRerunScriptUrl(value));
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
replacement.setAttribute(name, value);
|
|
195
|
+
}
|
|
196
|
+
replacement.textContent = script.textContent;
|
|
197
|
+
const existingScript = this.findExistingRerunScript(targetParent, script);
|
|
198
|
+
if (existingScript) {
|
|
199
|
+
existingScript.replaceWith(replacement);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
targetParent.appendChild(replacement);
|
|
203
|
+
}
|
|
204
|
+
this.pendingRerunScripts = [];
|
|
205
|
+
}
|
|
206
|
+
shouldReplaceBodyForRerunScripts() {
|
|
207
|
+
return this.pendingRerunScripts.length > 0;
|
|
208
|
+
}
|
|
150
209
|
/**
|
|
151
210
|
* Detects custom elements without shadow DOM (light-DOM custom elements).
|
|
152
211
|
* These need full replacement rather than morphing, because morphdom would
|
|
@@ -155,6 +214,15 @@ class DomSwapper {
|
|
|
155
214
|
isLightDomCustomElement(element) {
|
|
156
215
|
return element.localName.includes("-") && element.shadowRoot === null;
|
|
157
216
|
}
|
|
217
|
+
replaceCustomElement(fromEl, toEl) {
|
|
218
|
+
const newEl = document.createElement(toEl.tagName);
|
|
219
|
+
for (const attr of toEl.attributes) {
|
|
220
|
+
newEl.setAttribute(attr.name, attr.value);
|
|
221
|
+
}
|
|
222
|
+
newEl.innerHTML = toEl.innerHTML;
|
|
223
|
+
fromEl.replaceWith(newEl);
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
158
226
|
/**
|
|
159
227
|
* Morphs document body using morphdom.
|
|
160
228
|
* Preserves persisted elements and hydrated custom elements.
|
|
@@ -169,16 +237,10 @@ class DomSwapper {
|
|
|
169
237
|
return false;
|
|
170
238
|
}
|
|
171
239
|
if (isHydratedCustomElement(fromEl)) {
|
|
172
|
-
return
|
|
240
|
+
return this.replaceCustomElement(fromEl, toEl);
|
|
173
241
|
}
|
|
174
242
|
if (this.isLightDomCustomElement(fromEl)) {
|
|
175
|
-
|
|
176
|
-
for (const attr of toEl.attributes) {
|
|
177
|
-
newEl.setAttribute(attr.name, attr.value);
|
|
178
|
-
}
|
|
179
|
-
newEl.innerHTML = toEl.innerHTML;
|
|
180
|
-
fromEl.replaceWith(newEl);
|
|
181
|
-
return false;
|
|
243
|
+
return this.replaceCustomElement(fromEl, toEl);
|
|
182
244
|
}
|
|
183
245
|
if (fromEl.isEqualNode(toEl)) {
|
|
184
246
|
return false;
|
|
@@ -214,6 +276,116 @@ class DomSwapper {
|
|
|
214
276
|
document.body.replaceChildren(...newDocument.body.childNodes);
|
|
215
277
|
this.processDeclarativeShadowDOM(document.body);
|
|
216
278
|
}
|
|
279
|
+
collectRerunScripts(newDocument) {
|
|
280
|
+
return Array.from(newDocument.querySelectorAll("script[data-eco-rerun]")).map((script) => ({
|
|
281
|
+
parent: script.closest("body") ? "body" : "head",
|
|
282
|
+
attributes: Array.from(script.attributes).map((attr) => [attr.name, attr.value]),
|
|
283
|
+
textContent: script.textContent ?? "",
|
|
284
|
+
src: script.getAttribute("src"),
|
|
285
|
+
scriptId: script.getAttribute("data-eco-script-id")
|
|
286
|
+
}));
|
|
287
|
+
}
|
|
288
|
+
removeStaleHeadScripts(newDocument) {
|
|
289
|
+
const nextScriptKeys = new Set(
|
|
290
|
+
Array.from(newDocument.head.querySelectorAll("script")).map((script) => this.getHeadScriptKey(script)).filter((key) => key !== null)
|
|
291
|
+
);
|
|
292
|
+
for (const script of Array.from(document.head.querySelectorAll("script"))) {
|
|
293
|
+
const key = this.getHeadScriptKey(script);
|
|
294
|
+
if (!key || nextScriptKeys.has(key)) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (this.shouldPersistExecutableInlineHeadScript(script)) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
script.remove();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
shouldPersistExecutableInlineHeadScript(script) {
|
|
304
|
+
const scriptId = script.getAttribute("data-eco-script-id") || script.getAttribute("id");
|
|
305
|
+
if (!scriptId) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
if (script.hasAttribute("data-eco-rerun")) {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
if (script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute("src")) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
return !this.isNonExecutableHeadScript(script);
|
|
315
|
+
}
|
|
316
|
+
isNonExecutableHeadScript(script) {
|
|
317
|
+
const type = (script.getAttribute("type") ?? "").trim().toLowerCase();
|
|
318
|
+
if (!type) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
return ![
|
|
322
|
+
"application/javascript",
|
|
323
|
+
"application/ecmascript",
|
|
324
|
+
"module",
|
|
325
|
+
"text/ecmascript",
|
|
326
|
+
"text/javascript"
|
|
327
|
+
].includes(type);
|
|
328
|
+
}
|
|
329
|
+
areHeadScriptsEquivalent(nextScript, currentScript) {
|
|
330
|
+
if (this.getHeadScriptKey(nextScript) !== this.getHeadScriptKey(currentScript)) {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
if ((nextScript.textContent ?? "") !== (currentScript.textContent ?? "")) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
const nextAttributes = Array.from(nextScript.attributes).map((attribute) => [attribute.name, attribute.value]);
|
|
337
|
+
const currentAttributes = Array.from(currentScript.attributes).map((attribute) => [
|
|
338
|
+
attribute.name,
|
|
339
|
+
attribute.value
|
|
340
|
+
]);
|
|
341
|
+
if (nextAttributes.length !== currentAttributes.length) {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
return nextAttributes.every(
|
|
345
|
+
([name, value], index) => currentAttributes[index]?.[0] === name && currentAttributes[index]?.[1] === value
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
getHeadScriptKey(script) {
|
|
349
|
+
const scriptId = script instanceof HTMLScriptElement ? script.getAttribute("data-eco-script-id") || script.getAttribute("id") : script.scriptId;
|
|
350
|
+
if (scriptId) {
|
|
351
|
+
return `id:${scriptId}`;
|
|
352
|
+
}
|
|
353
|
+
const src = script instanceof HTMLScriptElement ? script.getAttribute(RERUN_SRC_ATTR) || script.getAttribute("src") : script.src;
|
|
354
|
+
if (src) {
|
|
355
|
+
return `src:${src}`;
|
|
356
|
+
}
|
|
357
|
+
const textContent = (script.textContent ?? "").trim();
|
|
358
|
+
return textContent ? `inline:${textContent}` : null;
|
|
359
|
+
}
|
|
360
|
+
findExistingHeadScript(script) {
|
|
361
|
+
const scriptKey = this.getHeadScriptKey(script);
|
|
362
|
+
if (!scriptKey) {
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
return Array.from(document.head.querySelectorAll("script")).find(
|
|
366
|
+
(candidate) => this.getHeadScriptKey(candidate) === scriptKey
|
|
367
|
+
) ?? null;
|
|
368
|
+
}
|
|
369
|
+
findExistingRerunScript(root, script) {
|
|
370
|
+
const scripts = Array.from(root.querySelectorAll("script"));
|
|
371
|
+
if (script.scriptId) {
|
|
372
|
+
return scripts.find((candidate) => candidate.getAttribute("data-eco-script-id") === script.scriptId) ?? null;
|
|
373
|
+
}
|
|
374
|
+
return scripts.find(
|
|
375
|
+
(candidate) => (candidate.getAttribute(RERUN_SRC_ATTR) ?? candidate.getAttribute("src")) === script.src && (candidate.textContent ?? "") === script.textContent
|
|
376
|
+
) ?? null;
|
|
377
|
+
}
|
|
378
|
+
isExternalModuleRerunScript(script) {
|
|
379
|
+
if (!script.src) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
return script.attributes.some(([name, value]) => name === "type" && value === "module");
|
|
383
|
+
}
|
|
384
|
+
createRerunScriptUrl(src) {
|
|
385
|
+
const url = new URL(src, document.baseURI);
|
|
386
|
+
url.searchParams.set("__eco_rerun", String(++this.rerunNonce));
|
|
387
|
+
return url.toString();
|
|
388
|
+
}
|
|
217
389
|
/**
|
|
218
390
|
* Manually attaches declarative shadow DOM templates.
|
|
219
391
|
* Browsers only process `<template shadowrootmode>` during initial parse.
|