Skip to content

Commit 05ba8f5

Browse files
committed
fix(core): keep sub-composition mounts concurrent and order their head styles by host
Mounting one sub-composition at a time made a later host wait for an earlier host's fetch, which hangs when that fetch depends on the later mount. Mounts run concurrently again; each injected link and style is inserted before the head nodes of any host later in the document, so the cascade order no longer depends on which fetch finishes first. The parity test gains a scene that mounts the same card twice, which pins the suffix-every-copy rule.
1 parent a7c73e9 commit 05ba8f5

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

‎packages/core/src/runtime/compositionLoader.ts‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ function uniqueCompositionId(baseId: string, index: number): string {
188188
return `${baseId}__hf${index}`;
189189
}
190190

191+
const headNodeHost = new WeakMap<Element, Element>();
192+
193+
/** Mounts finish in any order; `<head>` keeps their links and styles in host document order. */
194+
function insertIntoHeadInHostOrder(node: Element, host: Element): void {
195+
const next = Array.from(document.head.children).find((element) => {
196+
const owner = headNodeHost.get(element);
197+
return (
198+
owner?.isConnected === true &&
199+
(host.compareDocumentPosition(owner) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0
200+
);
201+
});
202+
headNodeHost.set(node, host);
203+
document.head.insertBefore(node, next ?? null);
204+
}
205+
191206
const waitForExternalScriptLoad = (
192207
scriptEl: HTMLScriptElement,
193208
): Promise<{ status: "load" | "error" | "timeout"; elapsedMs: number }> =>
@@ -495,7 +510,7 @@ async function mountCompositionContent(params: {
495510
if (!isLinkElement(clonedLink)) continue;
496511
clonedLink.href = href;
497512
if (hasSameLink(document.head, clonedLink)) continue;
498-
document.head.appendChild(clonedLink);
513+
insertIntoHeadInHostOrder(clonedLink, params.host);
499514
params.injectedLinks.push(clonedLink);
500515
}
501516

@@ -517,7 +532,7 @@ async function mountCompositionContent(params: {
517532
{ scopeRootSelectors: true },
518533
);
519534
}
520-
document.head.appendChild(clonedStyle);
535+
insertIntoHeadInHostOrder(clonedStyle, params.host);
521536
params.injectedStyles.push(clonedStyle);
522537
const authored = clonedStyle.textContent || "";
523538
styles.push({ element: clonedStyle, authored, applied: authored });
@@ -725,11 +740,10 @@ async function mountExternalCompositions(
725740
return !parent || attemptedPaths.has(parent);
726741
});
727742

728-
type PendingMount = () => Promise<MountedComposition | null>;
729743
const mounted: Array<MountedComposition | null> = [];
730744
for (let offset = 0; offset < hosts.length; offset += 4) {
731745
const batch = await Promise.all(
732-
hosts.slice(offset, offset + 4).map(async (host): Promise<PendingMount | null> => {
746+
hosts.slice(offset, offset + 4).map(async (host): Promise<MountedComposition | null> => {
733747
const src = host.getAttribute("data-composition-src");
734748
if (!src) return null;
735749
const hostIdentity = hostIdentityByElement.get(host);
@@ -765,25 +779,18 @@ async function mountExternalCompositions(
765779
failed(new Error(refusal));
766780
return null;
767781
}
768-
const mount =
769-
(mountParams: Parameters<typeof mountCompositionContent>[0]): PendingMount =>
770-
async () => {
782+
const mount = async (mountParams: Parameters<typeof mountCompositionContent>[0]) => {
783+
const composition = await mountCompositionContent(mountParams);
784+
const runScripts = composition.runScripts;
785+
composition.runScripts = async () => {
771786
try {
772-
const composition = await mountCompositionContent(mountParams);
773-
const runScripts = composition.runScripts;
774-
composition.runScripts = async () => {
775-
try {
776-
await runScripts();
777-
} catch (error) {
778-
failed(error);
779-
}
780-
};
781-
return composition;
787+
await runScripts();
782788
} catch (error) {
783789
failed(error);
784-
return null;
785790
}
786791
};
792+
return composition;
793+
};
787794
try {
788795
const localTemplate =
789796
authoredCompositionId != null
@@ -792,7 +799,7 @@ async function mountExternalCompositions(
792799
)
793800
: null;
794801
if (localTemplate) {
795-
return mount({
802+
return await mount({
796803
host,
797804
authoredCompositionId,
798805
runtimeCompositionId,
@@ -824,7 +831,7 @@ async function mountExternalCompositions(
824831
)
825832
: null) ?? doc.querySelector<HTMLTemplateElement>("template");
826833
const sourceNode = template ? template.content : doc.body;
827-
return mount({
834+
return await mount({
828835
host,
829836
authoredCompositionId,
830837
runtimeCompositionId,
@@ -856,8 +863,7 @@ async function mountExternalCompositions(
856863
}
857864
}),
858865
);
859-
// Mount in document order: mounting appends <head> links and styles in cascade order.
860-
for (const pending of batch) mounted.push(pending ? await pending() : null);
866+
mounted.push(...batch);
861867
}
862868
return mounted.filter((composition): composition is MountedComposition => composition !== null);
863869
}

‎packages/producer/src/services/htmlCompiler.parity.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,22 @@ describe("mount/compile assembly parity", () => {
475475
'<div data-composition-id="card" data-composition-src="compositions/cards/card.html"></div><div data-composition-id="card__hf1" data-composition-src="compositions/cards/card.html"></div>',
476476
ids: ["card", "card__hf1", "card__hf2"],
477477
},
478+
{ name: "two copies inside one scene", sceneCopies: 2, ids: ["card__hf1", "card__hf2"] },
478479
])(
479480
"mounts nested project-root paths with $name identically on all three paths",
480-
async ({ extraHosts, ids }) => {
481+
async ({ extraHosts = "", sceneCopies = 1, ids }) => {
482+
const cardHost =
483+
'<div data-composition-id="card" data-composition-src="compositions/cards/card.html"></div>';
481484
const result = await contracts({
482485
...nestedCompositionPathFixture,
483486
"index.html": nestedCompositionPathFixture["index.html"]!.replace(
484487
'<div data-composition-id="scene"',
485488
`${extraHosts}<div data-composition-id="scene"`,
486489
),
490+
"compositions/scene.html": nestedCompositionPathFixture["compositions/scene.html"]!.replace(
491+
cardHost,
492+
cardHost.repeat(sceneCopies),
493+
),
487494
});
488495
expect(result.render).toEqual(result.preview);
489496
expect(assembledContract(result.mount)).toEqual(assembledContract(result.preview));

0 commit comments

Comments
 (0)