Skip to content

Commit 11e684d

Browse files
fix: the preview preloads the coming scene's images, and check stops failing on image swaps (#5128)
* fix(core): the preview loads the next scene's images before it starts * fix(cli): check stops reporting image loads that its own scrubbing cancelled * fix: preload only a coming clip's own images, and judge aborted images by whether they still decode * fix(cli): count an aborted image as broken only when its decode fails * style(cli): keep the aborted-image comment within the line limit * test(cli): pin that a reload still pending at the cap is not reported * test(cli): run the cap test on whatever timers are pending, and always restore real timers
1 parent e6daed3 commit 11e684d

4 files changed

Lines changed: 178 additions & 2 deletions

File tree

‎packages/cli/src/utils/checkBrowser.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { DEFAULT_CHECK_OPTIONS, runAuditGrid } from "./checkPipeline.js";
1212
import {
1313
captureOverviewShot,
14+
keepBrokenImageAborts,
1415
preResolveHostileMediaProxies,
1516
runBrowserCheck,
1617
} from "./checkBrowser.js";
@@ -83,6 +84,7 @@ const PROJECT: ProjectDir = {
8384
};
8485

8586
afterEach(() => {
87+
vi.useRealTimers();
8688
vi.clearAllMocks();
8789
vi.restoreAllMocks();
8890
vi.unstubAllGlobals();
@@ -526,6 +528,95 @@ it("elevates and deduplicates WebGPU validation warnings while preserving ordina
526528
);
527529
});
528530

