DNS prefetch: A Web Performance Trick

Web performance is the objective measurements and the perceived user experience of load time and runtime.

DNS-prefetch is an attempt to resolve domain names before resources get requested. This could be a file loaded later or link target a user tri…


This content originally appeared on DEV Community and was authored by Alpha Olomi

Web performance is the objective measurements and the perceived user experience of load time and runtime.

DNS-prefetch is an attempt to resolve domain names before resources get requested. This could be a file loaded later or link target a user tries to follow.

Why use dns-prefetch?

When a browser requests a resource from a (third party) server, that cross-origin’s domain name must be resolved to an IP address before the browser can issue the request. This process is known as DNS resolution. While DNS caching can help to reduce this latency, DNS resolution can add significant latency to requests. For websites that open connections to many third parties, this latency can significantly reduce loading performance.

dns-prefetch helps developers mask DNS resolution latency. The HTML element offers this functionality by way of a rel attribute value of dns-prefetch. The cross-origin domain is then specified in the href attribute:

Syntax

<link rel="dns-prefetch" href="//example.com">

Examples

<html>
  <head>
    <link rel="dns-prefetch" href="https://fonts.gstatic.com/">
    <!-- and all other head elements -->
  </head>
  <body>
    <!-- your page content -->
  </body>
</html>

You should place dns-prefetch hints in the <head> any time your site references resources on cross-origin domains, but there are some things to keep in mind.

How to use: Best practices

There are 3 main things to keep in mind:

  • dns-prefetch is only effective for DNS lookups on cross-origin domains, so avoid using it to point to your site or domain. This is because the IP behind your site's domain will have already been resolved by the time the browser sees the hint.

  • It's also possible to specify dns-prefetch (and other resources hints) as an HTTP header by using the HTTP Link field

Link: <https://fonts.gstatic.com/>; rel=dns-prefetch
  • Consider pairing dns-prefetch with the preconnect hint. While dns-prefetch only performs a DNS lookup, preconnect establishes a connection to a server. This process includes DNS resolution, as well as establishing the TCP connection, and performing the TLS handshake if a site is served over HTTPS. Combining the two provides an opportunity to further reduce the perceived latency of cross-origin requests. You can safely use them together like so:
<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
<link rel="dns-prefetch" href="https://fonts.gstatic.com/">

Note: If a page needs to make connections to many third-party domains, preconnecting them all is counter productive. 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.

See also


This content originally appeared on DEV Community and was authored by Alpha Olomi


Print Share Comment Cite Upload Translate Updates
APA

Alpha Olomi | Sciencx (2021-05-31T13:44:54+00:00) DNS prefetch: A Web Performance Trick. Retrieved from https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/

MLA
" » DNS prefetch: A Web Performance Trick." Alpha Olomi | Sciencx - Monday May 31, 2021, https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/
HARVARD
Alpha Olomi | Sciencx Monday May 31, 2021 » DNS prefetch: A Web Performance Trick., viewed ,<https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/>
VANCOUVER
Alpha Olomi | Sciencx - » DNS prefetch: A Web Performance Trick. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/
CHICAGO
" » DNS prefetch: A Web Performance Trick." Alpha Olomi | Sciencx - Accessed . https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/
IEEE
" » DNS prefetch: A Web Performance Trick." Alpha Olomi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/. [Accessed: ]
rf:citation
» DNS prefetch: A Web Performance Trick | Alpha Olomi | Sciencx | https://www.scien.cx/2021/05/31/dns-prefetch-a-web-performance-trick/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.