Lazy Loading, issues with CLS and your default placeholder image
-
The default placeholder
src
that is set on lazyloaded images is a base64 encoded image. This image is square and forces the image into a square aspect ratio while the source image is loaded. This causes Cumulative Layout Shift (CLS) on every single image that is not square.CLS is a core “web vital” as set by Google. They have made it into a ranking signal. This means every single one of your users is negatively impacted by using lazy loading via your plugin. Countless sites.
On the latest version of your plugin (3.8.4 at time of writing), in
core/modules/class-lazy.php
line 467 you have the line:Helpers\Parser::add_attribute( $new_image, 'src', 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' );
This forces the square base64 encoded image in as a placeholder. I recommend a couple things from here:
First, your team needs to understand the google web vitals, specifically CLS.
Then the best service you could give your users is to allow the natural width and height on the
img
tag to take over. Anything impacting those dimensions unnaturally will most likely negatively impact CLS, which could impact search engine ranking scores. So if you want to use a base64 image, you need a method of creating a placeholder that is at the very least the same aspect ratio of the source image, but should really be the same dimensions all together. It will need testing to ensure no CLS.However, in my case, I spent a good amount of time tracking this all down, to that one line of code. And for me, no base64 placeholder image makes my site work much more smoothly, and removes CLS for every single image that was lazy loaded, which can compound on pages with lots of image, resulting in a very poor CLS score.
So, I could very easily comment out the line of code, and the problem is “fixed”. But as a potential stop-gap that is backwards-compatible and would help other users, update the line of code with a filter on the placeholder src like so:
Helpers\Parser::add_attribute( $new_image, 'src', apply_filters( 'wp_smush_lazy_load_placeholder_src', 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' ) );
This means in my theme, I can add the following to not use your base64 image:
add_filter('wp_smush_lazy_load_placeholder_src', function($src) { return ''; });
The only enhancement to this filter would be to pass any image info to the filter (mainly width and height) so the user could dynamically determine what placeholder to use, if desired.
And just to note, I’m not the only user noticing this issue, as there are closed topics from a month and a year ago. This is an issue, you need to deal with it, and you need to promote good patterns for your users. I’d imagine most users will not notice and just carry on with CLS all over their site. Please, help those people out, and fix the CLS issues in your base plugin.
– https://www.ads-software.com/support/topic/lazy-loading-leads-to-cls/
– https://www.ads-software.com/support/topic/preventing-lazyload-reflow/
- The topic ‘Lazy Loading, issues with CLS and your default placeholder image’ is closed to new replies.