Definition and Usage
The rel="dns-prefetch" attribute of the <link> tag indicates that the browser should preemptively perform DNS resolution for the external domain specified by the href attribute and cache IP address and other information required for connection.
This reduces latency when the browser connects to that domain, thereby improving connection speed.
Here, an external domain refers to
a domain that is different from the current web page's own domain.
<link rel="dns-prefetch" href="https://another-domain.com">
The rel="dns-prefetch" attribute of the <link> tag is used to improve connection speed when the current web page needs to connect to external domains.
There are many cases where a web page needs to connect to external domains.
- When connecting to Google's service domain to use web fonts provided by Google
- When connecting to the corresponding domain to add external libraries or scripts to a web page
- When using externally hosted images or media on a web page
In these cases, using <link rel="dns-prefetch">
can improve page loading speed by allowing the page to connect to the corresponding external domains more quickly.
Usage Notes
DNS Resolution for a Domain
Users remember websites by domain names, but servers identify them by IP addresses.
This is why DNS (Domain Name System) exists.
An IP address is a unique number used to identify a website server.
Browsers use DNS to convert a domain name into its corresponding IP address. This process is performed first when connecting to a domain. For example, suppose you link a CSS file provided by Google to use web fonts on your page. This file is located at an external domain path, https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap.
Let's look at this in the following example code.
<head>
...
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap">
...
</head>
Looking at the example code, the path to the CSS file provided by Google is an external domain (https://fonts.googleapis.com).
When this page is loaded in a browser, the browser uses DNS to look up https://fonts.googleapis.com, which is connected via the link, and resolves it to its numeric IP address. This DNS lookup typically takes approximately 20–120 milliseconds. This means that resolving an external domain through DNS takes time.
Using <link rel="dns-prefetch">
performs DNS resolution for the linked domain in advance and caches IP address and other information required for connection, saving DNS resolution time for the domain.
How to Implement
Specify the resource path as the value of the href attribute.
<head>
...
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap">
...
</head>
This allows the browser to perform DNS resolution required to load Google web fonts in advance, which can improve page loading speed.
You can repeat the above code for each external domain you want to connect to.
You can use multiple <link> tags with rel="dns-prefetch" in a single document.
Important Considerations
There are several important considerations when using the rel="dns-prefetch" attribute of the <link> tag.
Not Supported by All Browser
The rel="dns-prefetch" attribute of the <link> tag is supported by many modern browsers, but it may not be supported by older browsers or some browsers.
Refer to Browser Compatibility.
Do Not Overuse
Using the rel="dns-prefetch" attribute of the <link> tag for all external domains is not recommended. It should only be used when necessary. Excessive DNS lookups can consume unnecessary resources. This unnecessary resource consumption can cause the browser to operate inefficiently. In general, you should try not to perform more than 10 DNS lookups.
Do Not Apply to Unused External Domains
Do not use it for external domains that are not even used on the current page.
This is simply wasteful and only causes unnecessary resource consumption by performing DNS lookups for domains that are not actually needed. This can cause the browser to operate inefficiently.
rel="dns-prefetch" Can Only Be Used With the <link> Tag
Performance Improvement May Be Minimal
The rel="dns-prefetch" attribute of the <link> tag can help improve web page loading speed, but the actual perceived speed improvement may be minimal. In particular, the effect may not be significant when the user's internet connection speed is slow or when the web page size is large.
Practical Examples
When should you use the rel="dns-prefetch" attribute of the <link> tag?
In general, it can be helpful to use it for external domains that include the following:
- External domains that are frequently requested or provide large amounts of content
- Domains that host external images or video files with slow loading times
The following are some examples of using the rel="dns-prefetch" attribute of the <link> tag on a web page.
Using Google Web Fonts
When using Google Web Fonts, you can write the code as follows.
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap">
The above code performs DNS resolution in advance for the Google Web Fonts service to improve page loading speed.
Adding External Libraries or Scripts
When adding external libraries or scripts to a web page, you can write the code as follows.
<link rel="dns-prefetch" href="https://cdnjs.cloudflare.com">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
The above code performs DNS resolution for the domain hosting the jQuery library in advance, which can improve the loading speed of the jQuery script.
Using a CDN (Content Delivery Network)
A CDN (Content Delivery Network) is a service that efficiently delivers content using a network of servers distributed around the world. It can be applied to images, CSS, JavaScript files, and other resources provided through a CDN.
<link rel="dns-prefetch" href="https://cdn.jsdelivr.net">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
The above code performs DNS resolution in advance for the CDN used by the web page, which can improve the loading speed of CSS and JavaScript files.
Browser Compatibility
| Desktop | ||||
|---|---|---|---|---|
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
<link rel="dns-prefetch">
|
46 | 97 | 3 | 17.5 |
| Mobile | |||
|---|---|---|---|
| Attribute |
Mobile Chrome
|
Mobile Firefox
|
Safari
|
<link rel="dns-prefetch">
|
46 | 4 | 26 |
Specifications
| Specification | |
|---|---|
<link rel="dns-prefetch">
|
HTML Standard #link-type-dns-prefetch |
See Also
- HTML rel Attribute – Meaning, List, and Examples
- HTML <link> Tag – The External Resource Link Element
- HTML <link rel="prefetch"> – Proper Understanding and Usage
- HTML <link rel="preconnect"> – Proper Understanding and Usage
- HTML <link rel="preload"> – Proper Understanding and Usage
- HTML crossorigin Attribute – Specifying CORS Request Modes