Skip to content

Commit 0c1002a

Browse files
committed
fix(server): poll image completion after resizing a measured page
A dropped pending image request fires neither load nor error, which left the resize settle waiting until the measure timeout. image.complete covers loaded, broken, and dropped requests.
1 parent 3a6f910 commit 0c1002a

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

‎apps/server/src/htmlRender/headlessChrome.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ const consoleMessageFromEvent = (method: string, params: unknown): ConsoleMessag
148148
const SETTLE_EXPRESSION =
149149
"document.fonts.ready.then(() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve(true)))))";
150150
// A resize can select another responsive image, which a fresh load would wait for.
151-
const RESIZE_SETTLE_EXPRESSION = `new Promise((resolve) => requestAnimationFrame(resolve)).then(() => Promise.all([...document.images].filter((image) => !image.complete && image.loading !== "lazy").map((image) => new Promise((resolve) => { image.addEventListener("load", resolve, { once: true }); image.addEventListener("error", resolve, { once: true }); })))).then(() => ${SETTLE_EXPRESSION})`;
151+
// Poll `complete`: a dropped pending request fires neither load nor error.
152+
const RESIZE_SETTLE_EXPRESSION = `new Promise((resolve) => requestAnimationFrame(function wait() { if ([...document.images].every((image) => image.complete || image.loading === "lazy")) resolve(); else requestAnimationFrame(wait); })).then(() => ${SETTLE_EXPRESSION})`;
152153
// scrollHeight is at least viewport height; use the root box for shorter pages.
153154
const MEASURE_EXPRESSION =
154155
"(() => { const root = document.documentElement; return root.scrollHeight > root.clientHeight ? root.scrollHeight : root.getBoundingClientRect().height; })()";

0 commit comments

Comments
 (0)