Prefetch
May 20, 2023
Prefetch
is a technique used in web development to improve the perceived speed of a website or application. It is a process of fetching resources, such as images, scripts, stylesheets, or other assets, that a web page is likely to use in the future, and caching them in the user’s browser before they are actually needed. By doing so, the browser can load the resources much faster when they are requested, leading to a smoother and more responsive user experience.
Purpose of Prefetching
The primary purpose of prefetching
is to reduce the amount of time it takes for a web page to load. When a user visits a website, the browser sends a request to the server for each resource needed to render the page, such as HTML, CSS, JavaScript, images, and so on. The server then responds with the requested resources, which the browser then downloads and renders on the page.
However, this process can be slow, especially if the website has a lot of resources or if the user has a slow internet connection. Prefetching can help to reduce the time it takes for the browser to download and render these resources by fetching them ahead of time, before they are actually needed.
The idea behind prefetching is that the browser can predict what resources a web page will need in the future based on the current page and the user’s browsing patterns. For example, if a user clicks on a link to go to a new page, the browser can prefetch the resources for that page in advance, so that when the user actually navigates to the new page, the resources are already available and can be loaded quickly.
Types of Prefetching
There are several different types of prefetching that can be used in web development, each with its own purpose and usage.
Link Prefetching
Link prefetching
is a technique that involves adding a rel="prefetch"
attribute to a link element in the HTML code of a web page. This tells the browser to fetch the resource specified in the link ahead of time, even before the user requests it.
For example, if a web page contains a link to an image, the developer can add the following code to prefetch the image:
<link rel="prefetch" href="path/to/image.jpg">
When the user loads the page, the browser will fetch the image and cache it in the background, so that when the user actually views the image, it can be loaded quickly.
Link prefetching can be used for any type of resource, including CSS files, JavaScript files, and other assets. However, it should be used sparingly, as prefetching too many resources can actually slow down the page load time.
DNS Prefetching
DNS prefetching
is a technique that involves resolving domain names in advance, before the browser needs to request resources from those domains. When a web page contains links to resources on different domains, the browser needs to resolve the domain names for each of those links before it can request the resources.
DNS prefetching can help to speed up this process by resolving the domain names in advance, so that when the browser needs to request resources from those domains, the domain names are already resolved and the requests can be made more quickly.
To enable DNS prefetching, developers can add the following code to the HTML head section:
<meta http-equiv="x-dns-prefetch-control" content="on">
<link rel="dns-prefetch" href="//example.com">
This code tells the browser to enable DNS prefetching and to prefetch the domain “example.com”.
Resource Hinting
Resource hinting
is a technique that involves adding hints to the HTML code of a web page to tell the browser which resources it should prefetch. There are several types of hints that can be used, each with its own purpose and syntax:
Preload
The preload
hint tells the browser to fetch a resource as soon as possible, even before it has finished parsing the HTML code of the page. This can be useful for resources that are critical to the page’s functionality, such as JavaScript files or fonts.
<link rel="preload" href="path/to/script.js" as="script">
This code tells the browser to preload the JavaScript file “script.js”.
Prefetch
The prefetch
hint tells the browser to fetch a resource in the background, after it has finished parsing the HTML code of the page. This can be useful for resources that are likely to be needed in the future, but are not critical to the page’s functionality.
<link rel="prefetch" href="path/to/image.jpg">
This code tells the browser to prefetch the image “image.jpg”.
Prerender
The prerender
hint tells the browser to fetch and render an entire web page in the background, even before the user requests it. This can be useful for web applications that require fast navigation between pages, as the next page can be prerendered while the user is still viewing the current page.
<link rel="prerender" href="path/to/next/page.html">
This code tells the browser to prerender the next page “next/page.html.
Best Practices for Prefetching
While prefetching can be a powerful technique for improving the speed and responsiveness of a website or application, it should be used carefully and thoughtfully to avoid negative effects on performance.
Here are some best practices to keep in mind when using prefetching:
1. Only prefetch resources that are likely to be used
Prefetching too many resources can actually slow down the page load time, as the browser has to spend extra time fetching and caching unnecessary resources. Make sure to only prefetch resources that are likely to be used, based on the current page and the user’s browsing patterns.
2. Use resource hinting to provide more information to the browser
Resource hinting can be a powerful tool for providing more information to the browser about which resources should be prefetched and when. Use preload hints for critical resources that should be fetched as soon as possible, prefetch hints for resources that are likely to be used in the future, and prerender hints for entire pages that should be loaded quickly.
3. Test and measure the performance impact of prefetching
Prefetching can have both positive and negative effects on the performance of a website or application, depending on how it is used. Make sure to test and measure the performance impact of prefetching on different devices and network conditions, and adjust the prefetching strategy as needed.