Skip to content

Commit 6bbf929

Browse files
committed
perf(core): let a scene wait when it only looks inside its own root
1 parent 525d817 commit 6bbf929

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎packages/core/src/compiler/htmlBundler.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,6 +3441,36 @@ ${["b", "h"].map((id, i) => ` <div data-composition-id="${id}" data-compositi
34413441
]);
34423442
});
34433443

3444+
it("lets a scene wait when its script only marks a node in its own root or reads an attribute off <html>", async () => {
3445+
const scene = (id: string, script: string) =>
3446+
`<template id="${id}-template"><div data-composition-id="${id}" id="r-${id}"><p>${id}</p>
3447+
<script>${script}</script></div></template>`;
3448+
const claim = (id: string) =>
3449+
`const root = (function(){var a=document.querySelectorAll('[id="r-${id}"]');for(var i=0;i<a.length;i++){if(!a[i].__hfClaimed){a[i].__hfClaimed=1;return a[i];}}return document.getElementById('r-${id}');})();`;
3450+
const dir = makeTempProject({
3451+
"index.html": `<!doctype html><html><head></head><body>
3452+
<div data-composition-id="main" data-width="1920" data-height="1080" data-duration="8">
3453+
${["g", "h", "i", "j"].map((id, i) => ` <div data-composition-id="${id}" data-composition-src="compositions/${id}.html" data-start="${i * 2}" data-duration="2"></div>`).join("\n")}
3454+
</div></body></html>`,
3455+
"compositions/g.html": scene("g", claim("g")),
3456+
"compositions/h.html": scene(
3457+
"h",
3458+
'JSON.parse(document.documentElement.getAttribute("data-composition-variables") || "[]");',
3459+
),
3460+
"compositions/i.html": scene("i", "document.documentElement.classList.add('dark');"),
3461+
"compositions/j.html": scene("j", "window.__hfClaimed = true;"),
3462+
});
3463+
const doc = parseHTML(await bundleToSingleHtml(dir, { sceneParts: true })).document;
3464+
const eager = (id: string) =>
3465+
doc.querySelector(`div[data-hf-scene="${id}"]`)?.getAttribute("data-hf-scene-eager");
3466+
expect(["g", "h", "i", "j"].map(eager)).toEqual([
3467+
null,
3468+
null,
3469+
"its script uses documentElement",
3470+
"its script uses window.__hfClaimed",
3471+
]);
3472+
});
3473+
34443474
it("marks a scene whose own script leaves work running as not swappable, and only that scene", async () => {
34453475
const dir = makeTempProject({
34463476
"index.html": `<!doctype html><html><head>

‎packages/core/src/compiler/inlineSubCompositions.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ function sceneScriptSwapRefusal(script: string): string | null {
7070
return match ? `its script uses ${match[0].trim()}` : null;
7171
}
7272

73-
// What ties a scene script to boot order: reaching outside its root, or leaving state on window for
74-
// other scripts (the timeline registry aside).
73+
// What ties a scene script to boot order: reaching outside its root (reading <html>'s attributes and
74+
// marking nodes its scoped `document` found stay inside), or leaving state on window for others.
7575
const REACHES_OUTSIDE_RE =
76-
/\b(documentElement|getElementsBy\w+|__hf[A-Z]\w*)\b|document\s*\.\s*(head|body)\b|querySelector(All)?\(\s*["'](head|body)["']|defineProperty\s*\(\s*(window|globalThis|self|document)\b|\b(window|globalThis|self)\s*(\.\s*(?!__timelines\b)[A-Za-z_$][\w$]*|\[[^\]]*\])\s*=(?!=)/;
76+
/\bgetElementsBy\w+\b|\bdocumentElement\b(?!\s*\.\s*(?:get|has)Attribute\s*\()|(?<!\.\s*)\b__hf[A-Z]\w*\b|\b(?:window|globalThis|self)\s*\.\s*__hf[A-Z]\w*|document\s*\.\s*(head|body)\b|querySelector(All)?\(\s*["'](head|body)["']|defineProperty\s*\(\s*(window|globalThis|self|document)\b|\b(window|globalThis|self)\s*(\.\s*(?!__timelines\b)[A-Za-z_$][\w$]*|\[[^\]]*\])\s*=(?!=)/;
7777

7878
/** Why an authored scene script must run at boot rather than when the playhead nears, or null. */
7979
function sceneScriptEagerReason(script: string): string | null {

0 commit comments

Comments
 (0)