keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

converting multiple image paths to WebP edge case #115

Closed coreykn closed 4 years ago

coreykn commented 4 years ago

If a matched attribute has more than one image, such as srcset, and one or more images are not comma and space separated the $sizes array in Cache_Enabler_Disk::_covert_webp_srcset will result in an array item having more than one image:

<img src="https://www.example.com/wp-content/uploads/test.jpg" srcset="https://www.example.com/wp-content/uploads/test-300x169.jpg 300w,https://www.example.com/wp-content/uploads/test-1024x576.jpg 1024w,https://www.example.com/wp-content/uploads/test-768x432.jpg 768w,https://www.example.com/wp-content/uploads/test-1200x675.jpg 1200w,https://www.example.com/wp-content/uploads/test.jpg 1280w" sizes="(max-width: 300px) 100vw, 300px">

$sizes[0] === 'https://www.example.com/wp-content/uploads/test-300x169.jpg 300w,https://www.example.com/wp-content/uploads/test-1024x576.jpg 1024w,https://www.example.com/wp-content/uploads/test-768x432.jpg 768w,https://www.example.com/wp-content/uploads/test-1200x675.jpg 1200w,https://www.example.com/wp-content/uploads/test.jpg 1280w';

$src_webp === 'https://www.example.com/wp-content/uploads/test-300x169.webp 300w,https://www.example.com/wp-content/uploads/test-1024x576.webp 1024w,https://www.example.com/wp-content/uploads/test-768x432.webp 768w,https://www.example.com/wp-content/uploads/test-1200x675.webp 1200w,https://www.example.com/wp-content/uploads/test.webp 1280w';

The extensions are still replaced or appended, however, only the first image is checked to see if it exists. In the example above that would mean only https://www.example.com/wp-content/uploads/test-300x169.webp is being checked, if it exists then the entire string is returned converted. In this case it means not all images are being checked to see if they exist, which could result in an image path being converted even if the WebP version doesn't exist.