Skip to content

Page profiler never stores above-the-fold background images: b is always sent with empty selector maps #1216

Description

@rodica-andronache

Description

On a normal page visit, the page profiler sends every background selector with an empty object, for example "b":{"[style*=\"background-image:url(\"]":{}}. This happens even when a background element is above the fold and its URL has been found. The stored profile ends up with "bg":{"<selector>":[]} for every device. Optimole therefore never learns which background images are above the fold, and the lazy-load CSS can only exempt the LCP background.

Cause

In assets/js/modules/main.js, the IntersectionObserver is created with the first selectorMap instance. Shortly after, selectorMap is replaced with a new Map:

// main.js:110-111
let selectorMap = new Map();
const observer = optmlImageDetector.createIntersectionObserver(aboveTheFoldImages, selectorMap);
...
// main.js:135
selectorMap = backgroundResult.selectorMap;

The observer callback (image-detector.js:100) checks selectorMap.has(baseSelector) against the original Map, which is always empty. Above-the-fold background elements are therefore never recorded. Meanwhile _processBackgroundSelectors (main.js:174) builds b from the new Map, which has the selector keys but no entries. With ?optml_debug=1, the log never prints Element with selector "…" is above the fold for background elements.

Expected: each matched selector lists the above-the-fold elements and their URLs, e.g. {"[style*=\"background-image:url(\"]":{"#content > … > div.wp-block-cover__image-background…":["https://…/qa-hero-bg.jpg"]}}.

Verification of the cause

I changed one line in the served script so it fills the existing Map instead of replacing it (backgroundResult.selectorMap.forEach((v, k) => selectorMap.set(k, v))). With that change:

  • The payload contained the selector and its URL.
  • The server stored them for mobile and desktop.
  • The lazy-load CSS on the page added a :not(...) exclusion for each above-the-fold background element, not only the LCP one.

I tested this on a page with two background groups side by side.

Impact

On pages with a single hero, nothing visible changes, because the hero is also the LCP element and is exempted and preloaded anyway. On pages with several background images above the fold, the ones that aren't the LCP element are still lazy-loaded. They can appear late and cost LCP/CLS points, which is what the profiler is meant to prevent.

Possible fix

Fill the existing Map, or pass the Map from processBackgroundSelectors to the observer, instead of reassigning selectorMap.

A related timing detail: extractBackgroundImageUrls runs right after processBackgroundSelectors (main.js:139), before the 300/600 ms wait. A background that gets optml-bg-lazyloaded during the wait would be sent with null URLs.

Not related to #1204 / #1214: it appears on both the released 4.2.17 build and the #1214 build. Found while testing #1204.

Step-by-step reproduction instructions

  1. Optimole 4.2.17, connected, with Lazy load and background lazy load on.
  2. Create a page whose first block has a background image in an inline style. For example, a Cover block with "Fixed background" on, or a Group block with a background image.
  3. In a private window (logged out), open the page with ?optml_debug=1 appended. Keep the console open.
  4. In the console, look at Sending background selectors:. Or look at the payload of the request to /wp-json/optml/v1/optimizations.
  5. Load the page in a mobile-width window too, so both devices are profiled. Then open it with ?optml_debug=true and read the <!-- plugin=optimole-wp measurement#… --> comment after </html>.

Result: b contains each matched selector with {}, and the stored profile shows "bg":{"…":[]} for both devices. Only the LCP background (lcp.bgUrls) is stored.

Screenshots, screen recording, code snippet or Help Scout ticket

Code links:
https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/assets/js/modules/main.js#L110-L135
https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/assets/js/modules/image-detector.js#L82-L104

Environment info

Optimole 4.2.17 (same code on master), WordPress + Neve, PHP 8.5, Chromium desktop 1440px / mobile 390px

Is the issue you are reporting a regression

No

