Skip to content

Commit 7266646

Browse files
fix(studio): a failed preview reload says which step it was still waiting for (#5700)
* fix(studio): a failed preview reload says which step it was still waiting for * 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. * refactor(studio): name the reachable-player check instead of commenting it
1 parent 19a8d44 commit 7266646

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

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

Lines changed: 15 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 shadowPlayerReachable(iframe: HTMLIFrameElement | null): boolean {
39+
try {
40+
return Boolean((iframe?.contentWindow as IframeWindow | null | undefined)?.__player);
41+
} catch {
42+
return false;
43+
}
44+
}
45+
3846
function isDocumentHidden(): boolean {
3947
return typeof document !== "undefined" && document.visibilityState === "hidden";
4048
}
@@ -104,15 +112,20 @@ export function useShadowPreviewReload({
104112
const failShadow = useCallback(
105113
(gen: number, cause: string) => {
106114
if (gen !== shadowGenRef.current) return;
115+
const waitingFor = !shadowPlayerReachable(shadowIframeRef.current)
116+
? "the page and its runtime to load"
117+
: pendingCommitRef.current?.gen !== gen
118+
? "its timeline to be ready"
119+
: "its loaders to clear";
107120
shadowGenRef.current += 1;
108121
stopPendingShadow();
109122
shadowIframeRef.current = null;
110123
isRefreshingRef.current = false;
111124
pendingSeekRef.current = null;
112125
setPreviewSlots(planShadowDiscard);
113126
setPreviewReloading(false);
114-
const message = `The preview did not reload (${cause}). The previous preview is still showing.`;
115-
logReload("shadow-failed", { cause });
127+
const message = `The preview did not reload (${cause}). It was still waiting for ${waitingFor}. The previous preview is still showing.`;
128+
logReload("shadow-failed", { cause, waitingFor });
116129
console.error(`[studio] ${message}`);
117130
onReloadFailedRef.current?.(message);
118131
},

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ describe("shadow reload readiness and failure", () => {
371371
expect(getApi().iframeRef.current).toBe(live);
372372
expect(live.style.visibility).toBe("");
373373
expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));
374+
expect(onPreviewReloadFailed).toHaveBeenCalledWith(
375+
expect.stringContaining("still waiting for the page and its runtime to load"),
376+
);
374377
expect(consoleError).toHaveBeenCalled();
375378
unmount(root);
376379
});
@@ -395,6 +398,48 @@ describe("shadow reload readiness and failure", () => {
395398
act(() => void vi.advanceTimersByTime(1));
396399
expect(getApi().previewSlots).toEqual([{ gen: 0, role: "live" }]);
397400
expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));
401+
expect(onPreviewReloadFailed).toHaveBeenCalledWith(
402+
expect.stringContaining("still waiting for the page and its runtime to load"),
403+
);
404+
unmount(root);
405+
});
406+
407+
it("names the timeline when a shadow's runtime runs but never reports a length", () => {
408+
vi.useFakeTimers();
409+
vi.spyOn(console, "error").mockImplementation(() => {});
410+
const onPreviewReloadFailed = vi.fn();
411+
const { getApi, gen, root } = beginReload({ onPreviewReloadFailed });
412+
const lengthless = makeFakeIframe(makeAdapterWindow({ duration: 0 }).win);
413+
act(() => {
414+
getApi().setShadowIframeNode(lengthless);
415+
getApi().onShadowIframeLoad(gen);
416+
getApi().onShadowReadyChange(gen, true);
417+
});
418+
419+
act(() => void vi.advanceTimersByTime(FULL_WAIT_MS));
420+
421+
expect(onPreviewReloadFailed).toHaveBeenCalledWith(
422+
expect.stringContaining("still waiting for its timeline to be ready"),
423+
);
424+
unmount(root);
425+
});
426+
427+
it("names the loaders when a shadow with a ready timeline never clears them", () => {
428+
vi.useFakeTimers();
429+
vi.spyOn(console, "error").mockImplementation(() => {});
430+
const onPreviewReloadFailed = vi.fn();
431+
const { getApi, gen, root } = beginReload({ onPreviewReloadFailed });
432+
const shadow = makeShadowWithSpies();
433+
act(() => {
434+
getApi().setShadowIframeNode(shadow.iframe);
435+
getApi().onShadowIframeLoad(gen);
436+
});
437+
438+
act(() => void vi.advanceTimersByTime(FULL_WAIT_MS));
439+
440+
expect(onPreviewReloadFailed).toHaveBeenCalledWith(
441+
expect.stringContaining("still waiting for its loaders to clear"),
442+
);
398443
unmount(root);
399444
});
400445

@@ -475,6 +520,31 @@ describe("shadow reload readiness and failure", () => {
475520
unmount(root);
476521
});
477522

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+
478548
it("drops a shadow whose document reports an error and reports the cause", () => {
479549
vi.spyOn(console, "error").mockImplementation(() => {});
480550
const onPreviewReloadFailed = vi.fn();

0 commit comments

Comments
 (0)