When should you use preconnect or dns-prefetch?

ProcrustesPR

Consider preconnect first when the current page is about to fetch critical resources from a cross-origin server. If a domain might be needed, or you want to avoid establishing too many connections at once, consider the lower-cost dns-prefetch instead. The two hints do different work in advance: preconnect handles DNS lookup, connection establishment, and TLS negotiation; dns-prefetch only looks up the IP address associated with a domain, without establishing a connection or downloading resources.

ProcrustesPRProcrustesPRHow should you balance CNAME convenience and performance?

preconnect: reserve it for critical origins you will need soon

According to web.dev, preconnect is suitable for cross-origin servers that provide critical resources for the current page, such as a CDN hosting important images or other resources. It prepares the connection before the browser discovers the resources, reducing the wait when it starts fetching them.

When choosing origins, consider both how soon they will be needed and how important they are to the current page, rather than adding every external domain you see:

  • Prioritize origins that provide critical cross-origin resources and will be needed soon.
  • If a page includes many cross-origin resources, select the most critical origins rather than establishing connections to every origin in advance.

This hint prepares a connection; it does not instruct the browser to download a particular file immediately. Adding the hint does not guarantee that the whole page will load faster.

dns-prefetch: look up the address first for less certain needs

dns-prefetch performs a DNS lookup in advance, resolving a domain name to an IP address. DNS caches on devices and across the network usually speed up lookups, but queries can still take time. A DNS lookup alone costs less than establishing a full connection in advance.

One scenario web.dev describes is a page with links to other websites that users might click. In this case, consider dns-prefetch without establishing connections to those sites. This lower-cost option is also worth considering when there are many cross-origin servers and you are concerned about overusing preconnect. However, it only saves time on the DNS lookup; it does not mean a connection has been established.

Third-party fonts: choose the origins and check the CORS requirements

Third-party fonts are one use case for preconnect. The page must connect to the font provider’s origin before it can download the fonts it needs. The time spent establishing that connection can delay font discovery and downloading. Connecting in advance can reduce this wait.

For example, Google Fonts serves style declarations from fonts.googleapis.com and font files from fonts.gstatic.com, so the two origins need to be considered separately. Font files use CORS, so the corresponding preconnect hint needs the crossorigin attribute. If it is omitted, the browser will open another connection when downloading the fonts rather than reuse the pre-established connection. Check whether the resource uses CORS rather than adding this attribute to every cross-origin hint.

ProcrustesPRProcrustesPRShould you use a 302 redirect to a temporary alias for the new server during a DNS update?

preconnect and dns-prefetch: perspectives from across the web

How should preload and preconnect be used together?

The two target different things: preload is useful for telling the browser to prioritize fetching a specific resource, while preconnect prepares a cross-origin connection that will soon be needed. For example, if you know which font file is critical, consider preloading that file. If it comes from another origin, also assess whether an early connection is needed. Both should be used selectively for resources or origins that genuinely matter.

Do third-party fonts always require preconnect?

Not necessarily. Another approach is to self-host fonts, eliminating the need to connect to a third-party font provider. web.dev notes that self-hosting is faster than downloading from another origin in most cases. However, when adopting this approach, check whether the site uses a CDN, HTTP/2, or HTTP/3, and whether the fonts have the correct cache headers.

Can dns-prefetch be added automatically when there are many external links?

Yes, automation tools can add these hints, but whether to add them should depend on how the links are used. One example from web.dev is dnstradamus, which uses JavaScript and Intersection Observer to add dns-prefetch hints to the current page when external links enter the visible area as the user scrolls. This is one approach to handling external links; it does not mean every link needs dns-prefetch.

ProcrustesPRHow does HSTS protect websites and subdomains?

References

  1. Optimize web fontsweb.dev — Discusses: Do third-party fonts always require preconnect?
  2. Assist the browser with resource hintsweb.dev — Discusses: How should preload and preconnect be used together?; Can dns-prefetch be added automatically when there are many external links?
  3. 加速 Google Web Vitals 重要指標 LCP 的五種方法tenten.co · 2021-08-18T21:46:13.000Z — Discusses: How should preload and preconnect be used together?

Related articles

How should you balance CNAME convenience and performance?Should you use a 302 redirect to a temporary alias for the new server during a DNS update?How does HSTS protect websites and subdomains?How do SSL certificates protect websites, and why are they needed?Should a Tumblr blog use the platform’s domain or the company’s domain?What types of URLs are there, and what are they used for?When should you use relative or absolute paths in WordPress?What are www subdomains and relative links used for?
ProcrustesPR
ProcrustesPR