performance icon indicating copy to clipboard operation
performance copied to clipboard

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

Open westonruter opened this issue 1 year ago • 4 comments

In https://github.com/WordPress/performance/issues/1058 I proposed adding preconnect links for resources used in embeds which appear in the initial viewport. This logic can be seen here:

https://github.com/WordPress/performance/blob/68887884e3b6783b3a2e6398c2ac07d6b00b6711/plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php#L286-L306

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.

westonruter avatar Oct 31 '25 22:10 westonruter

I think there would be some benefit to using a mix or dn-prefetching and preconnecting.

For YouTube embed, preconnecting to www.youtube.com would speed up the iframe loading, while the other domains could be dns-prefectched.

For Vimeo, the preconnection would be to player.vimeo.com and dns-prefetching for the other domains.

For embeds such as X (formally Twitter) that use JavaScript, the scripts domain would be the target of the preconnect.

peterwilsoncc avatar Nov 17 '25 04:11 peterwilsoncc

Hummm. That's a good point. Resources which are exclusively inside the embed's iframe should get dns-prefetch whereas resources which are eventually used in the page should get preconnect.

westonruter avatar Nov 17 '25 06:11 westonruter

Hi! I’d like to work on this — is anyone assigned?

hrishikesh2810 avatar Jan 11 '26 08:01 hrishikesh2810

@hrishikesh2810 yes, work is underway here: https://github.com/WordPress/performance/pull/2256

westonruter avatar Jan 11 '26 08:01 westonruter

That's a good point. Resources which are exclusively inside the embed's iframe should get dns-prefetch whereas resources which are eventually used in the page should get preconnect.

@peterwilsoncc After thinking about this some more (after much delay) I believe actually the browser's preload scanner will automatically establish the preconnect connection for the any IFRAME.

Cross-referenced with Gemini

The Case for dns-prefetch over preconnect for Static YouTube Iframes

Optimizing the Static Iframe: The Main URL When keeping a static <iframe> in the DOM, adding <link rel="preconnect" href="https://www.youtube.com"> or dns-prefetch for the main src URL provides negligible value.

  • The Preload Scanner: Because the <iframe> is statically present, the browser's preload scanner tokenizes the src attribute during the initial parse of the document. The scanner immediately initiates the DNS/TCP/TLS handshake for www.youtube.com. Placing a resource hint in the <head> for this main URL is redundant and simply adds unnecessary byte weight to the HTML.

**The Subresource Dilemma: preconnect vs dns-prefetch** The true performance bottleneck of a YouTube embed is the waterfall of cross-origin subresources it triggers (like i.ytimg.com for poster images or fonts.googleapis.com for typography). Optimizing these subresources requires navigating modern browser network security.

  • Why preconnect Fails (Network State Partitioning): Modern browsers partition their connection pools using a Network Isolation Key (NIK), a tuple consisting of [top-level-site, frame-site]. If the parent document (example.com) executes a preconnect for i.ytimg.com, the browser opens a socket keyed to [example.com, example.com]. When the YouTube iframe later requests that same resource, it does so with a NIK of [example.com, youtube.com]. Because the frame-site does not match, the browser cannot "see" the warmed-up socket and opens a new one. The parent's preconnect is entirely wasted.
  • Why dns-prefetch Succeeds: dns-prefetch bypasses this browser-level partitioning by leveraging the operating system. When the parent document executes a dns-prefetch, the browser passes the query down to the OS, which stores the resolved IP in the OS-level DNS cache. When the iframe later requests the resource, it misses the browser's partitioned cache but successfully retrieves the IP from the unpartitioned OS cache, saving crucial lookup time.

Conclusion When using static YouTube iframes, the optimal hinting strategy is to strictly use dns-prefetch for known iframe subresources. preconnect should be avoided for cross-origin iframe subresources to prevent exhausting the connection pool with inaccessible sockets.

So I'll go ahead with switching from preconnect to dns-prefetch. We can follow up with switching some of those back to preconnect in the future if we find that there is a performance benefit. The main thing right now is to avoid degrading performance by preconnecting wastefully.

westonruter avatar Feb 27 '26 00:02 westonruter