Skip to content

Commit 9f17f9e

Browse files
fix(server): desktop snapshots wait for the resized frame instead of returning the old one
A desktop window reports a resize (devicePixelRatio, innerWidth/innerHeight) before its compositor draws a frame at the new size, so an unclipped Page.captureScreenshot right after a resize could still return the old 1280x800 frame. Snapshot capture on display-scaled pages now retries until the PNG matches the measured viewport, bounded at 2s, then keeps the last frame. Re-enables the viewport test skipped in #22150. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2777514 commit 9f17f9e

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎apps/server/src/preview/ServerBrowserPage.test.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ describe("server browser drag", () => {
370370
});
371371
});
372372

373-
// TODO(#19913 follow-up): flaky in CI since 2e8b5b9d; the capture can return the pre-resize frame.
374-
// Skipped to unblock main; a proper fix makes the capture wait for the resized frame.
375-
it.skip("captures the native display viewport after page script reassigns the globals", async () => {
373+
it("captures the native display viewport after page script reassigns the globals", async () => {
376374
// Context deviceScaleFactor changes DPR but does not scale raw CDP captures.
377375
const browser = await chromium.launch({
378376
headless: true,

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const MAX_EVALUATION_BYTES = 64_000;
2626
const MAX_VISIBLE_TEXT_LENGTH = 20_000;
2727
const MAX_SCREENSHOT_WIDTH = 1280;
2828
const WAIT_POLL_MS = 100;
29+
const RESIZED_FRAME_MS = 2_000;
2930
export const DIAGNOSTIC_BUFFER_LIMIT = 200;
3031

3132
export class ServerBrowserOperationError extends Error {
@@ -484,6 +485,36 @@ const renderingOf = async (input: SnapshotInput) => {
484485
};
485486
};
486487

488+
// A desktop window reports a resize before its compositor draws a frame at the new size, so an
489+
// unclipped capture can still return the old frame. Bounded: a frame that never matches is kept.
490+
// Headless pages are sized by the server, which awaits each resize.
491+
const captureResized = async (
492+
input: SnapshotInput,
493+
options: {
494+
readonly scale: number;
495+
readonly viewport: Viewport;
496+
readonly offset: { readonly x: number; readonly y: number } | undefined;
497+
readonly renderScale: number;
498+
},
499+
deadline: number,
500+
) => {
501+
if (input.renderScale !== "display")
502+
return captureViewport(input.page, input.cdp, { format: "png", ...options });
503+
const expected = {
504+
width: options.viewport.width * options.renderScale * options.scale,
505+
height: options.viewport.height * options.renderScale * options.scale,
506+
};
507+
const giveUpAt = Math.min(Date.now() + RESIZED_FRAME_MS, deadline - 2_000);
508+
for (;;) {
509+
const data = await captureViewport(input.page, input.cdp, { format: "png", ...options });
510+
const size = pngSize(data);
511+
const matches =
512+
Math.abs(size.width - expected.width) <= 1 && Math.abs(size.height - expected.height) <= 1;
513+
if (matches || Date.now() + WAIT_POLL_MS >= giveUpAt) return data;
514+
await new Promise((resolve) => setTimeout(resolve, WAIT_POLL_MS));
515+
}
516+
};
517+
487518
const capture = async (
488519
input: SnapshotInput,
489520
deadline: number,
@@ -506,7 +537,7 @@ const capture = async (
506537
"url" | "title" | "loading" | "visibleText" | "interactiveElements"
507538
>
508539
>,
509-
captureViewport(input.page, input.cdp, { format: "png", scale, viewport, offset }),
540+
captureResized(input, { scale, viewport, offset, renderScale }, deadline),
510541
]);
511542
void pageAndScreenshot.catch(constVoid);
512543
// Leaves time for the second read; a frame that never loads holds the tree until then.

0 commit comments

Comments
 (0)