Skip to content

WebP module: Safari 404s on srcset images, and no image gets a srcset #88

Open

Description

@RichKenyon

Summary

inc/performance/webp-uploads.php on main (8b47ae7) has four problems that show up as missing images in Safari and as missing responsive images everywhere. There is also a related bug in inc/performance/dominant-color-images.php.

  1. Safari requests srcset URLs with no file extension and gets 404s. The client-side replaceImagesWithWebP() script rewrites srcset with /([^\s,]+)\.(jpg|jpeg|png)(\?[^\s,]*)?(\s+\d+w)?/gi.
    • Group 1 is the URL without its extension. The callback then runs url.replace(/\.(jpg|jpeg|png)(\?|$)/i, '.webp$2') on it, which matches nothing.
    • So image-768x511.jpg 768w comes back as image-768x511 768w.
    • The image first renders from src. When the browser picks a srcset candidate, it disappears.
  2. Only Safari reaches that script. browser_supports_webp() checks the navigation request's Accept header for image/webp.
    • Safari decodes WebP but sends text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 on navigations. It gets .jpg markup, which the script then rewrites.
    • Chrome gets .webp markup, so the script has nothing to touch.
    • The same URL also serves different HTML per browser with no Vary: Accept, which a page cache can mix up.
  3. No srcset at all when WebP markup is served.
    • replace_attachment_image_src_with_webp() is hooked to wp_get_attachment_image_src, so the src is already .webp when core calculates the srcset.
    • Core can't match a .webp src to a registered size, so it returns no srcset.
    • Every wp_get_attachment_image() image loses its responsive candidates, so phones download the full size.
  4. The swap script is printed three times (wp_head, wp_print_scripts, wp_footer). It also swaps to .webp without knowing whether the file exists.

Related, dominant-color-images.php: the fade-in script is added with wp_add_inline_script( 'wp-block-library', … ). wp-block-library is a stylesheet handle, so the script never prints, and images with loading="lazy" stay at opacity: 0.8.

Steps to reproduce

  1. Upload a JPEG large enough to get intermediate sizes, and output it with wp_get_attachment_image().
  2. View the page in Safari on macOS (seen on 26.6).
    • The Web Inspector console shows Failed to load resource: the server responded with a status of 404 () (image-name, line 0).
    • The <img>'s srcset lists URLs without extensions.
  3. View the same page in Chrome: the <img> has a .webp src and no srcset.

Proposed fix (PR to follow)

  • One markup for every browser: serve WebP to every browser through a wdsbt_serve_webp filter, default true. Every current browser decodes WebP, Safari since 14.
  • Remove the client-side swap script.
  • Keep the srcset: stop swapping the src in wp_get_attachment_image_src. Swap it in wp_get_attachment_image_attributes, and swap each candidate in wp_calculate_image_srcset (already present), after core has built them.
  • Check files exist: in the content and output-buffer passes, swap only to a .webp that exists.
  • Scheme: keep generated and stored URLs in the request's scheme (set_url_scheme), so an https page doesn't get http:// images when the site URL is http.
  • Dominant color: attach the fade-in script to wdsbt-scripts, and handle it running after DOMContentLoaded.

If you'd rather keep content negotiation, the alternative is to keep the Accept check and send Vary: Accept, and still remove the swap script and the wp_get_attachment_image_src hook.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions