Improve Website Speed with Link PreloadWeb performance Link preload
In recent years, Web peformance optimizations became a mandatory skill for every front end developers. Web performance can be classified into two: page load performance and runtime performance. Here in this post, we'll see how to improve the page load performance using link preloading
Optimizing the page load or run time performance has a positive impact on the user experience. There are numourous studies shows the conversion rate in e-commerce websites has a direct relationship with the page load time. Every second saved from the page load speed improves the conversion rate.
What's Link Preload #
Link preload is a declarative way to fetch the assets without affecting the onload event of your entire page.
Link preload is declarative: Instead of using any browser APIs ( example fetch, XMLHttpRequest ..etc ), we can use the traditonal link html tag to download required assets. This is similar to the way we include any stylesheet file in our HTML page. Here is an example of preloading a css file using link preload:
<link rel="preload" href="/css/main.css" as="style">
Don't worry about those attributes. I will explain them in below sections.
Link preload does not affect onload event: Normally, while parsing the HTML, browser fetches and evaluates the assets ( JS, CSS, Images ..etc ). This, will increase the onload time of the entire page. However, assets downloaded using link preload will not affect the onload time.
Here are the all attributes you can use with the
<link rel="preload" />
This rel attribute tells the browser that it's a link preload.
In href, you need to give the relative/absolute path to the
asset that needs to be preloaded.
`as` attribute tells the browser what kind of resource to
be downloaded. The following values are applicable:
audio | document | embed | fetch | font | image | object | script | style | track | worker | video
Elements can accept a type attribute, which contains the MIME type of the resource the element points to.
If you want to do crossorigin fetches, need to set this
Media attribute can accept media types of media queries
media="( max-width: 900px )"
First, let's see how the assets in your HTML page affect the pageload. Here is an example HTML page with out any link preloading. It consist of stylesheet placed insidet the
head element, another external script file and a local script file added inside the
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Blog page</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<link rel="stylesheet" href="assets/basic.css">
<h1>My Blog Website</h1>
... content goes here
<div>Written by Shidhin CR</div>
<script src="assets/basic.js" async></script>
Once the html file is loaded from the server, the browser start parsing the HTML.The browser does the asset discovery at this time. To know more about this whole process, I recommend you to read how browsers work. Once the browser find an asset, it will pause the parsing and start fetching the assets. After the asset is downloaded, the browser continues the parsing process.
With link preloading, the browser preload scanner will look for all the links that needs to be preloaded. This happens in parallel with parsing the HTML. Once the assets are loaded, they are cached in the browser cache.
Let's see this in action. I have created two html pages, one without any optimizations and other with link preload enabled. Let's see how both these pages perform. We can measure this by looking the difference in DomContentLoaded, onLoad, FirstPaint and First Contentful Paint.
Check the first page here: Without Link Preload
And the second page with link preload enabled: With Link Preload
With out link preload
With link preload
Browser support #
Thanks for reading. Please let me know if you have any comments by pressing the comment button below.