Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
}

$this->reduce_layout_shifts( $context );
$this->add_preconnect_links( $context );
$this->add_dns_prefetch_links( $context );
$this->lazy_load_embeds( $context );

/*
Expand Down Expand Up @@ -207,24 +207,24 @@ private function reduce_layout_shifts( OD_Tag_Visitor_Context $context ): void {
}

/**
* Gets preconnect URLs based on embed type.
* Gets dns-prefech URLs based on embed type.
*
* The following embeds have been chosen for optimization due to their relative popularity among all embed types.
* The list of hosts being preconnected to was obtained by inserting an embed into a post and then looking
* The list of hosts being dns-prefetched to was obtained by inserting an embed into a post and then looking
* at the network log on the frontend as the embed renders. Each should include the host of the iframe src
* as well as URLs for assets used by the embed, _if_ the URL looks like it is not geotargeted (e.g. '-us')
* or load-balanced (e.g. 's0.example.com'). For the load balancing case, attempt to load the asset by
* incrementing the number appearing in the subdomain (e.g. s1.example.com). If the asset still loads, then
* it is a likely case of a load balancing domain name which cannot be safely preconnected since it could
* it is a likely case of a load balancing domain name which cannot be safely dns-prefetched since it could
* not end up being the load balanced domain used for the embed. Lastly, these domains are only for the URLs
* for GET requests, as POST requests are not likely to be part of the critical rendering path.
*
* @since 0.4.1
*
* @param OD_HTML_Tag_Processor $processor Processor, with the cursor currently at an embed block.
* @return array<non-empty-string> Array of URLs to preconnect to.
* @return array<non-empty-string> Array of URLs to dns-prefetch.
*/
private function get_preconnect_urls( OD_HTML_Tag_Processor $processor ): array {
private function get_dns_prefetch_urls( OD_HTML_Tag_Processor $processor ): array {
$urls = array();
$has_class = static function ( string $wanted_class ) use ( $processor ): bool {
return true === $processor->has_class( $wanted_class );
Expand Down Expand Up @@ -277,26 +277,26 @@ private function get_preconnect_urls( OD_HTML_Tag_Processor $processor ): array
}

/**
* Adds preconnect links for embed resources.
* Adds dns-prefetch links for embed resources.
*
* @since 0.4.1
*
* @param OD_Tag_Visitor_Context $context Tag visitor context, with the cursor currently at an embed block.
*/
private function add_preconnect_links( OD_Tag_Visitor_Context $context ): void {
private function add_dns_prefetch_links( OD_Tag_Visitor_Context $context ): void {
$processor = $context->processor;
$embed_wrapper_xpath = self::get_embed_wrapper_xpath( $processor->get_xpath() );

foreach ( $this->get_preconnect_urls( $processor ) as $preconnect_url ) {
foreach ( $this->get_dns_prefetch_urls( $processor ) as $dns_prefetch_url ) {
foreach ( $context->url_metric_group_collection as $group ) {
if ( $group->get_element_max_intersection_ratio( $embed_wrapper_xpath ) < PHP_FLOAT_EPSILON ) {
continue;
}

$context->link_collection->add_link(
array(
'rel' => 'preconnect',
'href' => $preconnect_url,
'rel' => 'dns-prefetch',
'href' => $dns_prefetch_url,
),
$group->get_minimum_viewport_width(),
$group->get_maximum_viewport_width()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Expand Up @@ -22,7 +22,7 @@
* }
*
* @phpstan-type LinkAttributes array{
* rel: 'preload'|'modulepreload'|'preconnect',
* rel: 'preload'|'modulepreload'|'preconnect'|'dns-prefetch',
* href?: non-empty-string,
* imagesrcset?: non-empty-string,
* imagesizes?: non-empty-string,
Expand Down