A Preload is a resource hint that tells the browser to request content before the browser has determined it needs to make the request. This can improve performance because when the browser does realize it needs to request a resource that has been preloaded, it is already there.
I have written extensively on prefetching a DNS lookup or even preconnecting to a domain. Preloading is a much more powerful extension of these concepts because it enables you to download entire resources in advance. Let’s start with a simplified example, as shown in the waterfall diagram below:
The browser first downloads the HTML document, which references a stylesheet style.css. Inside the stylesheet, we specify a custom font that should be used, as shown below:
This is a perfect example of a late-discovered resource. This browser doesn’t know it needs to request roboto.woff until it finishes downloading and parsing style.css. We can confirm this by looking at the Initiator column in the waterfall, which tells us what file caused the browser to make the request. The HTML document has an initiator of Other because we typed it into the address bar. The initiator for style.css was the HTML document since the browser found the <link> tag in the HTML. The initiator roboto.woff was the CSS file since the browser found the @font-face declaration for a style in the CSS file that was used in the HTML.
We can optimize this by adding a preload resource hint in the <head> of the HTML file, as shown below:
This instructs the browser to proactively download roboto.woff, and creates the waterfall shown below:
There are many things to note:
The browser now downloads roboto.woff in parallel with style.css.
The initiator for roboto.woff is the HTML file instead of the CSS.
All major performance metrics are faster, and we have reduced the length of the critical rendering path.
Real Life Case Studies
That was great for a demonstration, but what are some real world examples of the benefits of using preload resource hints?
When Should I Use Preload?
In the case studies above, you will notice a pattern:
Specifically, you are able to tell the browser to request a resource before it would have otherwise requested it. It is wasteful to tell the browser to download something it would have discovered on its own. For example, if you have style.css near the top of the HTML’s <head>, there is little reason to use a preload resource hint. The browser is already going to discover it.
Preload is ideal for late-discovered resources. A late-discovered resource is a resource that requires a chain of 1 or more other resources to be downloaded or executed before the browser gets to them. For example:
Fonts that referenced inside a CSS file
Images or content that only appear after an API call is made
Incorrectly Using Preload Can Hurt Performance
Given the impressive gains described above, I know what you must be thinking:
If preload resource hints improve overall performance and user experience, why don’t I preload all the critical resources on a page?
Or, in meme form:
Unfortunately, incorrectly using preload can actually hurt performance. Why? It’s simple: browsers use a lot of complex logic to decide what needs to be downloaded, and when, so the page can render and be responsive for the visitor as quickly as possible.
A site using preload is essentially asking to override what the browser would normally do. This can be the source of different performance issues if used carelessly for 3 keys reasons:
Preload resource hints are mandatory. While other types of resource hints like preconnect or dns-prefetch hints the browser can choose to follow, preload hints are required to be executed. Modern browsers use incredibly sophisticated logic to download critical resources. Preloads, potentially more than any other type of resource hint, can interfere the most with the browser’s operation and must be used with care.
Preloads use up resources. Browsers place limits on the number of simultaneous requests it will make to a single domain. Preloading a resource “uses up” a request slot and download bandwidth you think the browser needs, versus resources the browser would otherwise use for downloading resources that it knows it needs.
Preloads happen at the most critical time. Preloads typically happen near the start of loading a page, which is the most critical time for the browser. It is focused on downloading resources on the critical rendering path, which are the resources required to start rendering the page. Too many or unnecessary preloads impacts the browser from doing the right thing, degrading performance.
Excessive Preload Hints
One of the most common mistakes adopting preload hints is to use an excessive number of them. As a general rule, websites should not preload more than 3-4 resources. Beyond that, you are trying to outsmart the browser, which is rarely a good idea. Here is why:
First, preloads work best on late-discovered resources that are also critical to the page. Having more than a handful of critical resources that also cannot be easily discovered by the browser is usually a symptom of a larger design problem.
For example, a site may be preloading 6 fonts. Are all six of those fonts needed for above-the-fold content? Would a single variable weight font file work instead?Another option would be to include the font-face: definitions directly in the HTML via a <style> tag. With that approach, those font files are no longer late-discovered resources. The browser can immediately see which of the 6 font files, if any are needed and fetch it quickly without the need for a preload hint.
The second reason to avoid excessive preload hints is technical debt. The more preloads a page has, the more likely the chance that one of those preloads is not helping accelerate the initial loading and interactivity of the page. Each represents more technical debt to maintain and ensure that the preloaded resources remain critical to that page.
Preloading unused content
Luckily, modern browsers like Chrome will display a warning in the console, as shown below:
Preloading non-critical content
Another common mistake is to preload content that is not critical to the rendering and interactivity of the above-the-fold content. Usually if you have too many preloads, it is because you are preloading non-critical content.
Are you preloading an image? Unless it is the hero image, and that hero image is truly a late-discovered resource, it’s probably not necessary.
Are you preloading a font? Is it actually used by this page, or is this preload happening in a common template header and the font is not used everywhere?
Are you preloading a video? Why? Preloading a video is almost always a bad idea since it is, by definition, not critical content. Consider using the poster attribute on a <video> tag instead.
In general, if the <link rel=”preload” as=””> attribute is something besides style, script or font, it is usually non-critical and needs additional scrutiny.
Preloading non-existent content
The ultimate manifestation of preload neglect is when the preload results in a 404. 404’s are never good. 404’s in the critical rendering path are really bad. However, 404’s for preloaded content are the worst. You have usurped the browser’s normal behavior, delaying resources that the browser knows it needs, all to download invalid content.
Preloading isn’t always the right hint for the job
Preload is different from other resource hints in that you specify a complete URL. Oftentimes, you can’t depend on that URL staying constant, or have no way of knowing what the full URL will be.
If you use comments, you might not know what user avatar images get loaded, but you do know they all come from www.gravatar.com.
If you are using an ad exchange, you may not know the path for the ad bidding, but you do know you will make a request to ads.pubmatic.com
These are great opportunities to use a Preconnect or DNS Prefetch, instead of a Preload resource hint. Remember:
It is far better for performance to use a resource hint that does less work ahead of time, than to use a preload and be wrong.
Auditing for Preload Best Practices
As we have shown, there are many benefits to using preload resource hints, but also many pitfalls that hurt performance if you use them incorrectly. This means it is important to audit how a page is using resource hints to ensure you are following all the best practices.
First, identify all the preload resource hints a page is currently using. You will want to look for <link rel=”preload”> tags in the base HTML page, as well as Link: HTTP headers which can also contain resource hints. Also remember that Link headers containing resource hints can appear on other responses beyond just the based HTML page!
Once you have the list of preloads, ask yourself the following questions:
Is this resource a late-discovered resource or will the browser already discover it?
Is the preloaded resource critical to the initial drawing or interactivity of above-the-fold content? If not, remove it.
Are you preloading more than 3-4 resources? If so, have you tested to see if these preloads improve performance metrics?
Is this preloaded resource actually used by the page? If not, remove it.
Is this preload resource returning a 4xx or 5xx error? If so, remove it
Is the resource hint included in a common header used by other pages? Is this resource used for all of those pages? If not, only include the preload hint on pages that need it.
If you are looking for a more automated solution or want to do this validation at scale, you have options. Splunk automatically audits pages for 13 different issues that can occur when using resource hints:
As with all performance optimizations, you should always measure before and after to ensure the change actually improves performance. Yes, in theory, preload hints should speed things up. But modern web pages are complex with hundreds of resources, and modern browsers are even more complex to handle those resources quickly to deliver the best experience. Trust, but verify.
Summary and Next Steps
Preloads specifically, and Resource Hints in general, are a powerful optimization technique that can make significant improvements to crucial performance metrics such Core Web Vitals. In this post we’ve shown how Preload resource hints work, described guidance on when to use them, and provided a checklist of best practices you should follow when implemented preload hints on your own sites.
Get started with a free trial of Splunk Observability Cloud today.
Splunk, Splunk>, Turn Data Into Doing, and Data-to-Everything are trademarks or registered trademarks of Splunk Inc. in the United States and other countries. All other brand names, product names, or trademarks belong to their respective owners.