Definition and Usage
The rel="preconnect" attribute of the <link> tag indicates that the browser should preemptively establish a connection to the external domain specified by the href attribute.
Here, an external domain refers to
a domain that is different from the current web page's own domain.
<link rel="preconnect" href="https://another-domain.com">
This allows the browser to reduce the time required to establish a connection to an external domain, making it faster to load resources from that domain (such as images, scripts, and fonts).
- resource
- It refers to various files or data that can be accessed on the web. This includes HTML documents, images, stylesheets (CSS), and JavaScript files, as well as various types of files used to build web page content or implement functionality, such as font files, videos, and audio files.
Establishing a connection to an external domain takes time for the browser.
The <link> tag's rel="preconnect" attribute indicates that the browser should preemptively establish a connection to the external domain specified by the href attribute.
Before requesting resources from an external domain, the browser performs several connection steps:
- It looks up the domain name to find the server's IP address. This is called a DNS (Domain Name System) lookup.
- After confirming the IP address, it establishes a connection to exchange data with the server. This is called a TCP (Transmission Control Protocol) connection.
- If a secure connection such as HTTPS is required, the browser establishes a secure connection via a TLS (Transport Layer Security) handshake.
These connection processes can vary depending on network conditions and may introduce a certain amount of latency when the browser first requests resources from an external domain.
Preconnecting to External Domains
The <link> tag's rel="preconnect" attribute allows the browser to establish a connection in advance to the external domain specified by the href attribute. This allows the browser to reduce the time required to establish a connection to an external domain, making it faster to load resources from that domain.
<head>
...
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@0;1&display=swap">
...
</head>
The example above connects a stylesheet provided by Google to use Google Web Fonts.
The URL (https://fonts.googleapis.com) of the stylesheet provided by Google is an external domain.
In the example above, the rel="preconnect" attribute of the <link> tag is used to specify that the browser should establish a connection in advance to the https://fonts.googleapis.com domain.
By specifying the external domain to preconnect to with the rel="preconnect" attribute of the <link> tag before requesting external resources, the browser prepares a connection to that domain in advance, reducing the time required to load resources afterward. You can repeat the above code for each external domain you want to preconnect to.
You can use multiple <link> tags with rel="preconnect" in a single document.
Important Considerations
There are several important considerations when using the rel="preconnect" attribute of the <link> tag.
Apply Only to Necessary External Domains
The rel="preconnect" attribute of the <link> tag is quite useful, but applying it to every external domain can be counterproductive. This is because it can create unnecessary overhead for the browser and reduce the actual benefits of preconnecting. Be careful not to preconnect to too many external domains.
The rel="preconnect" attribute of the <link> tag should only be used for essential connections.
If you need to preconnect to many external domains, using the rel="dns-prefetch" attribute of the <link> tag is recommended.
The rel="dns-prefetch" attribute of the <link> tag can reduce DNS lookup time by allowing the browser to perform the DNS lookup, which is the first step in connecting to an external domain, in advance. Therefore, compared to the rel="preconnect" attribute of the <link> tag, it requires fewer connection-related tasks from the browser and can reduce the negative impact (such as performance degradation) on initial loading performance when used for multiple external domains.
Additional Explanation
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.
No Effect on the Same Domain
The rel="preconnect" attribute of the <link> tag saves the time required to establish a connection to an external domain. Because the same domain is already connected, preconnecting to it has no effect.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<link rel="preconnect">
|
46 | 79 | 39 | 11.1 |
Specifications
| Specification | |
|---|---|
<link rel="preconnect">
|
HTML Standard #link-type-preconnect |
References
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="dns-prefetch"> – Proper Understanding and Usage
- HTML <link rel="preload"> – Proper Understanding and Usage
- HTML crossorigin Attribute – Specifying CORS Request Modes