Repository navigation
Fixed store above-fold background images - #1240
girishpanchal30 wants to merge 13 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes requested
The shared selector map works, but overlapping watchers, background <img> elements and four-URL backgrounds still lose or reject above-fold background data.
assets/js/modules/image-detector.js:104—correctnessAn above-fold<img>that matches a background watcher is still never recorded.setupBackgroundImageObservationmarks it withdata-optml-bg-selectorand observes it (background.js:47-58). The observer callback sends everyIMGto thedata-opt-idbranch, and theelse ifbackground branch never runs for it. Its watcher is sent as{}, and the server hides it with:not(.optml-bg-lazyloaded). Checkdata-optml-bg-selectorin its ownif, independent of theIMGbranch. Add abackground.test.jsorimage-detectortest for an<img>with a background, with and withoutdata-opt-id.
Validation details
- Files reviewed: 4/4 changed files. Unchecked rows: the PHP 8 fatal without
b(issue #1204), lazyload class timing, image dimension snapshot timing, server storage per device. - Jest at HEAD passed both changed suites
cd /workspace/checks/R1/head && ls node_modules/.bin | grep -E '^(jest|babel-jest)$'; set -o pipefail; timeout 280 npx j…(run R3, exit 0). - With pr-base source, the three new or changed assertions fail
cd /workspace/checks/R1/head && npx jest --verbose assets/js/modules/__tests__/main.test.js assets/js/modules/__tests__/…(run R4, exit 0). - Headless Chromium with the real observers confirmed the overlap and four-URL outputs
cd /workspace/checks/R2 && python3 - <<'EOF' import re p='/workspace/checks/R2/run.cjs' s=open(p).read() s=s.replace("co…(run R17, exit 0).
Untested areas
- No WordPress site ran, so server storage per device and the generated CSS were not checked.
- The PHP 8 fatal without
b(issue #1204) was not re-checked against the endpoint. - A second pending background is dropped when its lazyload class arrives in a later task. The loader adds classes together at startup, so the real trigger is unconfirmed.
- Image dimension snapshot timing was not checked.
🤖 Automated review · run code-review-agent_6afa98ec222bf0.92208230.
🤖 Review agent — review posted ✅ on 56209c98 · changes requested · 3 findings · 37 min
Run code-review-agent_6afa98ec222bf0.92208230 · trail
There was a problem hiding this comment.
🟡 Changes requested
The three earlier findings are fixed, but over 100 above-fold backgrounds on one watcher still make the endpoint reject the whole profile.
Validation details
- Files reviewed: 6/6 changed files. Unchecked rows: Caps and limits: Pagination, Caps and limits: Realistic volume.
- Jest at HEAD on the three changed suites:
cd /workspace/checks/R1/head && (set -o pipefail; npx jest --no-cache assets/js/modules/__tests__/background.test.js ass…printedTests: 74 passed, 74 total(run R4). - The new tests with the modules of the previous head fail on their assertions:
cd /workspace/checks/R1 && rm -rf base && cp -r head base && cd base && rm -rf node_modules && ln -s ../head/node_module…printedTests: 5 failed, 67 skipped, 2 passed, 74 total(run R5). - The
<img>background finding from the earlier review body is fixed by the separate background check and re-observation.
Questions for the author
- The cap keeps the first three LCP background URLs. Is it intended that a fourth layer is never preloaded?
- Do server consumers treat stored
bgandlcp.bgUrlsas complete lists? Later URLs are then never optimized as above-fold.
Untested areas
- No WordPress site ran. Server storage per device and the lazyload CSS were not checked.
- A second pending background may be missed after
classObserver.disconnect(). The lazyloader is loaded unpinned from the CDN. - More than 100 matched watchers make the endpoint reject the profile. This also happened before the change.
- Caps and limits: Pagination and Realistic volume were not settled.
- The variant pass did not settle these places, where the fixed cause may still reach:
assets/js/modules/background.js:29-42,63-82— unverified: Shared classObserver.disconnect(element) can stop observation of later pending elements. Establishing production reachability still requires the external lazyloader’s mutation orde…
🤖 Automated review · run code-review-agent_6afaa719cc3310.15504553.
🤖 Review agent — review posted ✅ on e7855563 · changes requested · 1 finding · 28 min
Run code-review-agent_6afaa719cc3310.15504553 · trail
There was a problem hiding this comment.
🟡 Changes requested
The per-watcher selector cap is fixed, but more than 100 matching watchers still make the endpoint reject the whole profile.
Validation details
- Files reviewed: 6/6 changed files. Unchecked rows: issue #1204 PHP 8 fatal without
b, Caps and limits: Realistic volume, Caps and limits: Stable order. - The new test passes at HEAD: "Tests: 1 passed, 24 skipped, 25 total" (not in the output of run R3).
- With
main.jsfrom e785556, the new test fails on its assertion:cd /workspace/checks/T1/tree && git -C /repo show e785556329a764a3c8bd814a41546d3529179bed:assets/js/modules/main.js > a…(run R4, exit 0). - With pr-base modules, the test fails in its mock setup, which is inconclusive:
cd /workspace/checks/T1/tree && B=636463e382cf5e07543fa27a4dee3cc561266f8e; for f in main.js background.js image-detecto…(run R5, exit 0).
Questions for the author
- Does the lazyload CSS treat selectors past the 100 cap as below the fold?
- Do stored
bglists count as complete for later optimization?
Untested areas
- No WordPress site ran. Server storage and lazyload CSS were not exercised.
- A second pending background may be missed after
classObserver.disconnect(). This was not rechecked. - Issue #1204 PHP 8 fatal without
b, Realistic volume and Stable order were not settled. - The requirements check was reused from run code-review-agent_6afaa719cc3310.15504553, not rerun.
🤖 Automated review · run code-review-agent_6afaaf1f373149.78652713.
🤖 Review agent — review posted ✅ on ca7ee9fc · changes requested · 1 finding · 10 min
Run code-review-agent_6afaaf1f373149.78652713 · trail
There was a problem hiding this comment.
🟡 Changes requested
The watcher cap fixes the rejected profile, but watchers after the 100th now lose their background lazyload rule.
Validation details
- Files reviewed: 6/6 changed files. Unchecked rows: Caps and limits: Realistic volume, Stable order and Completeness assumption.
- The new test passes at HEAD: "Tests: 25 skipped, 1 passed" (not in the output of run R4)
- With
main.jsfrom ca7ee9f, the test fails on its assertion: "+ ".bg-100"," (not in the output of run R5) - With pr-base modules, the test fails in its mock setup, which is inconclusive:
set -o pipefail; cd /workspace/checks/T1 && rm -rf tree-base && cp -r tree tree-base && cd tree-base && for f in assets/…(run R6, exit 0) - The variant pass found no other uncapped endpoint limit.
Questions for the author
- Does WordPress give
ba countable default when it is missing or null, beforeoptimizations()runs? - Is profiling meant to be best-effort beyond the endpoint limits?
Untested areas
- No WordPress site ran. Server storage and lazyload CSS were not exercised.
- Realistic volume, Stable order and Completeness assumption were not settled.
- The requirements check was reused from run code-review-agent_6afaa719cc3310.15504553, not rerun.
- The variant pass did not settle these places, where the fixed cause may still reach:
- not reached: prior uncertainties concerning mixed-service URL filtering, selectors changing between observation and extraction, and external lazyloader timing around classObserver.disconnect(el…
🤖 Automated review · run code-review-agent_6afac66e655ef0.56882325.
🤖 Review agent — review posted ✅ on 681af46e · changes requested · 1 finding · 23 min
Run code-review-agent_6afac66e655ef0.56882325 · trail
There was a problem hiding this comment.
🟡 Changes requested
The unlisted-watcher fix works, but a background without a class or id now breaks the device's lazyload rule.
Validation details
- Files reviewed: 10/10 changed files. Unchecked rows: Caps and limits: Realistic volume and Stable order.
- Jest at HEAD passes: "Tests: 67 passed, 67 total" (not in the output of run R2)
- The new background test fails with
background.jsfrom 681af46:cd /workspace/checks/R1 && rm -rf base && mkdir base && (cd head && tar --exclude=./node_modules -cf - .) | tar -x -C ba…printedNumber of calls: 0(run R3) - The new PHP cases pass at HEAD:
cd /workspace/checks/R1/head && set -o pipefail; timeout 280 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Bg_…printedOK (2 tests, 7 assertions)(run R7) - Real Chromium with the real lazyloader sent trailing-dot keys in
b:cd /workspace/checks/X2b/site && sed -i 's#</body>#<script async src="optimizer.js"></script>\n</body>#' index.html && t…(run R16, exit 0) - Question for the author: the PR body still calls the
:not()combination a separate fix. The commits now include it. - Question for the author: does the REST pipeline default a missing or null
bto[]before the callback?
Untested areas
- Realistic volume and Stable order were not settled.
- Observation timing after the 300/600 ms wait was not settled.
- Raw versus stripped watcher keys were not compared on a real site.
- The variant pass did not settle these places, where the fixed cause may still reach:
assets/js/modules/background.js:63-67,119-127— unverified: Extraction regenerates selector keys instead of reusing recorded ones. An element whose only class becomes optml-bg-lazyloaded generates a trailing . after filtering, missing its r…assets/js/modules/dom-utils.js:41-50— unverified: Extraction regenerates selector keys instead of reusing recorded ones. An element whose only class becomes optml-bg-lazyloaded generates a trailing . after filtering, missing its r…inc/admin.php:603-608— unverified: Observation ends after 300/600 ms regardless of remaining work. Whether ordinary above-fold backgrounds complete later requires the lazyloader’s timing contract; its script uses un…
🤖 Automated review · run code-review-agent_6afadb70726a12.66020122.
🤖 Review agent — review posted ✅ on 7cbde284 · changes requested · 3 findings · 35 min
Run code-review-agent_6afadb70726a12.66020122 · trail
There was a problem hiding this comment.
🟡 Changes requested
The selector and rule fixes work, but the selector caps still drop above-fold backgrounds without a marker: see this thread.
Validation details
- Files reviewed: 11/11 changed files. Unchecked rows: Caps and limits: Realistic volume, Stable order, and issue #1204 PHP 8 fatal without
b. - The new
dom-utils.test.jscases pass at HEAD: 43 of 43 tests passed (run R3). - The same cases fail with the old
dom-utils.jsfrom 7cbde28 and from pr-base:cd /workspace/checks/R1/tree && git -C /repo show 7cbde284a59926ac3397fd1e8fdb21756fb696f3:assets/js/modules/dom-utils.j…printedReceived: "#c > div.:nth-of-type(2)"(run R4) - Stored watcher values are always arrays, so the removed
empty()branch leaves no warning path. - Reused, not rerun: Jest and PHPUnit runs of run code-review-agent_6afadb70726a12.66020122 at 7cbde28.
Untested areas
- Realistic volume and Stable order of the caps were not settled. No real-site data was available.
- Issue #1204 PHP 8 fatal without
bwas not rechecked in this run. - Observation after the 300/600 ms wait depends on the external lazyloader. Its timing was not established.
- The variant pass did not settle these places, where the fixed cause may still reach:
assets/js/modules/main.js:143-155— unverified: Extraction now follows the wait, but observation still ends after 300/600 ms. The loader is asynchronously fetched from /v2/latest/optimole_lib…; its resolved source and timing con…inc/admin.php:603-609— unverified: Extraction now follows the wait, but observation still ends after 300/600 ms. The loader is asynchronously fetched from /v2/latest/optimole_lib…; its resolved source and timing con…
🤖 Automated review · run code-review-agent_6afbfd1de97827.42297692.
🤖 Review agent — review posted ✅ on b404f543 · changes requested · 0 findings · 21 min
Run code-review-agent_6afbfd1de97827.42297692 · trail
There was a problem hiding this comment.
🟢 Approved
The PR passes: lists at the client caps now skip the hide rule, so cut above-fold backgrounds are no longer delayed.
Validation details
- Files reviewed: 3/3 files changed since the last review. Unchecked rows: Caps and limits: Realistic volume.
- At HEAD, the background selector tests pass:
cd /repo && set -o pipefail; timeout 290 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Bg_Selector::' 2>&1 | t…printedOK (29 tests, 49 assertions)(run R3) - The two new cap tests fail with the earlier
Lazyload.phplogic:cd /workspace/checks/R1/B && set -o pipefail; timeout 290 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Bg_Sel…printedFailures: 2(run R5) - Reused, not rerun: Jest runs and the variant pass of run code-review-agent_6afbfd1de97827.42297692 at b404f54. The client files are unchanged since then.
- Question for the author: the PR body calls the
:not()combination a separate fix.Lazyload.phpnow includes it. Please update the body.
Untested areas
- Caps and limits: Realistic volume was not settled. No real-site data was available.
- The variant pass was not rerun. Observation still ends after the 300/600 ms wait, and the external lazyloader timing is unknown.
- The two-device browser reproduction was not run. The sandbox web server was not available.
🤖 Automated review · run code-review-agent_6afc059c4e9366.79461919.
🤖 Review agent — review posted ✅ on 3fc86e30 · approved · 0 findings · 14 min
Run code-review-agent_6afc059c4e9366.79461919 · trail
There was a problem hiding this comment.
🟢 Approved
This approves the personalized CSS change, and the note asks whether real pages reach the 100-item caps.
Validation details
- Files reviewed: 4/4 files changed since the last review. Unchecked rows: Caps and limits: Realistic volume.
- At HEAD, the background selector tests pass:
set -o pipefail; C=/workspace/checks/C4; cd $C/head && timeout 290 vendor/bin/phpunit --do-not-cache-result --filter 'Te…printedOK (30 tests, 48 assertions)(run R2) - At HEAD, the viewport CSS tests pass:
set -o pipefail; C=/workspace/checks/C4; sed -n 228,231p $C/prior/tests/test-bg-selector.php; cd $C/head && timeout 290 …printedOK (10 tests, 19 assertions)(run R4) - With the
3fc86e3Lazyload.phpandmanager.php, the changed tests fail:set -o pipefail; C=/workspace/checks/C4; cd $C/prior && timeout 290 vendor/bin/phpunit --do-not-cache-result --filter 'T…printedTests: 30, Assertions: 47, Failures: 4.(run R3) andset -o pipefail; C=/workspace/checks/C4; cd $C/prior && timeout 290 vendor/bin/phpunit --do-not-cache-result --filter 'T…printedTests: 10, Assertions: 19, Failures: 1.(run R5) - A source trace shows that an empty string reaches
inc/manager.php:567only when every device hides nothing on purpose. - Question for the author: the PR body still calls the
:not()combination a separate fix. Please update the body.
Untested areas
- Caps and limits: Realistic volume was not settled. No real-site data was available.
- The variant pass was not rerun. The client files are unchanged since run code-review-agent_6afbfd1de97827.42297692 checked them.
- The two-device browser check was not run. The sandbox web server was down.
🤖 Automated review · run code-review-agent_6afc42111d4ac8.23804080.
🤖 Review agent — review posted ✅ on 31b90379 · approved · 0 findings · 17 min
Run code-review-agent_6afc42111d4ac8.23804080 · trail
| if ( empty( $above_fold_selectors ) ) { | ||
| $css_selectors[] = 'html ' . strip_tags( $selector ) . ':not(.optml-bg-lazyloaded)'; | ||
| // A full list may have been cut, so hiding the watcher could delay unlisted above-fold elements. | ||
| if ( count( $above_fold_selectors ) >= self::MAX_ABOVE_FOLD_SELECTORS ) { |
There was a problem hiding this comment.
note A watcher with 100 or more above-fold elements, or a page with 100 or more matching watchers, now gets no background lazyload for that watcher or page. When every device is capped, the general CSS is removed too. Do real pages reach these caps? If they stay far below 100, this is safe. If common layouts reach them, those pages lose background lazyload.
There was a problem hiding this comment.
These caps only bite on extreme pages: 100 watchers means ~93 custom watchers matching on one page (the profiler only reports watchers with matches; there are 7 defaults), and 100 above-fold elements means 100 backgrounds of one watcher visible in the first viewport. At those sizes development rejected the whole profile (> 100 in rest.php), so the generic CSS hid every background, above-the-fold ones included. Now only the capped watcher skips its hide rule; with a full watcher list the listed watchers keep their rules. The cost is eager loading of that watcher's below-fold backgrounds on such pages, the safer trade than hiding above-fold backgrounds the profiler couldn't list.
There was a problem hiding this comment.
Thanks, that settles the trade-off. At the base commit the endpoint rejected the whole profile above 100 watchers or 100 above-fold selectors, so the generic CSS hid every background. Skipping only the capped watcher's hide rule is the safer result. The commits since then do not change the caps.
There was a problem hiding this comment.
🟡 Changes requested
The observer flush and the 601px desktop rule work, but image dimensions are still read before the profiler waits.
Questions for the author:
-
Are background URLs after the third only optional hints? Or does a consumer need them for above-fold handling? The client keeps three per element.
-
The PR body still calls the
:not()combination a separate fix. Please update the body. -
assets/js/modules/main.js:122—correctnessImage dimensions are read once, before the profiler waits. Profiling starts atDOMContentLoaded(assets/js/optimizer.js:36-37). An image withoutwidthorheightthat has not loaded yet hasnaturalWidth0.detectImageDimensionsthen skips it (assets/js/modules/image-detector.js:46-59). The image often loads during the srcset wait or the 300/600 ms background wait. It is still missing fromm, so the server never stores its dimensions. This PR fixed the same read-before-wait order for background URLs. MovedetectImageDimensions(allOptimoleImages)after the waits and the flush, before the payload is built. Add amain.test.jscase wherenaturalWidthbecomes non-zero during the wait, and expect that image inm.
Validation details
- Files reviewed: 7/7 files changed since the last review. Unchecked rows: Caps and limits: Realistic volume, and the consumer check of the
get_current_personalized_css()signature. - At HEAD, the changed Jest files pass:
cd /workspace/checks/C3/head && set -o pipefail; timeout 280 npx jest assets/js/modules/__tests__/image-detector.test.js…printed53 passed, 53 total(run R2). - With the prior
main.js, the new flush-order test fails:cd /workspace/checks/C3/head && cp assets/js/modules/image-detector.js /workspace/checks/C3/head-image-detector.js && cp…printed2 failed, 51 passed, 53 total(run R4). - At HEAD, the background selector tests pass:
cd /workspace/checks/C3/head && set -o pipefail; timeout 280 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Bg_…printedOK (31 tests, 51 assertions)(run R5). - With the prior
Lazyload.php, the 601px assertions fail:cd /workspace/checks/C3/head && cp inc/v2/BgOptimizer/Lazyload.php /workspace/checks/C3/head-Lazyload.php && cp /workspa…printedFailures: 3(run R6).
Untested areas
- Caps and limits: Realistic volume. No real-site data was available.
- The
get_current_personalized_css()consumer check reused the prior run's result. - The LCP observer may drop a queued entry at its 1500 ms timeout. Browser delivery timing was not checked.
- The two-device browser check was not run. The site was not served.
- The variant pass did not settle these places, where the fixed cause may still reach:
assets/js/modules/lcp.js:49-71— unverified: The timeout disconnects without takeRecords(). Whether an already-queued LCP entry can be discarded requires the PerformanceObserver delivery/disconnect contract; that platform sou…
🤖 Automated review · run code-review-agent_6afd3c7159a9e1.07744730.
🤖 Review agent — review posted ✅ on 03ee02f3 · changes requested · 2 findings · 19 min
Run code-review-agent_6afd3c7159a9e1.07744730 · trail
| describe('flushIntersectionObserver', () => { | ||
| test('records an element the observer has not reported before it is disconnected', async () => { | ||
| const aboveTheFoldImages = []; | ||
| const selectorMap = new Map([['[style*="background-image:url("]', []]]); | ||
| document.body.innerHTML = '<img data-opt-id="9" style="background-image:url(a.jpg)" data-optml-bg-selector="x" data-optml-specific-selector="img.late" />'; | ||
| const observer = optmlImageDetector.createIntersectionObserver(aboveTheFoldImages, selectorMap); | ||
| observer.takeRecords = jest.fn(() => [{ isIntersecting: true, target: document.querySelector('img') }]); | ||
|
|
||
| await optmlImageDetector.flushIntersectionObserver(observer, aboveTheFoldImages, selectorMap); | ||
|
|
||
| expect(aboveTheFoldImages).toEqual([9]); | ||
| expect(selectorMap.get('[style*="background-image:url("]')).toEqual(['img.late']); | ||
| }); | ||
| }); | ||
|
|
There was a problem hiding this comment.
tests The timeout branch of flushIntersectionObserver has no test. This case lets the jsdom requestAnimationFrame win the race. It still passes if setTimeout(resolve, 100) is removed. That branch keeps profiling from waiting until a hidden tab is shown again, since hidden tabs render no frames. Add a case in this describe. Call jest.useFakeTimers() first, then stub window.requestAnimationFrame with a function that never calls back. Fake timers replace requestAnimationFrame, so the stub must come after them. Run await jest.advanceTimersByTimeAsync(100), then expect the entry from takeRecords in aboveTheFoldImages and selectorMap. Restore real timers at the end.
There was a problem hiding this comment.
Fixed. The new case records queued entries when no frame is rendered, as in a hidden tab stubs requestAnimationFrame after the fake timers and advances 100 ms. It passes at HEAD 1ad036a. With the setTimeout(resolve, 100) arm removed from flushIntersectionObserver, it fails with Exceeded timeout of 5000 ms for a test.
There was a problem hiding this comment.
🟢 Approved
The PR passes: image dimensions are now read after the waits, and the flush timeout has a test.
Question for the author:
- Do above-fold or LCP consumers need background URLs after the third? The client keeps three per element.
Validation details
- Files reviewed: 3/3 files changed since the last review. Unchecked rows: Caps and limits: Realistic volume, Caps and limits: Stable order, and the
get_current_personalized_css()signature consumer check. - At HEAD, both changed Jest files pass:
cd /workspace/checks/C1/head && set -o pipefail; timeout 280 npx jest assets/js/modules/__tests__/image-detector.test.js…printed55 passed(run R2). - Without the 100 ms timeout, the hidden-tab test fails:
cd /workspace/checks/C1/head && set -o pipefail; timeout 280 npx jest --verbose assets/js/modules/__tests__/image-detect…printedExceeded timeout of 5000 ms for a test.(run R4). - With the prior
main.js, the dimensions test fails: "Cannot read properties of null (reading 'm')" (not in the output of run R5).
Untested areas
- The variant pass was not rerun. The LCP observer may still drop a queued entry at its 1500 ms timeout.
- The three unchecked rows above cover code these commits do not touch.
- The natural size a browser reports for an unsized lazyload placeholder was not checked.
- The PR body still calls the
:not()combination a separate fix.
🤖 Automated review · run code-review-agent_6afd4a0f8aed37.54852406.
🤖 Review agent — review posted ✅ on 1ad036a3 · approved · 0 findings · 19 min
Run code-review-agent_6afd4a0f8aed37.54852406 · trail
All Submissions:
Changes proposed in this Pull Request:
The page profiler never recorded above-the-fold background images: every background selector was sent as
{}and stored as an empty list for both devices.runProfilinggave the IntersectionObserver one selector map, then replaced it with the map returned byprocessBackgroundSelectors. The observer kept checking the original, empty map, so it never recorded a background element.processBackgroundSelectorsnow fills the map passed to it, which is the same map the observer uses.null.main.test.jsandbackground.test.js.Note: the profiler now collects and stores the right data. However, the personalized lazyload CSS still doesn't exempt several above-the-fold backgrounds, because of how
Lazyload::collect_device_selectorscombines the:not()selectors. That is a separate server-side fix and will get its own issue.Closes #1216 .
How to test the changes in this Pull Request:
npm run build:optimizer).<div style="background-image:url(...);height:400px">elements.?optml_debug=1and the console open.Sending background selectors:. Or open the/wp-json/optml/v1/optimizationsrequest in the Network tab and look atb.{}, and noElement with selector "…" is above the foldline is logged.?optml_debug=trueand read themeasurement#…comment after</html>. Both devices should list the two elements underbg, not empty maps.Other information: