perf(studio): leave scene files for the preview runtime to fetch

The preview inlined every data-composition-src scene into one document, so
nothing could play until the whole film had arrived and parsed. The bundler
gains subCompositions: external, used only by the Studio preview: the root
keeps its scene hosts and the runtime fetches each scene file. Renders keep
inlining. Frames at sampled times match the inlined preview exactly.
This commit is contained in:
Miguel Ángel
2026-09-23 00:23:23 -04:00
parent e39caa8133
commit bd87e8524f
4 changed files with 42 additions and 6 deletions
@@ -66,8 +66,11 @@ describe("studio preview document", () => {
// Kept URLs resolve against the preview's <base href> through the asset route.
expect(html).toContain('<base href="/api/projects/film/preview/">');
expect(await fetchAsset("assets/pic.png")).toEqual(PIC);
const nested = html.match(/<img [^>]*id="nested" src="([^"]+)"/)?.[1];
expect(nested).toBe("assets/pic@2x.png");
expect(await fetchAsset(nested ?? "")).toEqual(PIC_2X);
// Scenes stay separate files the runtime fetches, so the first scene can play before the rest.
expect(html).toContain('data-composition-src="compositions/scene.html"');
expect(html).not.toContain('id="nested"');
const scene = (await fetchAsset("compositions/scene.html")).toString("utf-8");
expect(scene.match(/<img [^>]*id="nested" src="([^"]+)"/)?.[1]).toBe("../assets/pic@2x.png");
expect(await fetchAsset("assets/pic@2x.png")).toEqual(PIC_2X);
});
});
@@ -420,6 +420,32 @@ describe("bundleToSingleHtml", () => {
expect(hostEl?.hasAttribute("data-composition-src")).toBe(false);
});
it("leaves sub-composition hosts for the runtime to fetch with subCompositions: external", async () => {
const dir = makeTempProject({
"index.html": `<!doctype html>
<html><head></head><body>
<div id="root" data-composition-id="main" data-width="1920" data-height="1080" data-duration="4">
<div id="scene-host" data-composition-id="scene" data-composition-src="compositions/scene.html"
data-start="2" data-duration="2"></div>
</div>
</body></html>`,
"compositions/scene.html": `<template id="scene-template">
<div data-composition-id="scene" data-width="1920" data-height="1080"><p id="scene-content">hi</p></div>
</template>`,
});
const external = parseHTML(
await bundleToSingleHtml(dir, { subCompositions: "external" }),
).document;
const host = external.getElementById("scene-host");
expect(host?.getAttribute("data-composition-src")).toBe("compositions/scene.html");
expect(host?.getAttribute("data-start")).toBe("2");
expect(external.getElementById("scene-content")).toBeNull();
const inlined = parseHTML(await bundleToSingleHtml(dir)).document;
expect(inlined.getElementById("scene-content")).not.toBeNull();
});
it("inlines local scripts referenced by sub-compositions into the bundle", async () => {
const dir = makeTempProject({
"index.html": `<!doctype html>
+9 -3
View File
@@ -780,6 +780,11 @@ export interface BundleOptions {
* modes and emits `<script ... src="<URL>">` directly.
*/
runtime?: "inline" | "placeholder";
/**
* `"external"` leaves `data-composition-src` hosts for the runtime to fetch, so a preview can play
* the first scenes while later ones load. Default `"inline"`: one self-contained document.
*/
subCompositions?: "inline" | "external";
/**
* Inline .cube LUTs referenced from data-color-grading. Default: true for
* self-contained renders/exports. Studio preview disables this so the editor
@@ -981,9 +986,10 @@ export async function bundleToSingleHtml(
// Inline sub-compositions (via shared function)
const trackedCompositionHosts = getBundledTrackedCompositionHosts(document);
const hostIdentityByElement = assignBundledRuntimeCompositionIds(trackedCompositionHosts);
const subCompositionHosts = trackedCompositionHosts.filter((host) =>
host.hasAttribute("data-composition-src"),
);
const subCompositionHosts =
options?.subCompositions === "external"
? []
: trackedCompositionHosts.filter((host) => host.hasAttribute("data-composition-src"));
const subCompResult = inlineSubCompositions(document, subCompositionHosts, {
resolveHtml: (srcPath: string) => {
if (!isRelativeUrl(srcPath)) return null;
@@ -318,6 +318,7 @@ export const PREVIEW_BUNDLE_OPTIONS = {
runtime: "placeholder",
inlineAssets: false,
staticGuard: false,
subCompositions: "external",
} as const satisfies BundleOptions;
export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): void {