-
Notifications
You must be signed in to change notification settings - Fork 113
Fix handling IMG tags with JS-based lazy-loading #1785
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
5b4cbc0
e046d70
2aa7235
524796e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,14 @@ | |
*/ | ||
final class Image_Prioritizer_Img_Tag_Visitor extends Image_Prioritizer_Tag_Visitor { | ||
|
||
/** | ||
* List of PICTURE XPaths to skip processing of child IMG tags. | ||
* | ||
* @since n.e.x.t | ||
* @var string[] | ||
*/ | ||
private $picture_ancestor_xpaths_to_skip = array(); | ||
|
||
/** | ||
* Visits a tag. | ||
* | ||
|
@@ -36,14 +44,50 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { | |
$tag = $processor->get_tag(); | ||
|
||
if ( 'PICTURE' === $tag ) { | ||
return $this->process_picture( $processor, $context ); | ||
$picture_xpath = $processor->get_xpath(); | ||
if ( false === $this->process_picture( $processor, $context ) ) { | ||
$this->picture_ancestor_xpaths_to_skip[] = $picture_xpath; | ||
} | ||
return false; // Because the IMG child is what gets tracked in URL Metrics. | ||
} elseif ( 'IMG' === $tag ) { | ||
return $this->process_img( $processor, $context ); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Determines whether the current IMG is valid for tracking in URL Metrics. | ||
* | ||
* An IMG must have a src attribute which is not a data: URL. And if it has a srcset attribute, it also must not be | ||
* a data: URL. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param OD_HTML_Tag_Processor $processor Tag Processor. | ||
* @return bool Whether valid for tracking in URL Metrics. | ||
*/ | ||
private function is_img_with_valid_src_and_srcset( OD_HTML_Tag_Processor $processor ): bool { | ||
$src = $this->get_attribute_value( $processor, 'src' ); | ||
$has_src = ( is_string( $src ) && '' !== $src ); | ||
if ( ! $has_src ) { | ||
return false; | ||
} | ||
|
||
$srcset = $this->get_attribute_value( $processor, 'srcset' ); | ||
$has_srcset = ( is_string( $srcset ) && '' !== $srcset ); | ||
|
||
// Abort data: URLs (which may very be JS-based lazy-loading). | ||
if ( $this->is_data_url( $src ) ) { | ||
return false; | ||
} | ||
if ( $has_srcset && $this->is_data_url( $srcset ) ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Process an IMG element. | ||
* | ||
|
@@ -54,13 +98,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 ) ) { | ||
return false; | ||
} | ||
|
||
$xpath = $processor->get_xpath(); | ||
|
||
// If the PICTURE's processing was aborted, then abort processing its child IMG as well. | ||
if ( 'PICTURE' === $this->get_parent_tag_name( $context ) ) { | ||
foreach ( $this->picture_ancestor_xpaths_to_skip as $picture_xpath ) { | ||
if ( str_starts_with( $xpath, $picture_xpath ) ) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
$current_fetchpriority = $this->get_attribute_value( $processor, 'fetchpriority' ); | ||
$is_lazy_loaded = 'lazy' === $this->get_attribute_value( $processor, 'loading' ); | ||
$updated_fetchpriority = null; | ||
|
@@ -188,7 +240,7 @@ private function process_img( OD_HTML_Tag_Processor $processor, OD_Tag_Visitor_C | |
* | ||
* @param OD_HTML_Tag_Processor $processor HTML tag processor. | ||
* @param OD_Tag_Visitor_Context $context Tag visitor context. | ||
* @return bool Whether the tag should be tracked in URL Metrics. | ||
* @return bool Whether the PICTURE was processed. | ||
*/ | ||
private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visitor_Context $context ): bool { | ||
/** | ||
|
@@ -219,8 +271,13 @@ private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visit | |
} | ||
|
||
// Abort processing if a SOURCE lacks the required srcset attribute. | ||
$srcset = $this->get_valid_src( $processor, 'srcset' ); | ||
if ( null === $srcset ) { | ||
$srcset = $this->get_attribute_value( $processor, 'srcset' ); | ||
if ( ! is_string( $srcset ) ) { | ||
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 commentThe 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 |
||
if ( $this->is_data_url( $srcset ) ) { | ||
return false; | ||
} | ||
|
||
|
@@ -243,8 +300,8 @@ private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visit | |
|
||
// Process the IMG element within the PICTURE. | ||
if ( 'IMG' === $tag && ! $processor->is_tag_closer() ) { | ||
$src = $this->get_valid_src( $processor ); | ||
if ( null === $src ) { | ||
// Abort if process_img() won't later be processing this IMG. | ||
if ( ! $this->is_img_with_valid_src_and_srcset( $processor ) ) { | ||
return false; | ||
} | ||
|
||
|
@@ -275,31 +332,7 @@ private function process_picture( OD_HTML_Tag_Processor $processor, OD_Tag_Visit | |
) | ||
); | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Gets valid src attribute value for preloading. | ||
* | ||
* Returns null if the src attribute is not a string (i.e. src was used as a boolean attribute was used), if it | ||
* it has an empty string value after trimming, or if it is a data: URL. | ||
* | ||
* @since 0.3.0 | ||
* | ||
* @param OD_HTML_Tag_Processor $processor Processor. | ||
* @param 'src'|'srcset' $attribute_name Attribute name. | ||
* @return non-empty-string|null URL which is not a data: URL. | ||
*/ | ||
private function get_valid_src( OD_HTML_Tag_Processor $processor, string $attribute_name = 'src' ): ?string { | ||
$src = $processor->get_attribute( $attribute_name ); | ||
if ( ! is_string( $src ) ) { | ||
return null; | ||
} | ||
$src = trim( $src ); | ||
if ( '' === $src || $this->is_data_url( $src ) ) { | ||
return null; | ||
} | ||
return $src; | ||
return true; | ||
} | ||
|
||
/** | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,2 @@ | ||
<?php | ||
return static function ( Test_Image_Prioritizer_Helper $test_case ): void { | ||
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() ); | ||
|
||
// Populate one URL Metric so that none of the IMG elements are unknown. | ||
OD_URL_Metrics_Post_Type::store_url_metric( | ||
$slug, | ||
$test_case->get_sample_url_metric( | ||
array( | ||
'viewport_width' => 1000, | ||
'elements' => array( | ||
array( | ||
'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::IMG]', | ||
'isLCP' => true, | ||
), | ||
), | ||
) | ||
) | ||
); | ||
}; | ||
return static function ( Test_Image_Prioritizer_Helper $test_case ): void {}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,2 @@ | ||
<?php | ||
return static function ( Test_Image_Prioritizer_Helper $test_case ): void { | ||
$breakpoint_max_widths = array( 480, 600, 782 ); | ||
|
||
add_filter( | ||
'od_breakpoint_max_widths', | ||
static function () use ( $breakpoint_max_widths ) { | ||
return $breakpoint_max_widths; | ||
} | ||
); | ||
|
||
$test_case->populate_url_metrics( | ||
array( | ||
array( | ||
'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::PICTURE]/*[2][self::IMG]', | ||
'isLCP' => true, | ||
), | ||
) | ||
); | ||
}; | ||
return static function ( Test_Image_Prioritizer_Helper $test_case ): void {}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,2 @@ | ||
<?php | ||
return static function ( Test_Image_Prioritizer_Helper $test_case ): void { | ||
$breakpoint_max_widths = array( 480, 600, 782 ); | ||
|
||
add_filter( | ||
'od_breakpoint_max_widths', | ||
static function () use ( $breakpoint_max_widths ) { | ||
return $breakpoint_max_widths; | ||
} | ||
); | ||
|
||
$test_case->populate_url_metrics( | ||
array( | ||
array( | ||
'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::PICTURE]/*[2][self::IMG]', | ||
'isLCP' => true, | ||
), | ||
) | ||
); | ||
}; | ||
return static function ( Test_Image_Prioritizer_Helper $test_case ): void {}; |
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 thesrc
orsrcset
.