And here is the browser support for the preload. Next step we have prefetching. Prefetching is different from the other resource hints as it does not try to fetch something crucial for the current page. It instead tries to fetch non-critical sources that may come in handy in the future when the user decides to navigate to that page.
So here you can see, basically, we will load this next page CSS whenever the user goes to that page. And the browser automatically sets prefetching resources as low priority so that they don't influence the critical resources needed for the current page.
One thing to mention before we move on, there are also different types of prefetching, let's say, strategies. So it can be influenced by the user, and it can be influenced also by whether the mouse stays at some point near some link. Then the browser can also fetch that specific link. And one use case of this is, for example, if we're on a login page, while the user is typing their password in email, we can use this time and bandwidth to actually prefetch the necessary resources for the page that comes after the login, and that way once they click in the sign in, some of the resources will already be fetched and the experience will be faster.
Here is the browser support as well. As far as I've seen only Safari has some problems with prefetching, but otherwise it's pretty good. And next up we have prerender, which is the most expensive type of resource hint, because it renders a whole page in the background and if the user navigates to that page it will be the fastest, but one caveat is that it can unnecessarily waste bandwidth and make actually the experience slower, so you need to be very careful with prerender. Here is the browser support, and one thing that I want to mention, Chrome actually has this no state prefetch when we use prerender in that browser, because in order to minimize the memory, they've made it so that an initial renderer is initialized to loop over the resources and sub-resources and what it basically means is that when we use prerender in Chrome it actually acts more as a prefetch in order to save a bit of memory.
And the typical way we use these resource hints is that we put them in a link tag in our head, in our index.html or somewhere but you may be asking quite about Vue. So Vue CLI and Vite have some sort of prefetching automatically for chunks that are code splitted by them. But another way we can actually influence and do our own stuff is with a cool library I found which is by unifiedjs team and it's called unhead. It was made by Harlan Wilton, he's also a prominent vue open source contributor. And basically this lets us use a composable use head which we can then later specify links, meta tags and whatever else we want to have in our head tag. So we can just install it and then I'll show you now how we can actually use it. So if we go into VS Code, here I have a simple vue application and as you can see I have like two pages and here I've already installed it. And I'm using head so we can basically specify a title and we can also specify links. So here we have already specified that we have a link to my blog that we want to prefetch. And if we go to the website here you can see it if I just refresh. And here you can see that it actually prefetches it and it has the lowest priority. That means that whenever if there is a link to that page and we navigate to it, it will load faster. One way I can also show this is by going to elements and you can see here in the head it has injected it correctly so it's prefetched and then whatever resource that we have. So that's where one way we can influence it. Going back there is also another library that I wanted to mention and that is get.js it was made by the Angular team and essentially what we've been doing now is speculative prefetching so based on our own intuition we decide which resources to prefetch but a better way would be to use predictive prefetching. Predictive prefetching is based on data so it's data driven and we get that data from Google Analytics. Based on Google Analytics and the user's behavior when clicking links the way it works is it makes a Markov chain and Markov chain is basically simply a model which has the probability between going from one state to another but what that means for the user is that we, based on the data that we have, can actually conditionally prefetch whatever is most likely the user to go to. As far as I know there is a next module for guest.js but it also, I think it needs webpack, I'm not really sure and I haven't really gone that deep but you can also do your own research and I would love to hear what you, if you already have experience with this library Enview.
Final thoughts in conclusion, performance is a critical factor for the success of any web app and nowadays users have high expectations and they will not tolerate slow and sluggish applications. So by using performance optimization techniques, minimizing the network latency and bandwidth and using these resource hints, so predictive prefetching, we can improve the perceived performance of our website and give the user what they need and also a better user experience.
Thanks for listening, I'll be I think in Discord waiting for your questions and meanwhile you can also find me on Twitter, you can check out my blog on dev.to and you can also follow me on YouTube for more videos about Vue and Frontend.
Comments