Skip to content

Missing-srcset enhancement rewrites data-*-srcset attributes and breaks Envira lazy-load placeholders #1184

Closed

Description

@selul

Status (2026-10-16)

Fixed in PR #1185 (bugfix/1184 → development). Pirate-bot approved f38d4978, and all checks are green.


Summary

When the page profiler has missing-srcset data for an image, Optml_Tag_Replacer::add_missing_srcset_attributes() rewrites the wrong attributes. Its srcset= / sizes= / src= regexes are not anchored to an attribute name, so they also match data-*-srcset, data-*-sizes and data-*-src attributes that lazy-load and gallery plugins use.

On Envira Gallery images this:

  1. reads the existing value from data-envira-srcset (the first match) instead of srcset;
  2. writes the merged value into every match, so Envira's placeholder srcset="data:image/gif;base64,…" is replaced with real CDN candidates;
  3. inserts sizes after both the real and the data-envira-srcset attribute, so the tag ends up with two sizes attributes;
  4. mixes x and w descriptors in one srcset (… 2x, … 254w, … 400w), which is invalid.

Because the placeholder srcset is gone, the browser downloads the image immediately instead of leaving it to Envira's lazy loader. In Firefox that load finishes before Envira's justifiedGallery init, and the init then resets src to data-safe-src (the GIF placeholder). The image still shows (from srcset), but the markup no longer matches what the gallery script expects, and lazy loading is defeated for that image.

Steps to reproduce

  1. Envira Gallery Lite gallery on a page, Optimole connected, lazy load in viewport mode.
  2. Visit the page so optimizer.js stores a missing-srcset measurement for one gallery image (the profile in Optml_Manager::instance()->page_profiler).
  3. Reload and look at that image's tag.

Live example: http://testing.optimole.com/envira-test-page/, gallery 92, attachment 18 (#envira-gallery-wrap-92 #envira-gallery-image-18).

Expected: srcset keeps Envira's data: placeholder, data-envira-srcset is not touched, one sizes attribute.

Actual:

<img … class="envira-gallery-image envira-gallery-image-4 envira-normal envira-lazy"
  src="https://….i.optimole.com/…/w:254/h:300/…/marina-mazur-…-scaled.jpg"
  data-envira-srcset="… 2x, … 254w, … 400w" sizes="(max-width: 2560px) 254px"
  srcset="… 2x, … 254w, … 400w" sizes="(max-width: 2560px) 254px"
  data-safe-src="data:image/gif;base64,…" />

The other images in the same gallery (no profiler data) keep srcset="data:image/gif;base64,…".

Impact

  • The E2E check tests/e2e/envira.spec.ts › Gallery images all attributes replaced properly has failed on Firefox on 7 of the last 8 development runs since 2026-10-12, including the release PR Release #1169. It started once the shared QA site's profile recorded missing-srcset data for attachment 18. Chromium passes only because its script timing is different.
  • Any plugin that keeps the real source in a data-*-srcset / data-*-src attribute (Envira, other galleries and lazy loaders) can get duplicated attributes or a placeholder srcset replaced with eager candidates.

Root cause

inc/tag_replacer.php:

  • add_missing_srcset_attributes(): strpos( $tag, 'srcset=' ) / strpos( $tag, 'sizes=' ) presence checks; srcset insert after /(src=["'][^"']*["'])/i; sizes insert after /(srcset=["'][^"']*["'])/i; existing-sizes lookup /sizes=…/.
  • enhance_existing_srcset() / enhance_existing_sizes(): preg_match on /srcset=…/ and /sizes=…/, then preg_replace with no limit.

The code was introduced in 4.1.0 (8ab74741).

Proposed fix

  • Match only the real src / srcset / sizes attributes (attribute name preceded by whitespace) and replace a single occurrence.
  • When the real srcset (or src) is a data: placeholder, another script owns loading the image: leave the tag unchanged instead of adding eager candidates.
  • PHPUnit cases for an Envira-style tag.

Activity

  1. self-assigned this
    on Oct 16, 2026
  2. added theissue type on Oct 16, 2026
  3. selul commented on Oct 16, 2026

    @selul
    ContributorAuthor
  4. selul commented on Oct 16, 2026

    @selul
    ContributorAuthor
  5. pirate-bot commented on Oct 16, 2026

    @pirate-bot
    Collaborator

    QA agent — 🔴 Has problems · Envira placeholders are preserved, but a data-src edge still misses srcset and sizes.

    With a seeded Optimole account and local image delivery, the four-image Envira gallery keeps its placeholders and loads after scrolling in Chromium and Firefox. The original placeholder rewrite and duplicate sizes reproduce on the base build, not the candidate. A separate real-src image with a data: value in data-src still receives no srcset or sizes; that also fails on base. All 62 srcset tests pass, but their direct-method checks bypass this full-parser gap.

    Problems found

    • ❌ For ordinary non-placeholder images with profiler measurements, the image's own srcset and sizes are enhanced once without rewriting data- attributes, attribute-like text, or browser attribute boundaries.*
      The representative normal image passes and 26 of 27 HTTP edge cases pass, including quote/alt/title boundaries, spaced/unquoted attributes, duplicates, noscript, cut-off values, malformed markup and JSON escaping. The added srcset PHPUnit class passes: 62 tests, 156 assertions; PHP 7.4 lint passes. One end-to-end failure remains: when data-src contains a data: GIF before a real own src, no own src
      🐞 Existing bug: also fails on the base build. Not reported yet.

      For ordinary non-placeholder images with profiler measurements, the image's own

    Worth a human look

    1. Check real Optimole account connection and CDN image delivery on a public site.
    2. Try other live gallery/lazy-load plugins and customer page-builder output; this run used Envira Lite and synthetic markup cases.
    Checks (2 passed)
    • ✅ After reloading an Envira Lite gallery with stored missing-srcset measurements, the server-rendered images keep Envira's data: placeholder srcset and lazy-loader data attributes, without adding duplicate sizes attributes.
    • ✅ After scrolling the prepared Envira gallery, all four images display using optimized source URLs, while Envira retains ownership of lazy loading and no data-opt-src is added.

    Full report, evidence and session trail

  6. added a commit that references this issue on Oct 16, 2026
    0953cde
  7. pirate-bot commented on Oct 19, 2026

    @pirate-bot
    Collaborator

    🎉 This issue has been resolved in version 4.2.17 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

  8. added
    releasedIndicate that an issue has been resolved and released in a particular version of the product.
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3rd-part-compatibilitiesIssue or feature related to a compatibility with a 3rd party product.bug-reportreleasedIndicate that an issue has been resolved and released in a particular version of the product.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions