Definition and Usage
The rel="prefetch" attribute of the <link> tag indicates that the browser should preemptively fetch the resource specified by the href attribute and store it in the cache.
It is used to prefetch and cache pages that the user is likely to navigate to next from the current page, or resources loaded after the page has finished loading (such as scripts, images, and CSS files).
- 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.
<link rel="prefetch" href="/app/module.css">
By doing this, resources expected to be used in the future are cached in advance.
When these resources are used later after the web page has loaded, there is no need to load them over the network from scratch, which saves loading time.
This feature may not be supported in some browsers.
Please refer to Browser Compatibility for more information.
Usage Notes
Prefetch
Understanding the term "prefetch" makes it easier to understand <link rel="prefetch"> and helps you better figure out how to use it.
"prefetch" refers to
caching necessary data (or resources) in advance before they are needed, and then immediately using the cached version when needed.
This helps speed up the loading and use of data and resources.
The <link rel="prefetch"> is the way this prefetching concept is applied in HTML.
How to Implement
Specify the resource path as the value of the href attribute.
<head>
...
<link rel="prefetch" href="/app/module.js">
<link rel="prefetch" href="/app/module.css">
...
</head>
You can use multiple <link> tags with rel="prefetch" in a single document.
Important Considerations
There are several important considerations when using the rel="prefetch" attribute of the <link> tag.
Apply Only to Resources Needed Later
<link rel="prefetch"> does not directly improve the loading speed of resources when applied to resources loaded along with the current web page.
Instead, <link rel="prefetch"> fetches pages that the user is likely to visit later, or resources needed after the page has finished loading, and stores them in the browser's cache. This can potentially speed up page loading when those resources are needed later, since they are already stored in the local cache.
The following is an example of incorrect usage of <link rel="prefetch">.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Incorrect Usage Example</title>
<link rel="prefetch" href="image.jpg">
</head>
<body>
<img src="image.jpg" alt="Image loaded on the current web page">
</body>
</html>
In the example above, <link rel="prefetch"> was applied to an image that is loaded on the current web page. <link rel="prefetch"> should be used to prefetch pages that the user is likely to visit later or resources that are needed after the page has finished loading. Since an image loaded on the current web page is already a resource that is going to be displayed to the user, using <link rel="prefetch"> to load it in advance does not effectively improve page loading speed.
The following is an example of correct usage of <link rel="prefetch">.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Correct Usage Example</title>
<link rel="prefetch" href="next-page.html">
</head>
<body>
<a href="next-page.html">Go to the next page</a>
</body>
</html>
This is an example of correct usage. <link rel="prefetch"> is used to prefetch a page that the user is likely to visit later, such as next-page.html. A link to navigate to this page is also provided.
This allows the page to load faster when the user clicks the "Go to the next page" link because the page is already stored in the local cache.
Distinguishing Between Same-Origin and Cross-Origin Resources
<link rel="prefetch"> is primarily used to prefetch resources or documents from the same origin.
Cross-origin resources can also be fetched, but they may be restricted depending on the browser's security policies (such as CORS, Cross-Origin Resource Sharing) or privacy policies (such as third-party cookie restrictions).
For example, assume that the URL of a web page is https://example.com/page-1.html.
In addition to https://example.com/styles.css from the same domain, resources from an external domain, such as https://other-domain.com/image.png, can also be prefetched using <link rel="prefetch">. However, when fetching resources from an external domain, appropriate CORS configuration is required depending on the browser's security policies.
The following is an example of correctly prefetching a cross-origin resource using <link rel="prefetch">.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External Domain Resource Prefetch Example</title>
<!-- When prefetching resources from an external CDN or another domain, specify the crossorigin attribute as well. -->
<link rel="prefetch" href="https://other-domain.com/image.png" as="image" crossorigin="anonymous">
</head>
<body>
<img src="https://other-domain.com/image.png" alt="External image">
</body>
</html>
Code Explanation
The crossorigin attribute specifies the CORS (Cross-Origin Resource Sharing) request mode used when fetching external resources linked to an element.
In this way, images, scripts, and other resources from external domains can also be prefetched using <link rel="prefetch">. However, to properly handle cross-origin resources, the CORS policy of the external server must allow the type of request made by the browser.
rel="prefetch" Can Only Be Used With the <link> Tag
Avoid Using Multiple <link rel="prefetch"> Tags for the Same Resource
Using multiple <link rel="prefetch"> tags for the same resource is not efficient and may put an unnecessary burden on the browser. If multiple prefetch requests occur at the same time, they can result in unnecessary network traffic and inefficient use of the browser's cache memory.
There is no need to use <link rel="prefetch"> for resources that are already stored in the cache.
<link rel="prefetch"> tags for the same resource
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Incorrect Usage Example</title>
<link rel="prefetch" href="next-page.html">
<!-- Duplicate prefetch request -->
<link rel="prefetch" href="next-page.html">
</head>
<body>
<a href="next-page.html">Go to the next page</a>
</body>
</html>
<link rel="prefetch"> Does Not Affect JavaScript's onload Event
The JavaScript onload event occurs after all page resources have been loaded and the DOM tree has been fully constructed. The onload event is designed to occur after the DOM tree has been fully constructed and all resources have been loaded.
Resources prefetched using <link rel="prefetch"> do not affect DOM tree construction or the loading of other resources, so they do not affect when the onload event is triggered.
Therefore, when using the onload event to determine whether page loading has completed, there is no need to consider resources prefetched using <link rel="prefetch">.
<link rel="prefetch"> Does Not Work Recursively
<link rel="prefetch"> is used to instruct the browser to prefetch a specific resource.
However, it does not automatically prefetch other resources included in that resource.
For example, consider the following.
<link rel="prefetch" href="page.html">
In the example above, the page.html page itself (the HTML resource) is prefetched, but other resources included in the page.html page, such as CSS files, image files, and JavaScript files, are not automatically prefetched.
If you want to prefetch resources included in the page.html page as well, you must use separate <link rel="prefetch"> tags for each resource.
<link rel="prefetch" href="page.html">
<link rel="prefetch" href="page.css">
<link rel="prefetch" href="page.js">
<link rel="prefetch" href="page-image.jpg">
This allows the page.html page and the CSS files, JavaScript files, and image files included in that page to all be prefetched.
Practical Examples
<link rel="prefetch"> is an effective feature for improving website loading speed, but it is not suitable for every situation. The following are some common use cases for <link rel="prefetch">.
Prefetching the Next Page
While the user is visiting the current page, required resources for the next page can be prefetched to minimize the time required to navigate to that page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Prefetching the Next Page</title>
<link rel="prefetch" href="next-page.html">
</head>
<body>
<h1>Current Page</h1>
<a href="next-page.html">Go to the next page</a>
</body>
</html>
Optimizing Asynchronous Component Loading
In web applications, asynchronously loaded components can be prefetched to minimize loading delays when users request those features.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Optimizing Asynchronous Component Loading</title>
<!-- Prefetch an asynchronously loaded component -->
<link rel="prefetch" href="async-component.js">
</head>
<body>
<h1>Current Page</h1>
<p>
To load the asynchronous component,
click <button id="load-component">here</button>.
</p>
<script>
const btn = document.getElementById("load-component");
btn.addEventListener("click", () => {
// Load the component asynchronously
import("async-component.js").then(module => {
// Use the loaded component
});
});
</script>
</body>
</html>
Browser Compatibility
Last updated:
| Desktop | ||||
|---|---|---|---|---|
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
<link rel="prefetch">
|
8 | 12 | 2 | No |
Last updated:
| Mobile | |||
|---|---|---|---|
| Attribute |
Mobile Chrome
|
Mobile Firefox
|
Safari
|
<link rel="prefetch">
|
18 | 4 | No |
Specifications
| Specification | |
|---|---|
<link rel="prefetch">
|
HTML Standard #link-type-prefetch |
See Also
- HTML rel Attribute – Meaning, List, and Examples
- HTML <link> Tag – The External Resource Link Element
- HTML <link rel="dns-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