fix(studio): a preview frame served while its page loads keeps Studio's paused-frame decoder (#8613)

* fix(studio): a preview frame served while its page loads keeps Studio's paused-frame decoder

servePausedVideoFrames closed its decoder host on the frame's next `load`.
A shadow reload is served while its document is still `interactive`, so that
`load` is the same document finishing: the host closed, aborted the decode
already in flight without a reply, and the runtime's preview seek waited out
its five-second cap. Every shadow reload in an embedding app then promoted
about five seconds late.

The host now closes only once the frame holds a different document (a frame
Studio cannot read still closes on its next load), and a close answers every
ask in flight with `failed` so the runtime falls back to its own frames at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(studio): keep only the paused-frame host's own-load fix

Closing the host on the frame's next document is the fix. Answering in-flight
asks on close only helps a frame Studio cannot read, which no reported bug
reached; it goes, with its test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(studio): closeOnNextDocument's name carries what its comment said

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andy Cheigh
2026-11-23 20:43:24 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 32afb984bd
commit 0a685496ad
2 changed files with 78 additions and 1 deletions
@@ -0,0 +1,59 @@
// @vitest-environment happy-dom
import { afterEach, expect, it, vi } from "vitest";
import { servePausedVideoFrames } from "./pausedVideoProvider";
const handedOver = vi.hoisted(() => ({ ports: [] as MessagePort[] }));
vi.mock("./previewChannel", () => ({
previewEditingClient: () => ({
request: vi.fn(async (_: string, params: { channel: MessagePort }) => {
handedOver.ports.push(params.channel);
return { served: true };
}),
}),
}));
afterEach(() => {
for (const port of handedOver.ports.splice(0)) port.close();
});
/** A same-origin preview frame whose document the test swaps, as a navigation would. */
function previewFrame() {
const iframe = document.createElement("iframe");
let doc: Document = document.implementation.createHTMLDocument("served");
Object.defineProperty(iframe, "contentDocument", { get: () => doc });
Object.defineProperty(iframe, "src", {
value: `${location.origin}/api/projects/p1/preview/index.html`,
});
return {
iframe,
loadSameDocument: () => iframe.dispatchEvent(new Event("load")),
loadNextDocument: () => {
doc = document.implementation.createHTMLDocument("next");
iframe.dispatchEvent(new Event("load"));
},
};
}
/** Whether Studio still answers the frame's runtime on its port. */
async function answers(port: MessagePort): Promise<boolean> {
const reply = new Promise<boolean>((resolve) => {
port.onmessage = () => resolve(true);
setTimeout(() => resolve(false), 50);
});
port.postMessage({ type: "suspend", id: 1 });
return reply;
}
// fails without the fix: a shadow reload served while its page loaded lost Studio's decoder at that page's own load.
it("keeps serving a frame through the load of the document it was served for", async () => {
const frame = previewFrame();
servePausedVideoFrames(frame.iframe);
await vi.waitFor(() => expect(handedOver.ports).toHaveLength(1));
const port = handedOver.ports[0]!;
frame.loadSameDocument();
expect(await answers(port)).toBe(true);
frame.loadNextDocument();
expect(await answers(port)).toBe(false);
});
@@ -22,12 +22,30 @@ export function servePausedVideoFrames(iframe: HTMLIFrameElement): void {
served.set(iframe, client);
const { port1, port2 } = new MessageChannel();
const host = pausedFrameHost(port1, project);
iframe.addEventListener("load", host.close, { once: true });
closeOnNextDocument(iframe, host.close);
client
.request("serve-paused-frames", { channel: port2 }, { transfer: [port2] })
.then(({ served: on }) => on || host.close(), host.close);
}
function readableDocument(iframe: HTMLIFrameElement): Document | null {
try {
return iframe.contentDocument;
} catch {
return null;
}
}
function closeOnNextDocument(iframe: HTMLIFrameElement, close: () => void): void {
const served = readableDocument(iframe);
const onLoad = () => {
if (served && readableDocument(iframe) === served) return;
iframe.removeEventListener("load", onLoad);
close();
};
iframe.addEventListener("load", onLoad);
}
async function projectFiles(project: string): Promise<Set<string>> {
const res = await studioApiFetch(project);
if (!res.ok) throw new Error(`Project files unavailable: ${res.status}`);