531+
describe("keepBrokenImageAborts", () => {
532+
const draft = (url: string, abortedImage: boolean) => ({
533+
code: "request_failed",
534+
severity: "error" as const,
535+
message: `Failed to load ${url}`,
536+
time: 0,
537+
url,
538+
abortedImage,
539+
});
540+
541+
it("keeps an aborted image only when it is still broken, and every other failure", () => {
542+
const swappedPast = draft("http://h/seq/0004.png", true);
543+
const stuck = draft("http://h/plate.png", true);
544+
const missing = draft("http://h/gone.png", false);
545+
const kept = keepBrokenImageAborts(
546+
[swappedPast, stuck, missing],
547+
new Set(["http://h/plate.png"]),
548+
);
549+
expect(kept).toEqual([stuck, missing]);
550+
});
551+
552+
it("reports an aborted image a page still shows broken, not one swapped away or one that decodes", async () => {
553+
const base = "http://127.0.0.1:3000/assets";
554+
mountCanvasFixture(
555+
`<img id="stuck" src="${base}/plate.png"><img id="fine" src="${base}/seq/0012.png">`,
556+
);
557+
const stuck = document.getElementById("stuck") as HTMLImageElement;
558+
const fine = document.getElementById("fine") as HTMLImageElement;
559+
stuck.decode = () => Promise.reject(new Error("broken"));
560+
fine.decode = () => Promise.resolve();
561+
const aborted = (url: string) => ({
562+
url: () => url,
563+
failure: () => ({ errorText: "net::ERR_ABORTED" }),
564+
resourceType: () => "image",
565+
});
566+
const page = fakePage();
567+
page.on = vi.fn((event: string, handler: (request: ReturnType<typeof aborted>) => void) => {
568+
if (event !== "requestfailed") return;
569+
for (const url of ["seq/0004.png", "seq/0012.png", "plate.png"])
570+
handler(aborted(`${base}/${url}`));
571+
});
572+
installSessionMock(page);
573+
574+
const result = await runBrowserCheck(
575+
PROJECT,
576+
{ ...DEFAULT_CHECK_OPTIONS, samples: 1, contrast: false },
577+
{ kind: "none" },
578+
runAuditGrid,
579+
);
580+
581+
const failed = result.runtimeFindings.filter((finding) => finding.code === "request_failed");
582+
expect(failed.map((finding) => finding.message)).toEqual([
583+
"Failed to load assets/plate.png: net::ERR_ABORTED",
584+
]);
585+
});
586+
587+
it("does not report an aborted image whose reload is still pending at the cap", async () => {
588+
const url = "http://127.0.0.1:3000/assets/slow.png";
589+
mountCanvasFixture(`<img id="slow" src="${url}">`);
590+
(document.getElementById("slow") as HTMLImageElement).decode = () => new Promise(() => {});
591+
const page = fakePage();
592+
page.on = vi.fn((event: string, handler: (request: unknown) => void) => {
593+
if (event !== "requestfailed") return;
594+
handler({
595+
url: () => url,
596+
failure: () => ({ errorText: "net::ERR_ABORTED" }),
597+
resourceType: () => "image",
598+
});
599+
});
600+
installSessionMock(page);
601+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
602+
try {
603+
const checked = runBrowserCheck(
604+
PROJECT,
605+
{ ...DEFAULT_CHECK_OPTIONS, samples: 1, contrast: false },
606+
{ kind: "none" },
607+
runAuditGrid,
608+
);
609+
await vi.runAllTimersAsync();
610+
const result = await checked;
611+
expect(result.runtimeFindings.filter((finding) => finding.code === "request_failed")).toEqual(
612+
[],
613+
);
614+
} finally {
615+
vi.useRealTimers();
616+
}
617+
});
618+
});
619+
529620
describe("preResolveHostileMediaProxies", () => {
530621
const dirs: string[] = [];
531622
const mkProjectDir = (): string => {

‎packages/cli/src/utils/checkBrowser.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ interface RuntimeDraft {
6363
url?: string;
6464
line?: number;
6565
count?: number;
66+
abortedImage?: boolean;
6667
}
6768

6869
interface AnchorRequest {
@@ -195,10 +196,13 @@ export async function runBrowserCheck(
195196
currentTime = time;
196197
});
197198
const result = await runGrid(driver, options, motion);
199+
const broken = await abortedImagesStillBroken(page, drafts);
198200
return {
199201
...result,
200202
timings: { ...result.timings, launchSettleMs },
201-
runtimeFindings: drafts.map((draft) => runtimeFinding(draft, rootAnchor)),
203+
runtimeFindings: keepBrokenImageAborts(drafts, broken).map((draft) =>
204+
runtimeFinding(draft, rootAnchor),
205+
),
202206
};
203207
} finally {
204208
await chromeBrowser?.close().catch(() => undefined);
@@ -367,6 +371,46 @@ function wireRuntimeListeners(page: Page, drafts: RuntimeDraft[], currentTime: (
367371
wireNetworkListeners(page, drafts, currentTime);
368372
}
369373

374+
/** Check's scrubs cancel image loads: an aborted image failed only if an `<img>` shows it and its decode fails. */
375+
export function keepBrokenImageAborts(drafts: RuntimeDraft[], broken: Set<string>): RuntimeDraft[] {
376+
return drafts.filter((draft) => !draft.abortedImage || broken.has(draft.url ?? ""));
377+
}
378+
379+
const IMAGE_DECODE_CAP_MS = 5000;
380+
381+
async function abortedImagesStillBroken(page: Page, drafts: RuntimeDraft[]): Promise<Set<string>> {
382+
const urls = drafts.filter((draft) => draft.abortedImage).map((draft) => draft.url ?? "");
383+
if (urls.length === 0) return new Set();
384+
const broken = await page.evaluate(
385+
async (candidates: string[], capMs: number) => {
386+
// A load still pending at the cap (a deferred lazy image) is not a failure.
387+
const fails = (img: HTMLImageElement) =>
388+
new Promise<boolean>((resolve) => {
389+
const cap = setTimeout(() => resolve(false), capMs);
390+
img
391+
.decode()
392+
.then(
393+
() => resolve(false),
394+
() => resolve(true),
395+
)
396+
.finally(() => clearTimeout(cap));
397+
});
398+
const stillBroken = await Promise.all(
399+
candidates.map(async (url) => {
400+
const shown = Array.from(document.querySelectorAll("img")).filter(
401+
(img) => img.currentSrc === url || img.src === url,
402+
);
403+
return (await Promise.all(shown.map(fails))).some(Boolean);
404+
}),
405+
);
406+
return candidates.filter((_, i) => stillBroken[i]);
407+
},
408+
urls,
409+
IMAGE_DECODE_CAP_MS,
410+
);
411+
return new Set(broken);
412+
}
413+
370414
function wireNetworkListeners(page: Page, drafts: RuntimeDraft[], currentTime: () => number): void {
371415
page.on("requestfailed", (request) => {
372416
const url = request.url();
@@ -379,6 +423,7 @@ function wireNetworkListeners(page: Page, drafts: RuntimeDraft[], currentTime: (
379423
message: `Failed to load ${urlPath(url)}: ${failure ?? "net::ERR_FAILED"}`,
380424
time: currentTime(),
381425
url,
426+
abortedImage: failure === "net::ERR_ABORTED" && request.resourceType() === "image",
382427
});
383428
});
384429
page.on("response", (response) => {

‎packages/core/src/runtime/entry.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,34 @@ describe("runtime entry", () => {
197197
expect(imageSkipped(later)).toEqual([false]);
198198
});
199199

200+
it("loads the next scene's images within the look-ahead while its clip is still hidden", async () => {
201+
servePreview();
202+
const root = mountRoot();
203+
timed(root, "div", "0");
204+
const soon = timed(root, "div", "1.5");
205+
const later = timed(root, "div", "5");
206+
const lazyPlate = (clip: HTMLElement) => {
207+
const img = clip.appendChild(document.createElement("img"));
208+
img.setAttribute("loading", "lazy");
209+
img.setAttribute(STUDIO_PREVIEW_LAZY_ATTR, "");
210+
return img;
211+
};
212+
const plate = lazyPlate(soon);
213+
const farPlate = lazyPlate(later);
214+
// A clip nested in the coming one waits for its own look-ahead.
215+
const nestedPlate = lazyPlate(timed(soon, "div", "8"));
216+
const authored = soon.appendChild(document.createElement("img"));
217+
authored.setAttribute("loading", "lazy");
218+
219+
await evaluateRuntime();
220+
expect(visibility(soon)).toEqual(["hidden"]);
221+
expect(
222+
[plate, farPlate, nestedPlate, authored].map((img) => img.getAttribute("loading")),
223+
).toEqual(["eager", "lazy", "lazy", "lazy"]);
224+
window.__player?.seek(3.5);
225+
expect(farPlate.getAttribute("loading")).toBe("eager");
226+
});
227+
200228
it("holds a paused jump on the previous picture until the next scene's image decodes", async () => {
201229
servePreview();
202230
const root = mountRoot();

‎packages/core/src/runtime/init.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ const WEB_AUDIO_MEDIA = "audio[data-start], video[data-start]";
384384
const webAudioMediaIn = (root: ParentNode): HTMLMediaElement[] =>
385385
Array.from(root.querySelectorAll(WEB_AUDIO_MEDIA)).filter(joinsWebAudio);
386386

387+
/** Studio's lazy images in `root` load now: a hidden clip has no layout box, so lazy ones would never fetch. */
388+
function loadPreviewImagesIn(root: Element): void {
389+
const images = root.matches(`[${STUDIO_PREVIEW_LAZY_ATTR}]`)
390+
? [root]
391+
: Array.from(root.querySelectorAll(`img[${STUDIO_PREVIEW_LAZY_ATTR}]`)).filter(
392+
(img) => img.closest("[data-start]") === root,
393+
);
394+
for (const img of images) img.setAttribute("loading", "eager");
395+
}
396+
387397
export function initSandboxRuntimeModular(): void {
388398
const state = createRuntimeState();
389399
authoredMediaObserver?.disconnect();
@@ -2798,6 +2808,8 @@ export function initSandboxRuntimeModular(): void {
27982808
rawNode.style.visibility = isVisibleNow ? "visible" : "hidden";
27992809
const upcoming =
28002810
hiddenImagesSkipped && !isVisibleNow && dueSoon(rawNode, visibleAt, currentTime);
2811+
if (upcoming && !rawNode.hasAttribute(STUDIO_PREVIEW_UPCOMING_ATTR))
2812+
loadPreviewImagesIn(rawNode);
28012813
rawNode.toggleAttribute(STUDIO_PREVIEW_UPCOMING_ATTR, upcoming);
28022814
if (isMediaElement(rawNode) && metadataBoundMedia.has(rawNode) && inPreloadWindow(rawNode))
28032815
preloadNearPlayhead(
@@ -3888,7 +3900,7 @@ export function initSandboxRuntimeModular(): void {
38883900
const held = { time: quantized, apply: () => applySeek(quantized, options) };
38893901
heldSeek = held;
38903902
for (const img of undecoded) {
3891-
if (img.hasAttribute(STUDIO_PREVIEW_LAZY_ATTR)) img.setAttribute("loading", "eager");
3903+
loadPreviewImagesIn(img);
38923904
for (let clip = img.closest(SKIPPED_CLIP); clip; clip = img.closest(SKIPPED_CLIP))
38933905
clip.setAttribute(STUDIO_PREVIEW_UPCOMING_ATTR, "");
38943906
}

0 commit comments

Comments
 (0)