Skip to content

Commit 4845b65

Browse files
committed
fix(studio): a failed reload of a cross-origin preview still cleans up and reports
Naming the step the reload waited for read the hidden frame's player, which throws for a cross-origin frame and skipped the cleanup and the failure report.
1 parent 0cd1591 commit 4845b65

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎packages/studio/src/player/hooks/useShadowPreviewReload.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function restoreSeekPainted(iframe: HTMLIFrameElement | null): Promise<void> | u
3535
return win?.__hfWaitForSeekCompletion?.().catch(() => {});
3636
}
3737

38+
function shadowHasPlayer(iframe: HTMLIFrameElement | null): boolean {
39+
try {
40+
return Boolean((iframe?.contentWindow as IframeWindow | null | undefined)?.__player);
41+
} catch {
42+
return false; // a cross-origin shadow throws SecurityError, and its runtime is out of reach anyway
43+
}
44+
}
45+
3846
function isDocumentHidden(): boolean {
3947
return typeof document !== "undefined" && document.visibilityState === "hidden";
4048
}
@@ -104,8 +112,7 @@ export function useShadowPreviewReload({
104112
const failShadow = useCallback(
105113
(gen: number, cause: string) => {
106114
if (gen !== shadowGenRef.current) return;
107-
const shadowWin = shadowIframeRef.current?.contentWindow as IframeWindow | null | undefined;
108-
const waitingFor = !shadowWin?.__player
115+
const waitingFor = !shadowHasPlayer(shadowIframeRef.current)
109116
? "the page and its runtime to load"
110117
: pendingCommitRef.current?.gen !== gen
111118
? "its timeline to be ready"

‎packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,31 @@ describe("shadow reload readiness and failure", () => {
520520
unmount(root);
521521
});
522522

523+
it("still drops and reports a shadow whose window throws on access", () => {
524+
vi.spyOn(console, "error").mockImplementation(() => {});
525+
const onPreviewReloadFailed = vi.fn();
526+
const { getApi, live, gen, root } = beginReload({ onPreviewReloadFailed });
527+
const crossOrigin = makeFakeIframe(
528+
Object.defineProperty({}, "__player", {
529+
get: () => {
530+
throw new DOMException("Blocked a frame", "SecurityError");
531+
},
532+
}),
533+
);
534+
act(() => getApi().setShadowIframeNode(crossOrigin));
535+
536+
act(() => getApi().onShadowError(gen, "Composition timeline not found after 8s"));
537+
538+
expect(getApi().previewSlots).toEqual([{ gen: 0, role: "live" }]);
539+
expect(getApi().iframeRef.current).toBe(live);
540+
expect(isPreviewChanging()).toBe(false);
541+
expect(onPreviewReloadFailed).toHaveBeenCalledTimes(1);
542+
expect(onPreviewReloadFailed).toHaveBeenCalledWith(
543+
expect.stringContaining("still waiting for the page and its runtime to load"),
544+
);
545+
unmount(root);
546+
});
547+
523548
it("drops a shadow whose document reports an error and reports the cause", () => {
524549
vi.spyOn(console, "error").mockImplementation(() => {});
525550
const onPreviewReloadFailed = vi.fn();

0 commit comments

Comments
 (0)