Skip to content

Commit 3af00b8

Browse files
committed
fix(server): recheck images after settling a resized page
Settling runs resize observers, which can swap an image's source after the first completion check. Repeat the wait until no image is pending, for at most five rounds so rotating images cannot stall measuring.
1 parent 69abc22 commit 3af00b8

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
@@ -149,7 +149,8 @@ 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. Search open
151151
// shadow roots too, and poll `complete`: a dropped pending request fires neither load nor error.
152-
const RESIZE_SETTLE_EXPRESSION = `new Promise((resolve) => requestAnimationFrame(() => { const images = []; const collect = (root) => { for (const element of root.querySelectorAll("*")) { if (element instanceof HTMLImageElement && element.loading !== "lazy") images.push(element); if (element.shadowRoot) collect(element.shadowRoot); } }; collect(document); (function wait() { if (images.every((image) => image.complete)) resolve(); else requestAnimationFrame(wait); })(); })).then(() => ${SETTLE_EXPRESSION})`;
152+
// Settling runs resize observers, which can swap images again; a few rounds bound rotating images.
153+
const RESIZE_SETTLE_EXPRESSION = `(async () => { const frame = () => new Promise((resolve) => requestAnimationFrame(resolve)); const pending = (root = document) => [...root.querySelectorAll("*")].some((element) => (element instanceof HTMLImageElement && element.loading !== "lazy" && !element.complete) || (element.shadowRoot !== null && pending(element.shadowRoot))); for (let round = 0; round < 5; round += 1) { await frame(); while (pending()) await frame(); await ${SETTLE_EXPRESSION}; if (!pending()) return true; } return true; })()`;
153154
// scrollHeight is at least viewport height; use the root box for shorter pages.
154155
const MEASURE_EXPRESSION =
155156
"(() => { const root = document.documentElement; return root.scrollHeight > root.clientHeight ? root.scrollHeight : root.getBoundingClientRect().height; })()";

0 commit comments

Comments
 (0)