-
Notifications
You must be signed in to change notification settings - Fork 109
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
Incorporate media queries into preconnect links to account for whether embeds are in viewport #1654
Changes from 6 commits
9a3c838
a89a2b4
6fba611
622901a
c89ba87
85d4fd4
192d382
aa06707
c22701f
5ecfcf2
9e0da37
096497f
9169bca
415846d
dba4073
2c799ad
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -81,6 +81,7 @@ private function is_embed_wrapper( OD_HTML_Tag_Processor $processor ): bool { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Otherwise, if the embed is not in any initial viewport, it will add lazy-loading logic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @since 0.2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @since n.e.x.t Adds preconnect links for each viewport group and skips if the element is not in the viewport for that group. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param OD_Tag_Visitor_Context $context Tag visitor context. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @return bool Whether the tag should be tracked in URL Metrics. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -109,7 +110,8 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
&& | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$context->url_metric_group_collection->get_last_group()->count() > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$max_intersection_ratio = $context->url_metric_group_collection->get_element_max_intersection_ratio( self::get_embed_wrapper_xpath( $processor->get_xpath() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$embed_wrapper_xpath = self::get_embed_wrapper_xpath( $processor->get_xpath() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$max_intersection_ratio = $context->url_metric_group_collection->get_element_max_intersection_ratio( $embed_wrapper_xpath ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ( $max_intersection_ratio > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* The following embeds have been chosen for optimization due to their relative popularity among all embed types. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -171,12 +173,20 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach ( $preconnect_hrefs as $preconnect_href ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$context->link_collection->add_link( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
array( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'rel' => 'preconnect', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'href' => $preconnect_href, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach ( $context->url_metric_group_collection as $group ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ( $group->get_element_max_intersection_ratio( $embed_wrapper_xpath ) <= 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$context->link_collection->add_link( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
array( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'rel' => 'preconnect', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'href' => $preconnect_href, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$group->get_minimum_viewport_width(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$group->get_maximum_viewport_width() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. The analogous logic that adds preload links for the LCP image: performance/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php Lines 146 to 180 in df29967
For that we use |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} elseif ( embed_optimizer_update_markup( $processor, false ) && ! $this->added_lazy_script ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$processor->append_body_html( wp_get_inline_script_tag( embed_optimizer_get_lazy_load_script(), array( 'type' => 'module' ) ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,18 +130,29 @@ function ( array $links ): array { | |
*/ | ||
private function merge_consecutive_links( array $links ): array { | ||
|
||
// Ensure links are sorted by the minimum_viewport_width. | ||
usort( | ||
$links, | ||
/** | ||
* Comparator. | ||
* | ||
* The links are sorted first by the 'href' attribute to group identical URLs together. | ||
* If the 'href' attributes are the same, the links are then sorted by 'minimum_viewport_width'. | ||
* | ||
* @param Link $a First link. | ||
* @param Link $b Second link. | ||
* @return int Comparison result. | ||
*/ | ||
static function ( array $a, array $b ): int { | ||
return $a['minimum_viewport_width'] <=> $b['minimum_viewport_width']; | ||
// Get href values, defaulting to empty string if not present. | ||
$href_a = $a['attributes']['href'] ?? ''; | ||
$href_b = $b['attributes']['href'] ?? ''; | ||
|
||
$href_comparison = strcmp( $href_a, $href_b ); | ||
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. This would be the same as: $href_comparison = ( $href_a <=> $href_b ); Right? 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. I believe in this case it would be the same. |
||
if ( 0 === $href_comparison ) { | ||
return $a['minimum_viewport_width'] <=> $b['minimum_viewport_width']; | ||
} | ||
|
||
return $href_comparison; | ||
} | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ function od_initialize_extensions(): void { | |
*/ | ||
function od_generate_media_query( ?int $minimum_viewport_width, ?int $maximum_viewport_width ): ?string { | ||
if ( is_int( $minimum_viewport_width ) && is_int( $maximum_viewport_width ) && $minimum_viewport_width > $maximum_viewport_width ) { | ||
_doing_it_wrong( __FUNCTION__, esc_html__( 'The minimum width must be greater than the maximum width.', 'optimization-detective' ), 'Optimization Detective 0.7.0' ); | ||
_doing_it_wrong( __FUNCTION__, esc_html__( 'The minimum width cannot be greater than the maximum width.', 'optimization-detective' ), 'Optimization Detective 0.7.0' ); | ||
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. Right 😅 |
||
return null; | ||
} | ||
$media_attributes = array(); | ||
|
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.
Can the intersection ratio ever be less than 0? I don't think so, and Gemini confirms. Since we're comparing floats, it's probably not safe to check if the return value is exactly equal to 0. How about:
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.
You are right. Changed it in 5ecfcf2.