@ecopages/core 0.6.0 → 0.6.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 +1 -1
- package/package.json +3 -3
- package/src/router/client/navigation-commit.js +16 -2
- package/src/router/client/navigation-head-order.fixture.css +3 -0
- package/src/router/client/navigation-head.d.ts +2 -1
- package/src/router/client/navigation-head.fixture.css +3 -0
- package/src/router/client/navigation-head.js +222 -39
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ The important ownership rules are:
|
|
|
35
35
|
- Watch-mode resources belong to one `DevSession`, including the watcher, HMR manager, client bridge, Page prewarm, and Dev Toolbar. `EcopagesApp` shares one readiness promise across concurrent startup and request-only calls; after `stop()`, `start()` and `fetch()` initialize a fresh server adapter.
|
|
36
36
|
- App shutdown tears down Integration runtime resources, including a Lit static-export worker if one is still running. Serving does not start that worker. See [Runtime Adapters](src/adapters/README.md).
|
|
37
37
|
- HMR and invalidation use shared graph-aware services instead of runtime-specific ad hoc wiring. One Development Invalidation service belongs to each `AppRuntime` and is shared by its Project Watcher and HMR manager. Editing a `source` module used by both a server route and a browser entrypoint reloads the browser so server-rendered markup and the island refresh together.
|
|
38
|
-
- Browser and React navigation use one core-owned document commit pipeline; each router supplies its page swap and scroll handling while core coordinates fetch/cache/parse, head reconciliation, script activation, view transitions, and lifecycle events. A later navigation supersedes cleanup from earlier transitions, including when it skips animation; a failed or uncommitted page swap clears transition names. Browser-router always morphs the incoming body, keeping nodes with matching persistence keys attached when their ancestor structure remains compatible; during rerun refresh, custom-element descendants on that path also stay in place when their markup remains structurally compatible. Superseded stylesheet waits settle when a later page removes their staged links. For rerun scripts matched by relative `src` (without an explicit script id or HTML `id`), core keeps identity stable across pages with different `<base>` URLs while loading external modules from the incoming page's resolved URL. See [Router Layer](src/router/README.md).
|
|
38
|
+
- Browser and React navigation use one core-owned document commit pipeline; each router supplies its page swap and scroll handling while core coordinates fetch/cache/parse, head reconciliation, script activation, view transitions, and lifecycle events. Head reconciliation avoids rewriting unchanged resolved link URLs, stages new stylesheets without applying them to the outgoing page, and preserves the destination order of uniquely keyed CSS by staging replacements when retained stylesheets need to change order; it requests a full document navigation if a replacement fails or retained CSS changes order while a retained style or stylesheet is persisted. A later navigation supersedes cleanup from earlier transitions, including when it skips animation; a failed or uncommitted page swap clears transition names. Browser-router always morphs the incoming body, keeping nodes with matching persistence keys attached when their ancestor structure remains compatible; during rerun refresh, custom-element descendants on that path also stay in place when their markup remains structurally compatible. Superseded stylesheet waits settle when a later page removes their staged links. For rerun scripts matched by relative `src` (without an explicit script id or HTML `id`), core keeps identity stable across pages with different `<base>` URLs while loading external modules from the incoming page's resolved URL. See [Router Layer](src/router/README.md).
|
|
39
39
|
- `serveDistFile()` handles runtime static fallbacks and preview through the app's `RuntimeHost`, serving only `dist/client/` with real-path containment, HEAD and 405 responses, gzip negotiation, ETags, conditional 304s, and the shared cache policy. The private `dist/server/` output is a sibling of the served directory. `immutable` applies only to outputs the build recorded as content-hashed; HTML stays revalidated. See [Runtime Adapters](src/adapters/README.md).
|
|
40
40
|
- The build adapter returns each build's plugin-recorded inputs by module alongside source-module closures, and passes an explicit recorder to Rolldown plugin hooks. Cache owners bind only the paths read by their entry closure, so development invalidates affected Page Browser Graphs, HTML entries, route modules, and browser entrypoints. Grouped browser scripts merge external modules, compatible plugin instances, and non-conflicting define values within their build group, disable splitting when any entry opts out, resolve by the path or name the caller passed (and the bundler's facade when that spelling differs), and cache only complete groups with matching dependency sets. Static production export combines grouped Page entries and HTML module scripts in compatible app-wide browser build groups, saves their route associations in the server manifest, and lets `ecopages start` restore listed route assets before SSR requests. Non-exact route templates also get a parameter-free plan without running route data hooks, Dynamic Pages that declare `staticPaths` get a per-entry plan, and so does every `app.static` route instance. Later params reuse a planned graph when grouped browser dependencies and page-client entries match. After `ecopages start` restores the manifest, a request whose graph is not listed is an error that names the route and the missing entry; it is never built. Entry-specific plugins with a `bundleGroupKey` retain their own configuration inside the shared build, while entries with different bundle options keep separate requests. In-process Page caches also track reachable generated artifacts. On-demand page-cache path invalidation removes query and development runtime variants of the pathname and releases their build-input bindings. See [Build Layer](src/build/README.md), [Page Cache](src/page-cache/README.md), and [Asset Processing](src/assets/README.md).
|
|
41
41
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/core",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Core package for Ecopages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@clack/prompts": "^1.7.0",
|
|
24
|
-
"@ecopages/file-system": "0.6.
|
|
24
|
+
"@ecopages/file-system": "0.6.1",
|
|
25
25
|
"@ecopages/logger": "^0.2.3",
|
|
26
26
|
"@ecopages/scripts-injector": "^0.1.5",
|
|
27
27
|
"@jridgewell/remapping": "^2.3.5",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"ws": "^8.20.1"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@ecopages/dev-toolbar": "0.6.
|
|
37
|
+
"@ecopages/dev-toolbar": "0.6.1"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@ecopages/dev-toolbar": {
|
|
@@ -2,6 +2,10 @@ import { completeNavigationLifecycle, dispatchBeforeSwap } from './navigation-li
|
|
|
2
2
|
import { activatePreparedNavigationScripts, prepareNavigationHead, } from './navigation-head.js';
|
|
3
3
|
import { runNavigationViewTransition } from './navigation-view-transition.js';
|
|
4
4
|
import { ECO_PERSIST_ATTRIBUTE } from '../../client/navigation-attributes.js';
|
|
5
|
+
async function requestReload(input) {
|
|
6
|
+
await input.onReloadRequested?.();
|
|
7
|
+
return input.isStale() ? 'stale' : 'reload-requested';
|
|
8
|
+
}
|
|
5
9
|
/** Runs shared document work around the router-specific page swap. */
|
|
6
10
|
export async function commitNavigationPage(input) {
|
|
7
11
|
let headUpdate = null;
|
|
@@ -14,20 +18,28 @@ export async function commitNavigationPage(input) {
|
|
|
14
18
|
if (input.isStale())
|
|
15
19
|
return 'stale';
|
|
16
20
|
if (requestedReload) {
|
|
17
|
-
await input
|
|
18
|
-
return input.isStale() ? 'stale' : 'reload-requested';
|
|
21
|
+
return await requestReload(input);
|
|
19
22
|
}
|
|
20
23
|
const preparedHeadUpdate = await prepareNavigationHead(input.newDocument, input.persistAttribute ?? ECO_PERSIST_ATTRIBUTE, input.skipScriptActivation);
|
|
21
24
|
headUpdate = preparedHeadUpdate;
|
|
22
25
|
if (input.isStale())
|
|
23
26
|
return 'stale';
|
|
27
|
+
if (preparedHeadUpdate.requiresReload) {
|
|
28
|
+
return await requestReload(input);
|
|
29
|
+
}
|
|
24
30
|
await input.beforeTransition?.();
|
|
25
31
|
if (input.isStale())
|
|
26
32
|
return 'stale';
|
|
27
33
|
let committed = false;
|
|
34
|
+
let reloadRequested = false;
|
|
28
35
|
await runNavigationViewTransition(async () => {
|
|
29
36
|
if (input.isStale())
|
|
30
37
|
return false;
|
|
38
|
+
if (preparedHeadUpdate.requiresReload) {
|
|
39
|
+
reloadRequested = true;
|
|
40
|
+
await requestReload(input);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
31
43
|
input.beforeCommit?.();
|
|
32
44
|
if (input.isStale())
|
|
33
45
|
return false;
|
|
@@ -45,6 +57,8 @@ export async function commitNavigationPage(input) {
|
|
|
45
57
|
committed = true;
|
|
46
58
|
return true;
|
|
47
59
|
}, { enabled: input.viewTransitionsEnabled, incomingDocument: input.newDocument });
|
|
60
|
+
if (reloadRequested)
|
|
61
|
+
return input.isStale() ? 'stale' : 'reload-requested';
|
|
48
62
|
if (input.isStale() || !committed)
|
|
49
63
|
return 'stale';
|
|
50
64
|
completeNavigationLifecycle(document, { url: input.url, direction: input.direction }, { isStale: input.isStale });
|
|
@@ -2,11 +2,12 @@ import { type ScriptActivation } from '../../client/navigation-scripts/activatio
|
|
|
2
2
|
export type NavigationHeadUpdate = {
|
|
3
3
|
scriptActivations: ScriptActivation[];
|
|
4
4
|
rerunScriptActivations: ScriptActivation[];
|
|
5
|
+
requiresReload: boolean;
|
|
5
6
|
commit(): void;
|
|
6
7
|
cancel(): void;
|
|
7
8
|
};
|
|
8
9
|
/**
|
|
9
|
-
* Stages incoming stylesheets and prepares one diff for either router to commit.
|
|
10
|
+
* Stages incoming stylesheets without applying them and prepares one diff for either router to commit.
|
|
10
11
|
*/
|
|
11
12
|
export declare function prepareNavigationHead(newDocument: Document, persistAttribute?: string, shouldSkipScriptActivation?: (script: HTMLScriptElement) => boolean): Promise<NavigationHeadUpdate>;
|
|
12
13
|
/** Starts the shared head and body script queue after an adapter has swapped its page. */
|
|
@@ -3,6 +3,8 @@ import { collectRerunScripts, isNonExecutableHeadScript, isRerunScript, shouldPe
|
|
|
3
3
|
import { getNavigationScriptIdentity } from '../../client/navigation-scripts/identity.js';
|
|
4
4
|
import { ECO_PERSIST_ATTRIBUTE } from '../../client/navigation-attributes.js';
|
|
5
5
|
const stagedStylesheetLoads = new Map();
|
|
6
|
+
const pendingStylesheetLinks = new Map();
|
|
7
|
+
let latestHeadUpdateGeneration = 0;
|
|
6
8
|
function resolveMetaElementKey(element) {
|
|
7
9
|
const identity = element.getAttribute('name') ||
|
|
8
10
|
element.getAttribute('property') ||
|
|
@@ -47,6 +49,8 @@ function indexHeadChildren(head) {
|
|
|
47
49
|
for (const element of Array.from(head.children)) {
|
|
48
50
|
if (element.hasAttribute('data-eco-injected'))
|
|
49
51
|
continue;
|
|
52
|
+
if (element instanceof HTMLLinkElement && pendingStylesheetLinks.has(element))
|
|
53
|
+
continue;
|
|
50
54
|
const key = resolveHeadElementKey(element);
|
|
51
55
|
if (key)
|
|
52
56
|
elements.set(key, element);
|
|
@@ -84,19 +88,100 @@ function syncAttributes(current, incoming) {
|
|
|
84
88
|
}
|
|
85
89
|
for (const attribute of Array.from(incoming.attributes)) {
|
|
86
90
|
if ((incoming.tagName === 'LINK' || incoming.tagName === 'BASE') && attribute.name === 'href') {
|
|
87
|
-
|
|
91
|
+
const href = incoming.href;
|
|
92
|
+
if (current.href !== href)
|
|
93
|
+
current.setAttribute('href', href);
|
|
88
94
|
}
|
|
89
95
|
else {
|
|
90
96
|
current.setAttribute(attribute.name, attribute.value);
|
|
91
97
|
}
|
|
92
98
|
}
|
|
93
99
|
}
|
|
100
|
+
function isCascadeOrderedElement(key, element) {
|
|
101
|
+
return element.tagName === 'STYLE' || key.startsWith('link:stylesheet:');
|
|
102
|
+
}
|
|
103
|
+
function insertIncomingHeadElement(element, key, incomingElements, currentElements) {
|
|
104
|
+
let anchor = null;
|
|
105
|
+
if (isCascadeOrderedElement(key, element)) {
|
|
106
|
+
let found = false;
|
|
107
|
+
for (const [nextKey, nextIncoming] of incomingElements) {
|
|
108
|
+
if (nextKey === key) {
|
|
109
|
+
found = true;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (!found || !isCascadeOrderedElement(nextKey, nextIncoming))
|
|
113
|
+
continue;
|
|
114
|
+
anchor = currentElements.get(nextKey) ?? null;
|
|
115
|
+
if (anchor)
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (anchor) {
|
|
120
|
+
document.head.insertBefore(element, anchor);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
document.head.appendChild(element);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function isInIncomingCascadePosition(key, element, incomingElements, currentElements) {
|
|
127
|
+
let before = true;
|
|
128
|
+
for (const [otherKey, incoming] of incomingElements) {
|
|
129
|
+
if (otherKey === key) {
|
|
130
|
+
before = false;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (!isCascadeOrderedElement(otherKey, incoming))
|
|
134
|
+
continue;
|
|
135
|
+
const other = currentElements.get(otherKey);
|
|
136
|
+
if (!other || other === element)
|
|
137
|
+
continue;
|
|
138
|
+
const otherFollows = Boolean(element.compareDocumentPosition(other) & Node.DOCUMENT_POSITION_FOLLOWING);
|
|
139
|
+
if (otherFollows === before)
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
function cascadeOrderChanged(incoming, current) {
|
|
145
|
+
const retained = new Map([...current].filter(([, element]) => !(element instanceof HTMLLinkElement && pendingStylesheetLinks.has(element))));
|
|
146
|
+
const incomingKeys = [...incoming]
|
|
147
|
+
.filter(([key, element]) => isCascadeOrderedElement(key, element) && retained.has(key))
|
|
148
|
+
.map(([key]) => key);
|
|
149
|
+
const currentKeys = [...retained]
|
|
150
|
+
.filter(([key, element]) => isCascadeOrderedElement(key, element) && incoming.has(key))
|
|
151
|
+
.map(([key]) => key);
|
|
152
|
+
return incomingKeys.some((key, index) => currentKeys[index] !== key);
|
|
153
|
+
}
|
|
94
154
|
function isPersisted(element, persistAttribute) {
|
|
95
155
|
return (element.hasAttribute(persistAttribute) ||
|
|
96
156
|
element.hasAttribute(ECO_PERSIST_ATTRIBUTE) ||
|
|
97
157
|
element.matches('meta[charset]') ||
|
|
98
158
|
shouldPersistExecutableInlineHeadScript(element));
|
|
99
159
|
}
|
|
160
|
+
function hasPersistedCascadeReorder(incoming, current, persistAttribute) {
|
|
161
|
+
return (cascadeOrderChanged(incoming, current) &&
|
|
162
|
+
[...current].some(([key, element]) => incoming.has(key) && isCascadeOrderedElement(key, element) && isPersisted(element, persistAttribute)));
|
|
163
|
+
}
|
|
164
|
+
function stylesheetOrderChanged(incoming, active, staged) {
|
|
165
|
+
let previous;
|
|
166
|
+
for (const key of incoming.keys()) {
|
|
167
|
+
if (!key.startsWith('link:stylesheet:'))
|
|
168
|
+
continue;
|
|
169
|
+
const stage = staged.get(key);
|
|
170
|
+
const current = stage && !stage.committed ? stage.link : active.get(key);
|
|
171
|
+
if (!current)
|
|
172
|
+
return true;
|
|
173
|
+
if (previous && !(previous.compareDocumentPosition(current) & Node.DOCUMENT_POSITION_FOLLOWING))
|
|
174
|
+
return true;
|
|
175
|
+
previous = current;
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
function hasPersistedStylesheetForStage(key, stage, persistAttribute) {
|
|
180
|
+
return Array.from(document.head.children).some((element) => element !== stage.link &&
|
|
181
|
+
!(element instanceof HTMLLinkElement && pendingStylesheetLinks.has(element)) &&
|
|
182
|
+
resolveHeadElementKey(element) === key &&
|
|
183
|
+
isPersisted(element, persistAttribute));
|
|
184
|
+
}
|
|
100
185
|
function createHeadScriptActivations(newDocument, currentElements, shouldSkip) {
|
|
101
186
|
const knownScriptKeys = new Set([...currentElements.keys()].filter((key) => key.startsWith('script:')));
|
|
102
187
|
const activations = [];
|
|
@@ -130,100 +215,198 @@ function waitForStylesheet(link) {
|
|
|
130
215
|
let timeout;
|
|
131
216
|
const observer = new MutationObserver(() => {
|
|
132
217
|
if (!link.isConnected)
|
|
133
|
-
done();
|
|
218
|
+
done(false);
|
|
134
219
|
});
|
|
135
|
-
const done = () => {
|
|
220
|
+
const done = (loaded) => {
|
|
136
221
|
clearTimeout(timeout);
|
|
137
222
|
observer.disconnect();
|
|
138
|
-
link.removeEventListener('load',
|
|
139
|
-
link.removeEventListener('error',
|
|
140
|
-
resolve();
|
|
223
|
+
link.removeEventListener('load', onLoad);
|
|
224
|
+
link.removeEventListener('error', onError);
|
|
225
|
+
resolve(loaded);
|
|
141
226
|
};
|
|
142
|
-
|
|
227
|
+
const onLoad = () => done(true);
|
|
228
|
+
const onError = () => done(false);
|
|
229
|
+
timeout = setTimeout(() => done(false), 5000);
|
|
143
230
|
observer.observe(document.head, { childList: true, subtree: true });
|
|
144
|
-
link.addEventListener('load',
|
|
145
|
-
link.addEventListener('error',
|
|
231
|
+
link.addEventListener('load', onLoad, { once: true });
|
|
232
|
+
link.addEventListener('error', onError, { once: true });
|
|
146
233
|
});
|
|
147
234
|
}
|
|
148
|
-
async function
|
|
235
|
+
async function replacementStylesheetsLoaded(currentElements, stagedStylesheets) {
|
|
236
|
+
for (const [key, stage] of stagedStylesheets) {
|
|
237
|
+
if (currentElements.has(key) && !(await stage.loaded))
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
async function preloadIncomingStylesheets(incomingElements, currentElements, stagedStylesheets, generation) {
|
|
149
243
|
const loadPromises = [];
|
|
150
|
-
|
|
244
|
+
const reorder = cascadeOrderChanged(incomingElements, currentElements);
|
|
245
|
+
const placementElements = new Map(currentElements);
|
|
246
|
+
if (reorder) {
|
|
247
|
+
for (const [key] of incomingElements) {
|
|
248
|
+
if (key.startsWith('link:stylesheet:'))
|
|
249
|
+
placementElements.delete(key);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const entries = [...incomingElements];
|
|
253
|
+
const signature = JSON.stringify(entries.filter(([key, element]) => isCascadeOrderedElement(key, element)).map(([key]) => key));
|
|
254
|
+
if (reorder)
|
|
255
|
+
entries.reverse();
|
|
256
|
+
for (const [key, element] of entries) {
|
|
151
257
|
if (!key.startsWith('link:stylesheet:'))
|
|
152
258
|
continue;
|
|
153
|
-
const
|
|
154
|
-
|
|
259
|
+
const registryKey = JSON.stringify([signature, key]);
|
|
260
|
+
const existingStage = stagedStylesheetLoads.get(registryKey);
|
|
261
|
+
if (existingStage &&
|
|
262
|
+
existingStage.link.isConnected &&
|
|
263
|
+
isInIncomingCascadePosition(key, existingStage.link, incomingElements, currentElements)) {
|
|
155
264
|
existingStage.users += 1;
|
|
265
|
+
existingStage.latestGeneration = generation;
|
|
156
266
|
stagedStylesheets.set(key, existingStage);
|
|
157
267
|
loadPromises.push(existingStage.loaded);
|
|
268
|
+
placementElements.set(key, existingStage.link);
|
|
158
269
|
continue;
|
|
159
270
|
}
|
|
160
|
-
if (currentElements.has(key))
|
|
271
|
+
if (currentElements.has(key) && !existingStage && !reorder)
|
|
161
272
|
continue;
|
|
162
273
|
const link = cloneHeadElement(element);
|
|
163
|
-
|
|
164
|
-
|
|
274
|
+
link.media = 'not all';
|
|
275
|
+
const stage = {
|
|
276
|
+
link,
|
|
277
|
+
loaded: waitForStylesheet(link),
|
|
278
|
+
users: 1,
|
|
279
|
+
committed: false,
|
|
280
|
+
registryKey,
|
|
281
|
+
loadedSuccessfully: null,
|
|
282
|
+
latestGeneration: generation,
|
|
283
|
+
};
|
|
284
|
+
void stage.loaded.then((loaded) => {
|
|
285
|
+
stage.loadedSuccessfully = loaded;
|
|
286
|
+
});
|
|
287
|
+
pendingStylesheetLinks.set(link, stage);
|
|
288
|
+
stagedStylesheetLoads.set(registryKey, stage);
|
|
165
289
|
stagedStylesheets.set(key, stage);
|
|
166
290
|
loadPromises.push(stage.loaded);
|
|
167
|
-
|
|
291
|
+
insertIncomingHeadElement(link, key, incomingElements, placementElements);
|
|
292
|
+
placementElements.set(key, link);
|
|
168
293
|
}
|
|
169
294
|
await Promise.all(loadPromises);
|
|
295
|
+
return await replacementStylesheetsLoaded(currentElements, stagedStylesheets);
|
|
296
|
+
}
|
|
297
|
+
function activateStagedStylesheet(key, stage, incoming) {
|
|
298
|
+
for (const candidate of Array.from(document.head.children)) {
|
|
299
|
+
if (candidate === stage.link || resolveHeadElementKey(candidate) !== key)
|
|
300
|
+
continue;
|
|
301
|
+
if (candidate instanceof HTMLLinkElement && pendingStylesheetLinks.has(candidate))
|
|
302
|
+
continue;
|
|
303
|
+
candidate.remove();
|
|
304
|
+
}
|
|
305
|
+
syncHeadElement(stage.link, incoming);
|
|
306
|
+
}
|
|
307
|
+
function reconcileIncomingHead(incomingElements, stagedStylesheets) {
|
|
308
|
+
const current = indexHeadChildren(document.head);
|
|
309
|
+
for (const [key, incoming] of incomingElements) {
|
|
310
|
+
if (incoming.tagName === 'SCRIPT' && !isNonExecutableHeadScript(incoming))
|
|
311
|
+
continue;
|
|
312
|
+
const existing = current.get(key);
|
|
313
|
+
const staged = stagedStylesheets.get(key);
|
|
314
|
+
if (staged && !staged.committed) {
|
|
315
|
+
activateStagedStylesheet(key, staged, incoming);
|
|
316
|
+
current.set(key, staged.link);
|
|
317
|
+
}
|
|
318
|
+
else if (existing) {
|
|
319
|
+
syncHeadElement(existing, incoming);
|
|
320
|
+
}
|
|
321
|
+
else if (!staged) {
|
|
322
|
+
const clone = cloneHeadElement(incoming);
|
|
323
|
+
insertIncomingHeadElement(clone, key, incomingElements, current);
|
|
324
|
+
current.set(key, clone);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
for (const [key, incoming] of [...incomingElements].reverse()) {
|
|
328
|
+
if (incoming.tagName !== 'STYLE')
|
|
329
|
+
continue;
|
|
330
|
+
const style = current.get(key);
|
|
331
|
+
if (style && !isInIncomingCascadePosition(key, style, incomingElements, current)) {
|
|
332
|
+
insertIncomingHeadElement(style, key, incomingElements, current);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return current;
|
|
170
336
|
}
|
|
171
337
|
/**
|
|
172
|
-
* Stages incoming stylesheets and prepares one diff for either router to commit.
|
|
338
|
+
* Stages incoming stylesheets without applying them and prepares one diff for either router to commit.
|
|
173
339
|
*/
|
|
174
340
|
export async function prepareNavigationHead(newDocument, persistAttribute = ECO_PERSIST_ATTRIBUTE, shouldSkipScriptActivation = () => false) {
|
|
341
|
+
const generation = ++latestHeadUpdateGeneration;
|
|
175
342
|
const incomingElements = indexHeadChildren(newDocument.head);
|
|
176
343
|
const currentElements = indexHeadChildren(document.head);
|
|
177
344
|
const pendingRerunScripts = collectRerunScripts(newDocument, (script) => !shouldSkipScriptActivation(script));
|
|
178
345
|
const scriptActivations = createHeadScriptActivations(newDocument, currentElements, shouldSkipScriptActivation);
|
|
179
346
|
const rerunScriptActivations = pendingRerunScripts.map(toRerunScriptActivation);
|
|
180
347
|
const stagedStylesheets = new Map();
|
|
181
|
-
|
|
348
|
+
const hasPersistedReorder = hasPersistedCascadeReorder(incomingElements, currentElements, persistAttribute);
|
|
349
|
+
const requiresReload = hasPersistedReorder ||
|
|
350
|
+
!(await preloadIncomingStylesheets(incomingElements, currentElements, stagedStylesheets, generation));
|
|
182
351
|
let committed = false;
|
|
183
352
|
let cancelled = false;
|
|
353
|
+
const reloadRequired = () => {
|
|
354
|
+
if (requiresReload)
|
|
355
|
+
return true;
|
|
356
|
+
const active = indexHeadChildren(document.head);
|
|
357
|
+
if (hasPersistedCascadeReorder(incomingElements, active, persistAttribute))
|
|
358
|
+
return true;
|
|
359
|
+
if (stylesheetOrderChanged(incomingElements, active, stagedStylesheets))
|
|
360
|
+
return true;
|
|
361
|
+
for (const [key, stage] of stagedStylesheets) {
|
|
362
|
+
if (!stage.link.isConnected || (active.has(key) && stage.loadedSuccessfully === false))
|
|
363
|
+
return true;
|
|
364
|
+
if (hasPersistedStylesheetForStage(key, stage, persistAttribute))
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
return false;
|
|
368
|
+
};
|
|
184
369
|
return {
|
|
185
370
|
scriptActivations,
|
|
186
371
|
rerunScriptActivations,
|
|
372
|
+
get requiresReload() {
|
|
373
|
+
return reloadRequired();
|
|
374
|
+
},
|
|
187
375
|
commit() {
|
|
188
376
|
if (committed || cancelled)
|
|
189
377
|
return;
|
|
378
|
+
if (reloadRequired())
|
|
379
|
+
throw new Error('Navigation head update requires a full document navigation');
|
|
190
380
|
committed = true;
|
|
191
|
-
const current =
|
|
192
|
-
for (const [
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const staged = stagedStylesheets.get(key);
|
|
197
|
-
if (existing) {
|
|
198
|
-
if (staged && staged.link !== existing)
|
|
199
|
-
staged.link.remove();
|
|
200
|
-
syncHeadElement(existing, incoming);
|
|
201
|
-
}
|
|
202
|
-
else if (!staged) {
|
|
203
|
-
document.head.appendChild(cloneHeadElement(incoming));
|
|
204
|
-
}
|
|
381
|
+
const current = reconcileIncomingHead(incomingElements, stagedStylesheets);
|
|
382
|
+
for (const [link, stage] of pendingStylesheetLinks) {
|
|
383
|
+
const key = resolveHeadElementKey(link);
|
|
384
|
+
if (stage.latestGeneration < generation && key && !incomingElements.has(key))
|
|
385
|
+
link.remove();
|
|
205
386
|
}
|
|
206
387
|
for (const [key, element] of current) {
|
|
207
388
|
if (incomingElements.has(key) || isPersisted(element, persistAttribute))
|
|
208
389
|
continue;
|
|
209
390
|
element.remove();
|
|
210
391
|
}
|
|
211
|
-
for (const
|
|
392
|
+
for (const stage of stagedStylesheets.values()) {
|
|
212
393
|
stage.committed = true;
|
|
213
|
-
|
|
214
|
-
|
|
394
|
+
pendingStylesheetLinks.delete(stage.link);
|
|
395
|
+
if (stagedStylesheetLoads.get(stage.registryKey) === stage)
|
|
396
|
+
stagedStylesheetLoads.delete(stage.registryKey);
|
|
215
397
|
}
|
|
216
398
|
},
|
|
217
399
|
cancel() {
|
|
218
400
|
if (committed || cancelled)
|
|
219
401
|
return;
|
|
220
402
|
cancelled = true;
|
|
221
|
-
for (const
|
|
403
|
+
for (const stage of stagedStylesheets.values()) {
|
|
222
404
|
stage.users -= 1;
|
|
223
405
|
if (stage.users === 0 && !stage.committed) {
|
|
224
406
|
stage.link.remove();
|
|
225
|
-
|
|
226
|
-
|
|
407
|
+
pendingStylesheetLinks.delete(stage.link);
|
|
408
|
+
if (stagedStylesheetLoads.get(stage.registryKey) === stage)
|
|
409
|
+
stagedStylesheetLoads.delete(stage.registryKey);
|
|
227
410
|
}
|
|
228
411
|
}
|
|
229
412
|
},
|