Skip to content

How to manually preload assets with code?

Are you trying to optimize your website’s performance by manually preloading assets with code and smooth out every bit that has a negative impact on it?


We'll walk you through the process of preloading various different kinds of assets like fonts, CSS and JavaScript files and images, which will get downloaded locally into the browser during early stages of page load process.

TL;DR
  • Preloading allows browsers to begin loading resources early, preventing delays in rendering visual elements caused by render-blocking assets like CSS, JavaScript, fonts, and images.
  • To preload assets, use a link tag with the "rel" attribute set to "preload" and specify the type of file with the "as" attribute, such as "style" for CSS or "script" for JavaScript.
  • An optional "onload" attribute can be used to apply styles as soon as they load, but ensure compatibility by wrapping a duplicate link inside `<noscript>` tags for browsers with disabled JavaScript.
  • Preloading enhances website performance, ensuring faster load times and a better user experience, especially important in an era of complex websites and

Why is preload important?

When a browser loads a page, assets like CSS, JavaScript, fonts and image files are render-blocking by default. Furthermore, if we have a lot of these files it can cause your website to appear slow, as users won’t be able to see anything until all those files finish loading, which directly impacts Largest Contentful Paint (LCP) performance.

In other words, the window.onload event won’t be triggered until all render-blocking resources are not finished loading, causing a delay in the visual elements rendering process.

Therefore, by implementing preloading, we enable the browser to start loading resources early in the page load process, which will be used only after they’re referenced in the DOM tree. This optimization helps improve Core Web Vitals metrics. Furthermore, this will allow for visual elements to load without being delayed by those resources.

How to preload an asset?

In order to preload a CSS or JavaScript file we can use a link tag, including a “rel” attribute and setting it to preload. This is especially useful when you’ve minified JavaScript and removed unused CSS to ensure critical resources load first. Furthermore, we need to give the browser more context about what type of file we’re preloading by adding an “as” attribute and set it either to style or script, whether we’re preloading a CSS or JavaScript file, respectively.

To clarify, the “as” attribute will let the browser know about the destination of the preloading request, which will ensure that it sets appropriate request headers, priority, and applies relevant content security policy directives.

Here is an example of how to use preloading on a JavaScript file.

<link rel="preload" href="used-later.js" as="script">

Here is an example of how to use it with CSS or font files.

<link rel="preload" as="style" href='<a href="https://fonts.googleapis.com/css?family=Roboto:400,600%7CMaterial+Icons">https://fonts.googleapis.com/css?family=Roboto:400,600|Material+Icons</a>'>

Furthermore, if you want to apply a file as soon as it’s loaded, you can use the “onload” attribute and set it to “this.rel = ‘stylesheet’”. However, you need to keep in mind that this will require JavaScript to work. Therefore, you should also make a duplicate link tag of that file and wrap it inside <noscript></noscript> tags. This will ensure that the file still loads in case that the JavaScript is disabled or fails to load in the browser.

Here is also an example of such a case.

<link

  rel="preload"

  as="style"

  onload="this.rel = 'stylesheet'"

  href='https://fonts.googleapis.com/css?family=Roboto:100,900|Material+Icons'>

<noscript>

  <link

    rel="stylesheet"

    href='https://fonts.googleapis.com/css?family=Roboto:400,600|Material+Icons'>

</noscript>

Preloading fonts is particularly important for preventing layout shifts – learn more about fixing large layout shifts to improve visual stability.

Conclusion

Asset preloading works best when combined with other optimization techniques like efficiently encoding images and serving images in WebP format to create a comprehensive performance strategy.

To conclude, we talked about what preloading is good for, why it’s important, and how to implement it within code. Between websites getting ever more complex and fast internet connection being available to more and more people around the world, every byte that loads on a user’s screen matters. And preloading is one of those things that we can utilize to speed up the loading process and ensure optimal user experience. Understanding preloading is part of why you should speed up your site for better performance and user satisfaction.

BoostBuddy.io

Stay ahead with fast and optimized website that converts better!

OR

Get the complete performance checklist for just $20 $169 here!