-
Notifications
You must be signed in to change notification settings - Fork 99
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
Prioritize loading poster image of video LCP elements #1498
Changes from all commits
fadc0fe
a6c9592
138b2b8
03fc900
d6c88a8
4f125de
0f8f82d
d3f44c8
7d289c2
b7e95c8
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Tag visitor that optimizes VIDEO tags: | ||
* - Adds preload links for poster images if in a breakpoint group's LCP. | ||
* | ||
* @package image-prioritizer | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
|
||
// Exit if accessed directly. | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Image Prioritizer: Image_Prioritizer_Video_Tag_Visitor class | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @access private | ||
*/ | ||
final class Image_Prioritizer_Video_Tag_Visitor extends Image_Prioritizer_Tag_Visitor { | ||
|
||
/** | ||
* Visits a tag. | ||
* | ||
* @param OD_Tag_Visitor_Context $context Tag visitor context. | ||
* | ||
* @return bool Whether the tag should be tracked in URL metrics. | ||
*/ | ||
public function __invoke( OD_Tag_Visitor_Context $context ): bool { | ||
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. @westonruter I suppose this is where I would add the logic for #1575? 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. Yes exactly! |
||
$processor = $context->processor; | ||
if ( 'VIDEO' !== $processor->get_tag() ) { | ||
return false; | ||
} | ||
|
||
// Skip empty poster attributes and data: URLs. | ||
$poster = trim( (string) $processor->get_attribute( 'poster' ) ); | ||
if ( '' === $poster || $this->is_data_url( $poster ) ) { | ||
return false; | ||
} | ||
|
||
$xpath = $processor->get_xpath(); | ||
|
||
// TODO: If $context->url_metric_group_collection->get_element_max_intersection_ratio( $xpath ) is 0.0, then the video is not in any initial viewport and the VIDEO tag could get the preload=none attribute added. | ||
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. Thinking out loud for a future enhancement:
Thoughts? Worth creating an issue for it? 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. Oh, interesting idea. Yes, I think that warrants an issue. Related to #1035 which involves 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. Done: #1590 |
||
|
||
// If this element is the LCP (for a breakpoint group), add a preload link for it. | ||
foreach ( $context->url_metric_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) { | ||
$link_attributes = array( | ||
'rel' => 'preload', | ||
'fetchpriority' => 'high', | ||
'as' => 'image', | ||
'href' => $poster, | ||
'media' => 'screen', | ||
); | ||
|
||
$crossorigin = $this->get_attribute_value( $processor, 'crossorigin' ); | ||
if ( null !== $crossorigin ) { | ||
$link_attributes['crossorigin'] = 'use-credentials' === $crossorigin ? 'use-credentials' : 'anonymous'; | ||
} | ||
|
||
$context->link_collection->add_link( | ||
$link_attributes, | ||
$group->get_minimum_viewport_width(), | ||
$group->get_maximum_viewport_width() | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
return array( | ||
'set_up' => 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; | ||
} | ||
); | ||
|
||
foreach ( array_merge( $breakpoint_max_widths, array( 1000 ) ) as $viewport_width ) { | ||
OD_URL_Metrics_Post_Type::store_url_metric( | ||
od_get_url_metrics_slug( od_get_normalized_query_vars() ), | ||
$test_case->get_sample_url_metric( | ||
array( | ||
'viewport_width' => $viewport_width, | ||
'elements' => array( | ||
array( | ||
'isLCP' => true, | ||
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::VIDEO]', | ||
), | ||
), | ||
) | ||
) | ||
); | ||
} | ||
}, | ||
'buffer' => ' | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>...</title> | ||
</head> | ||
<body> | ||
<video class="desktop" poster="https://example.com/poster.jpg" width="1200" height="500" src="https://example.com/header.mp4"></video> | ||
</body> | ||
</html> | ||
', | ||
'expected' => ' | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>...</title> | ||
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/poster.jpg" media="screen"> | ||
</head> | ||
<body> | ||
<video data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[1][self::VIDEO]" class="desktop" poster="https://example.com/poster.jpg" width="1200" height="500" src="https://example.com/header.mp4"></video> | ||
<script type="module">/* import detect ... */</script> | ||
</body> | ||
</html> | ||
', | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
return array( | ||
'set_up' => 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; | ||
} | ||
); | ||
|
||
foreach ( $breakpoint_max_widths as $non_desktop_viewport_width ) { | ||
$elements = array( | ||
array( | ||
'isLCP' => true, | ||
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]', | ||
), | ||
array( | ||
'isLCP' => false, | ||
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::VIDEO]', | ||
), | ||
); | ||
OD_URL_Metrics_Post_Type::store_url_metric( | ||
od_get_url_metrics_slug( od_get_normalized_query_vars() ), | ||
$test_case->get_sample_url_metric( | ||
array( | ||
'viewport_width' => $non_desktop_viewport_width, | ||
'elements' => $elements, | ||
) | ||
) | ||
); | ||
} | ||
|
||
OD_URL_Metrics_Post_Type::store_url_metric( | ||
od_get_url_metrics_slug( od_get_normalized_query_vars() ), | ||
$test_case->get_sample_url_metric( | ||
array( | ||
'viewport_width' => 1000, | ||
'elements' => array( | ||
array( | ||
'isLCP' => false, | ||
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]', | ||
), | ||
array( | ||
'isLCP' => true, | ||
'xpath' => '/*[1][self::HTML]/*[2][self::BODY]/*[2][self::VIDEO]', | ||
), | ||
), | ||
) | ||
) | ||
); | ||
}, | ||
'buffer' => ' | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>...</title> | ||
</head> | ||
<body> | ||
<img class="mobile" src="https://example.com/mobile-header.jpg" alt="Mobile Logo" width="800" height="600" crossorigin> | ||
<video class="desktop" poster="https://example.com/poster.jpg" width="1200" height="500" crossorigin> | ||
<source src="https://example.com/header.webm" type="video/webm"> | ||
<source src="https://example.com/header.mp4" type="video/mp4"> | ||
</video> | ||
</body> | ||
</html> | ||
', | ||
'expected' => ' | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>...</title> | ||
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/mobile-header.jpg" crossorigin="anonymous" media="screen and (max-width: 782px)"> | ||
<link data-od-added-tag rel="preload" fetchpriority="high" as="image" href="https://example.com/poster.jpg" media="screen and (min-width: 783px)" crossorigin="anonymous"> | ||
</head> | ||
<body> | ||
<img data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[1][self::IMG]" class="mobile" src="https://example.com/mobile-header.jpg" alt="Mobile Logo" width="800" height="600" crossorigin> | ||
<video data-od-xpath="/*[1][self::HTML]/*[2][self::BODY]/*[2][self::VIDEO]" class="desktop" poster="https://example.com/poster.jpg" width="1200" height="500" crossorigin> | ||
<source src="https://example.com/header.webm" type="video/webm"> | ||
<source src="https://example.com/header.mp4" type="video/mp4"> | ||
</video> | ||
<script type="module">/* import detect ... */</script> | ||
</body> | ||
</html> | ||
', | ||
); |
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.
I'd like to move this eventually into
OD_HTML_Tag_Processor
/OD_HTML_Processor
. Also @dmsnell has suggested this could eventually be part of the core class as well, as most HTML attributes have their own dedicated logic to get back a normalized value.