Activity

  1. added
    bugThis label could be used to identify issues that are caused by a defect in the product.
    on Oct 30, 2026
  2. added theissue type on Oct 30, 2026
  3. girishpanchal30 commented on Nov 16, 2026

    @girishpanchal30
    Contributor

    @rodica-andronache I have fixed it with PR #1240

  4. milicavs commented on Nov 17, 2026

    @milicavs

    🧪 QA steps and test links

    Optimole's page profiler never records which background images sit above the fold. Every background selector is sent as an empty {}, so on pages with several above-the-fold backgrounds, the ones that aren't the LCP element stay lazy-loaded and can appear late. Nothing changes visually on the page itself; the proof is in the profiler's debug output.

    Link Build Bot check
    Reproduce the bug Optimole 4.2.20 (released) ✅ Reproduced: the profiler sent {} for both background selectors, so neither background was recorded
    Verify the fix PR #1240 build 1ad036a ✅ Fixed: "is above the fold" lines for both backgrounds, and both selectors listed both elements with their image URLs

    In the links:

    • Steps 1 to 3 are already done, and the links open on the test page with ?optml_debug=1. Open the browser console (F12) before the page loads, or reload once it is open.
    • Optimole has a stand-in connection (no real account), so images point to a CDN address that doesn't load. The profiler's data is still correct.
    • The profiler normally skips logged-in users; a small must-use plugin turns it on for the admin, so you can test while signed in.
    • Two more Playground helpers: the profiler's request gets the Origin header Playground drops, and Optimole's lazyload script is served from the site.
    • The profile is not saved in the links (the server answers "Invalid background selector" for a signed-in request), so check the console, not the measurement comment.
    • Tools → QA Logs shows debug.log.

    Reproduce on Optimole 4.2.20:

    1. Connect Optimole and turn on Lazy load and lazy loading for background images.
    2. Create a page with a Custom HTML block holding two side-by-side <div>s, each with style="background-image:url(...);height:400px" (above the fold).
    3. In a private window (logged out), open the page with ?optml_debug=1 and the console open.
    4. In the console, expand Sending background selectors: and look for Element with selector "…" is above the fold lines.
    • Bug: each selector shows {}, and no "is above the fold" line appears for the backgrounds.
    • Fixed: an "is above the fold" line for each background, and the selectors list both elements, each with its image URL.
    Edge cases
    • Mobile width (about 390px), same page: both backgrounds are listed for the mobile visit too.
    • After a desktop and a mobile visit (logged out, real site), open the page with ?optml_debug=true: the measurement comment after </html> lists both elements under bg for both devices.
    • A single cover block as the only above-the-fold background: it is listed, and the page looks the same as before.
    • A background far below the fold: it is not listed as above the fold.
    • Background lazy loading turned off: the page loads normally, no profiler errors in the console.

    Fix: #1240 (open)

  5. girishpanchal30 commented on Nov 17, 2026

    @girishpanchal30
    Contributor
  6. girishpanchal30 commented on Nov 18, 2026

    @girishpanchal30
    Contributor
  7. pirate-bot commented on Nov 18, 2026

    @pirate-bot
    Collaborator

    QA agent — 🟢 Passing checks · Above-the-fold backgrounds are now stored and exempted from lazy loading.

    The test site used a stand-in Optimole connection and local image delivery. Desktop and mobile profiles stored both background URLs, and the resulting CSS exempted both images while keeping the lower image lazy-loaded. The base build reproduced the empty profiles, while the candidate also passed the delayed-load and limit checks.

    Worth a human look

    1. Investigate the offscreen unsized placeholder: it saved 300×150 for an 800×500 image on both builds (automated review).
    2. Check whether an LCP entry queued at the observer's 1.5-second timeout is kept (automated review).
    3. Compare a single Cover block's appearance before and after on a connected site (automated QA hint).
    4. Check external callers of get_current_personalized_css() against its nullable return (automated review).
    Checks (7 passed)
    • ✅ On a logged-out desktop visit, the profiler sends both above-the-fold background elements and their URLs under each matching selector, and does not list the below-the-fold background.
    • ✅ On a logged-out mobile-width visit, the profiler sends both above-the-fold background elements and their URLs under each matching selector.
    • ✅ After desktop and mobile profiling, the saved profile contains both background elements and URLs for each device.
    • ✅ After profiling, personalized lazy-load CSS exempts every above-the-fold background rather than only the LCP background, while retaining lazy loading for a below-the-fold background.
    • ✅ A background that acquires the lazy-loaded class during the profiler's wait is sent with its image URL rather than null.
    • ✅ An Optimole image without dimensions that finishes loading during the profiler waits is profiled with its natural dimensions.
    • ✅ At the profiler's background limits, the request is accepted and personalized CSS does not hide backgrounds that may have been omitted by a full watcher or above-fold list.

    Full report, evidence and session trail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugThis label could be used to identify issues that are caused by a defect in the product.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions