-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix handling IMG tags with JS-based lazy-loading #1785
base: trunk
Are you sure you want to change the base?
Conversation
return false; | ||
} | ||
|
||
// Abort if the srcset is a data: URL since there is nothing to optimize. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, there could be something to optimize. We can't preload the URL, but we could still add fetchpriority=high
to the IMG
.
@@ -53,13 +97,21 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { | |||
* @return bool Whether the tag should be tracked in URL Metrics. | |||
*/ | |||
private function process_img( OD_HTML_Tag_Processor $processor, OD_Tag_Visitor_Context $context ): bool { | |||
$src = $this->get_valid_src( $processor ); | |||
if ( null === $src ) { | |||
if ( ! $this->is_img_with_valid_src_and_srcset( $processor ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted below, this may be too conservative. We could still process this IMG
. It's just we would skip any parts related to preloading the src
or srcset
.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #1785 +/- ##
==========================================
+ Coverage 57.43% 57.52% +0.08%
==========================================
Files 84 84
Lines 6508 6519 +11
==========================================
+ Hits 3738 3750 +12
+ Misses 2770 2769 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
On quite a few sites I found JS-based lazy-loading causing problems with Image Prioritizer. In particular, the Avada theme's Fusion Images lazy-loading functionality replaces
srcset
withdata-srcset
but it doesn't change thesrc
, for example:This was resulting in an erroneous preload link being added, in particular the
imagesrcset
is pointing to adata:
URL:So this PR adds checks to make sure that if an
IMG
has such asrcset
it is also excluded from processing.In a future PR we could consider undoing such JS-based lazy-loading, rewriting
data-src
tosrc
,data-srcset
back tosrcset
and so on, and then to process theIMG
as normal. This would have a dramatic improvement to LCP especially when the such JS-based lazy-loading is being done for images in the initial viewport, especially for anIMG
which is the LCP element. See #1746.