Skip to content

Commit 94967ab

Browse files
committed
fix(server): desktop-drawn preview snapshots match the page's scale and viewport
1 parent 42da166 commit 94967ab

4 files changed

Lines changed: 222 additions & 10 deletions

File tree

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,3 +1648,105 @@ it.live("a desktop page that comes back reconnects without waiting for a viewer"
16481648
}),
16491649
).pipe(Effect.provide(layer)),
16501650
);
1651+
1652+
it.live.each([
1653+
"display ratio 1.5",
1654+
"phone viewport",
1655+
"zoomed and scrolled",
1656+
"zoom after ratio read",
1657+
"zoom after first layout read",
1658+
"zoom never settles",
1659+
"headless",
1660+
] as const)("snapshot sizing: %s", (scenario) =>
1661+
Effect.scoped(
1662+
Effect.gen(function* () {
1663+
desktopRendersNext = scenario !== "headless";
1664+
const { browser, broker, tabId } = yield* ready;
1665+
yield* browser.attachViewer(viewerInput(tabId, false));
1666+
const context = scenario === "headless" ? contexts[0]! : desktopConnections[0]!.context;
1667+
const phone = scenario === "phone viewport";
1668+
const beforeZoom = {
1669+
ratio: phone ? 1 : 1.5,
1670+
width: phone ? 390 : 1280,
1671+
height: phone ? 844 : 800,
1672+
zoom: 1,
1673+
};
1674+
const afterZoom = { ratio: 3, width: 640, height: 400, zoom: 2 };
1675+
let current = scenario === "zoomed and scrolled" ? afterZoom : beforeZoom;
1676+
let ratioReads = 0;
1677+
let layoutReads = 0;
1678+
const captures: unknown[] = [];
1679+
const size = phone
1680+
? { width: 390, height: 844 }
1681+
: scenario === "zoomed and scrolled"
1682+
? { width: 1279, height: 799 }
1683+
: { width: 1280, height: 800 };
1684+
// An encoded IHDR makes the assertion independent of the predicted clip size.
1685+
const header = Buffer.alloc(24);
1686+
header.write("IHDR", 12);
1687+
header.writeUInt32BE(size.width, 16);
1688+
header.writeUInt32BE(size.height, 20);
1689+
for (const session of context.sessions) {
1690+
const send = session.send.getMockImplementation()!;
1691+
session.send.mockImplementation(async (method, input) => {
1692+
if (
1693+
method === "Runtime.evaluate" &&
1694+
String((input as { expression?: string })?.expression).includes("ratio:")
1695+
) {
1696+
ratioReads += 1;
1697+
const reply = { result: { value: current } };
1698+
if (scenario === "zoom after ratio read") current = afterZoom;
1699+
if (scenario === "zoom never settles")
1700+
current = current === beforeZoom ? afterZoom : beforeZoom;
1701+
return reply;
1702+
}
1703+
if (method === "Page.getLayoutMetrics") {
1704+
const reply = {
1705+
cssVisualViewport: {
1706+
pageX: 0,
1707+
pageY: scenario === "zoomed and scrolled" ? 100 : 0,
1708+
zoom: current.zoom,
1709+
},
1710+
};
1711+
layoutReads += 1;
1712+
if (scenario === "zoom after first layout read" && layoutReads === 1)
1713+
current = afterZoom;
1714+
return reply;
1715+
}
1716+
if (method === "Page.captureScreenshot") {
1717+
captures.push((input as { clip?: unknown })?.clip);
1718+
return { data: header.toString("base64") };
1719+
}
1720+
return send(method, input);
1721+
});
1722+
}
1723+
const snapshot = broker.invoke<PreviewAutomationSnapshot>({
1724+
scope,
1725+
tabId,
1726+
operation: "snapshot",
1727+
input: {},
1728+
});
1729+
if (scenario === "zoom never settles") {
1730+
expect(JSON.stringify(yield* Effect.exit(snapshot))).toContain("zoom kept changing");
1731+
expect(captures).toEqual([]);
1732+
expect(ratioReads).toBe(3);
1733+
return;
1734+
}
1735+
const result = yield* snapshot;
1736+
expect(captures).toEqual([
1737+
phone
1738+
? undefined
1739+
: {
1740+
x: 0,
1741+
y: scenario === "zoomed and scrolled" ? 200 : 0,
1742+
width: 1280,
1743+
height: 800,
1744+
scale: scenario === "headless" ? 0.5 : 2 / 3,
1745+
},
1746+
]);
1747+
expect(result.screenshot).toMatchObject(size);
1748+
if (scenario === "headless") expect(ratioReads).toBe(0);
1749+
if (scenario.startsWith("zoom after")) expect(ratioReads).toBe(2);
1750+
}),
1751+
).pipe(Effect.provide(layer)),
1752+
);

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,20 @@ const make = Effect.gen(function* () {
15681568
return started;
15691569
};
15701570

1571+
// Desktop pages report CSS sizes and a ratio that includes zoom. Captures use display pixels,
1572+
// so read the page's applied zoom rather than the server's possibly pending value.
1573+
const snapshotRendering = async (tab: ServerTab) => {
1574+
if (!tab.desktop) return { renderScale: RENDER_SCALE };
1575+
const page = await ServerBrowserPage.readPageMetrics(tab.cdp);
1576+
return {
1577+
renderScale: page.ratio / page.zoom,
1578+
viewport: {
1579+
width: Math.round(page.width * page.zoom),
1580+
height: Math.round(page.height * page.zoom),
1581+
},
1582+
};
1583+
};
1584+
15711585
// Scaled captures repaint every screencast; pause them to avoid leaking that frame.
15721586
const withScreencastsPaused = <A>(tab: ServerTab, capture: () => Promise<A>): Promise<A> =>
15731587
withCaptureLock(tab, async () => {
@@ -1990,8 +2004,8 @@ const make = Effect.gen(function* () {
19902004
return { tabId: tab.tabId, colorScheme };
19912005
}
19922006
case "snapshot": {
1993-
return withScreencastsPaused(tab, () =>
1994-
ServerBrowserPage.snapshot({ ...tab, renderScale: RENDER_SCALE }),
2007+
return withScreencastsPaused(tab, async () =>
2008+
ServerBrowserPage.snapshot({ ...tab, ...(await snapshotRendering(tab)) }),
19952009
);
19962010
}
19972011
case "click": {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,29 @@ describe("server browser drag", () => {
319319
}
320320
});
321321
});
322+
323+
it("reads real snapshot metrics after page script reassigns the globals", async () => {
324+
const browser = await chromium.launch({ headless: true });
325+
try {
326+
const context = await browser.newContext({
327+
viewport: { width: 1280, height: 800 },
328+
deviceScaleFactor: 1.5,
329+
});
330+
const page = await context.newPage();
331+
const cdp = await context.newCDPSession(page);
332+
await page.setContent(
333+
"<script>devicePixelRatio = -1.5; innerWidth = 99999; innerHeight = 1e7;</script>",
334+
);
335+
expect(await page.evaluate("[devicePixelRatio, innerWidth, innerHeight]")).toEqual([
336+
-1.5, 99999, 1e7,
337+
]);
338+
expect(await ServerBrowserPage.readPageMetrics?.(cdp)).toEqual({
339+
ratio: 1.5,
340+
width: 1280,
341+
height: 800,
342+
zoom: 1,
343+
});
344+
} finally {
345+
await browser.close();
346+
}
347+
});

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

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,85 @@ const SNAPSHOT_SCRIPT = `(() => {
150150
};
151151
})()`;
152152

153+
/**
154+
* Width and height from a PNG's header, so the reported size is the image's own. Null for data
155+
* that is not a PNG.
156+
*/
157+
const pngSize = (base64: string) => {
158+
const header = Buffer.from(base64.slice(0, 32), "base64");
159+
return header.length >= 24 && header.toString("latin1", 12, 16) === "IHDR"
160+
? { width: header.readUInt32BE(16), height: header.readUInt32BE(20) }
161+
: null;
162+
};
163+
164+
/** A page's viewport in device-independent pixels, the units a capture clip is given in. */
165+
type Viewport = { readonly width: number; readonly height: number };
166+
167+
/**
168+
* A page's display ratio and CSS viewport, and its zoom. The ratio and viewport are read in an
169+
* isolated world, since page script can reassign `devicePixelRatio`, `innerWidth`, and
170+
* `innerHeight` in its own.
171+
*/
172+
export const readPageMetrics = async (cdp: CDPSession) => {
173+
const { frameTree } = await cdp.send("Page.getFrameTree");
174+
const { executionContextId } = await cdp.send("Page.createIsolatedWorld", {
175+
frameId: frameTree.frame.id,
176+
// Chromium reuses a named world; an unnamed one is created again on each call.
177+
worldName: "t3-preview-metrics",
178+
});
179+
const readZoom = async () =>
180+
(await cdp.send("Page.getLayoutMetrics")).cssVisualViewport.zoom ?? 1;
181+
// The desktop applies a published zoom on its own schedule. Reading the zoom on both sides of
182+
// the ratio and viewport read catches a change in between, which would pair old CSS sizes
183+
// with the new zoom.
184+
let zoom = await readZoom();
185+
for (let attempt = 1; ; attempt += 1) {
186+
const { result } = await cdp.send("Runtime.evaluate", {
187+
expression: "({ ratio: devicePixelRatio, width: innerWidth, height: innerHeight })",
188+
contextId: executionContextId,
189+
returnByValue: true,
190+
});
191+
const zoomAfter = await readZoom();
192+
if (zoomAfter === zoom) {
193+
return {
194+
...(result.value as {
195+
readonly ratio: number;
196+
readonly width: number;
197+
readonly height: number;
198+
}),
199+
zoom,
200+
};
201+
}
202+
// A capture sized from a torn read is cropped and oversized, so fail instead.
203+
if (attempt === 3) {
204+
throw new Error(
205+
"The page's zoom kept changing while the snapshot read its size. Try the snapshot again.",
206+
);
207+
}
208+
zoom = zoomAfter;
209+
}
210+
};
211+
153212
// Scaled captures repaint live screencasts, so callers pause them. Clips use document offsets.
154213
export const captureViewport = async (
155214
page: Page,
156215
cdp: CDPSession,
157-
options: { readonly format: "png" | "jpeg"; readonly quality?: number; readonly scale: number },
216+
options: {
217+
readonly format: "png" | "jpeg";
218+
readonly quality?: number;
219+
readonly scale: number;
220+
readonly viewport?: Viewport;
221+
},
158222
) => {
159223
let clip;
160224
if (options.scale < 1) {
161-
const viewport = page.viewportSize() ?? { width: 1280, height: 800 };
225+
const viewport = options.viewport ?? page.viewportSize() ?? { width: 1280, height: 800 };
162226
const { cssVisualViewport } = await cdp.send("Page.getLayoutMetrics");
227+
// The offsets are CSS pixels; a clip is in device-independent pixels, which page zoom scales.
228+
const zoom = cssVisualViewport.zoom ?? 1;
163229
clip = {
164-
x: cssVisualViewport.pageX,
165-
y: cssVisualViewport.pageY,
230+
x: cssVisualViewport.pageX * zoom,
231+
y: cssVisualViewport.pageY * zoom,
166232
...viewport,
167233
scale: options.scale,
168234
};
@@ -179,11 +245,13 @@ export const snapshot = async (input: {
179245
readonly page: Page;
180246
readonly cdp: CDPSession;
181247
readonly renderScale: number;
248+
/** For a page Playwright did not size, such as one the desktop draws. */
249+
readonly viewport?: Viewport;
182250
readonly consoleEntries: ReadonlyArray<PreviewAutomationConsoleEntry>;
183251
readonly networkEntries: ReadonlyArray<PreviewAutomationNetworkEntry>;
184252
readonly actionTimeline: PreviewAutomationSnapshot["actionTimeline"];
185253
}): Promise<PreviewAutomationSnapshot> => {
186-
const viewport = input.page.viewportSize() ?? { width: 1280, height: 800 };
254+
const viewport = input.viewport ?? input.page.viewportSize() ?? { width: 1280, height: 800 };
187255
const scale = Math.min(1, MAX_SCREENSHOT_WIDTH / (viewport.width * input.renderScale));
188256
const state = refsFor(input.page);
189257
invalidateRefs(input.page);
@@ -196,7 +264,7 @@ export const snapshot = async (input: {
196264
>
197265
>,
198266
input.page.ariaSnapshot({ mode: "ai", boxes: true, timeout: DEFAULT_TIMEOUT_MS }),
199-
captureViewport(input.page, input.cdp, { format: "png", scale }),
267+
captureViewport(input.page, input.cdp, { format: "png", scale, viewport }),
200268
]);
201269
if (state.generation !== generation) {
202270
throw new ServerBrowserOperationError(
@@ -220,8 +288,10 @@ export const snapshot = async (input: {
220288
screenshot: {
221289
mimeType: "image/png",
222290
data,
223-
width: Math.round(viewport.width * input.renderScale * scale),
224-
height: Math.round(viewport.height * input.renderScale * scale),
291+
...(pngSize(data) ?? {
292+
width: Math.round(viewport.width * input.renderScale * scale),
293+
height: Math.round(viewport.height * input.renderScale * scale),
294+
}),
225295
},
226296
};
227297
};

0 commit comments

Comments
 (0)