Repository navigation
Open
Description
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.
- Safari requests srcset URLs with no file extension and gets 404s. The client-side
replaceImagesWithWebP()script rewritessrcsetwith/([^\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 768wcomes back asimage-768x511 768w. - The image first renders from
src. When the browser picks a srcset candidate, it disappears.
- Group 1 is the URL without its extension. The callback then runs
- Only Safari reaches that script.
browser_supports_webp()checks the navigation request'sAcceptheader forimage/webp.- Safari decodes WebP but sends
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8on navigations. It gets.jpgmarkup, which the script then rewrites. - Chrome gets
.webpmarkup, 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.
- Safari decodes WebP but sends
- No srcset at all when WebP markup is served.
replace_attachment_image_src_with_webp()is hooked towp_get_attachment_image_src, so the src is already.webpwhen core calculates the srcset.- Core can't match a
.webpsrc 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.
- The swap script is printed three times (
wp_head,wp_print_scripts,wp_footer). It also swaps to.webpwithout 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
- Upload a JPEG large enough to get intermediate sizes, and output it with
wp_get_attachment_image(). - 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>'ssrcsetlists URLs without extensions.
- The Web Inspector console shows
- View the same page in Chrome: the
<img>has a.webpsrcand nosrcset.
Proposed fix (PR to follow)
- One markup for every browser: serve WebP to every browser through a
wdsbt_serve_webpfilter, defaulttrue. 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 inwp_get_attachment_image_attributes, and swap each candidate inwp_calculate_image_srcset(already present), after core has built them. - Check files exist: in the content and output-buffer passes, swap only to a
.webpthat 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 afterDOMContentLoaded.
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.
Metadata
Metadata
Assignees
Labels
No labels
Activity