Skip to content

Embed Optimizer wastefully adds preconnect links for initial-viewport embeds when only dns-prefetch is used #2247

@westonruter

Description

@westonruter

In #1058 I proposed adding preconnect links for resources used in embeds which appear in the initial viewport. This logic can be seen here:

private function add_preconnect_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 ( $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,
),
$group->get_minimum_viewport_width(),
$group->get_maximum_viewport_width()
);
}
}
}

However, because the resources are loaded in an iframe (typically), most of the time the preconnect is wasted because of cross-origin isolation: the iframed third-party embed will have to connect again anyway.

Nevertheless, there is a benefit to preconnecting but only in that it warms the operating system's DNS cache. Since a preconnect also involves a dns-prefetch, this explains why there was some performance benefit measured when the preconnect links were added. But the performance would surely be even better if it had gone with dns-prefetch to begin with.

This was discussed in the Web Performance Slack.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueIssue particularly suitable to be worked on by new contributors[Plugin] Embed OptimizerIssues for the Embed Optimizer plugin (formerly Auto Sizes)[Type] BugAn existing feature is broken

    Type

    Projects

    Status

    Not Started/Backlog 📆

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions