Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DNS-Prefetch and Preconnect - One, or Both? Fallback?

Tags:

html

dns

I've been reading up on using dns-prefetch and preconnect. Obviously they should be used, because every little bit helps.

dns-prefetch has a lot better support across browsers than preconnect. So my question is, if a browser doesn't support preconnect, does it fall back to a dns-prefetch? My thinking is preconnect is basically dns-prefetch with some extra sauce (tls negotiations, what not). But I could also see how that isn't the case.

Next, if I am wanting something to preconnect, but want to make sure the dns-prefetch at least works, should I include both? Is that optimal?

like image 513
Kyle Hawk Avatar asked Dec 18 '17 19:12

Kyle Hawk


People also ask

What is Preconnect and DNS prefetch?

dns-prefetch : indicates to the browser that it should perform the resolution of a given domain name (determining the IP to contact) before that domain is used to download resources. preconnect : indicates to the browser that it should connect a given origin, before that domain is used to download resources.

What does DNS prefetch do?

A DNS prefetch is a resource hint to make a DNS lookup for a domain the browser has not yet determined needs to be made. This can improve performance because when the browser does need to make a request for a resource, the DNS lookup for that domain has already occurred.

Should I use Preconnect?

Preconnecting is only effective for domains other than the origin domain, so you shouldn't use it for your site. Only preconnect to critical domains you will use soon because the browser closes any connection that isn't used within 10 seconds.

What is preload and prefetch?

Preload is an early fetch instruction to the browser to request a resource needed for a page (key scripts, Web Fonts, hero images). Prefetch serves a slightly different use case — a future navigation by the user (e.g between views or pages) where fetched resources and requests need to persist across navigations.

What is the difference between DNS-prefetch and preconnect?

The difference between dns-prefetch and preconnect is dns-prefetch will only do the DNS lookup, while preconnect will do the DNS lookup, TLS negotiation, and the TCP handshake. This means that it avoids an additional 2 RTT once the resource is ready to be downloaded.

How do I use DNS-prefetch with HTTP headers?

Second, it's also possible to specify dns-prefetch (and other resources hints) as an HTTP header by using the HTTP Link field: Third, consider pairing dns-prefetch with the preconnect hint. While dns-prefetch only performs a DNS lookup, preconnect establishes a connection to a server.

What is the best way to use the DNS preconnect hint?

The preconnect hint is best used for only the most critical connections. For the others, just use <link rel="dns-prefetch"> to save time on the first step — the DNS lookup. The logic behind pairing these hints is because support for dns-prefetch is better than support for preconnect.

What does the browser look for in DNS prefetching?

The browser looks for prefetch in the HTML or the HTTP header Link. DNS prefetching allows the browser to perform DNS lookups on a page in the background while the user is browsing. This minimizes latency as the DNS lookup has already taken place once the user clicks on a link.


Video Answer


1 Answers

I was looking answer for the same question and i found this thread on reddit: https://www.reddit.com/r/webdev/comments/80w8c0/some_questions_regarding_preconnect_and/ and there was a link to a Twitter post: https://twitter.com/csswizardry/status/962313751818113024 which states:

Question:

Performance experts, shall we use dns-prefetch or preconnect for things like CDN?

preconnect seems to have a bigger impact but has a lesser support. Can we use both safely?

I can’t seem to find a definitive answer as to which to use when.

Answer:

Use both. Put your preconnects first.

like image 121
daniyel Avatar answered Nov 09 '22 09:11

daniyel