According to Google Search Console (GCS), one area of improvement I have on Martech Zone is improving my mobile performance as per Core Web Vitals (CWV), a set of metrics that Google uses to evaluate page performance and user experience. My site utilizes custom fonts, and I noticed that there’s quite a delay in loading the page on mobile devices because those fonts are loaded late via CSS. I was able to dramatically improve my mobile performance by preloading my fonts.
Table of Contents
What is Preloading?
Preloading is a powerful technique used in web development to improve a website’s loading performance by instructing the browser to prioritize specific resources. By telling the browser to load certain assets earlier than it typically would, preloading enhances the user experience and can significantly impact
How Does Preloading Impact Core Web Vitals?
Core Web Vitals consist of three main metrics:
- Largest Contentful Paint (LCP) Measures how quickly the main content loads on a webpage. It should occur within 2.5 seconds of the page first starting to load. Preloading can help improve LCP by reducing the time essential content (like a hero image or main text) takes to appear.
- First Input Delay (FID): Measures when a user first interacts with your site (like clicking a button) and when the browser responds. FID should be less than 100 milliseconds. Preloading indirectly benefits FID by ensuring that necessary resources are ready for interaction, preventing the browser from becoming busy handling delayed resources.
- Cumulative Layout Shift (CLS): Measures the visual stability of a page. Pages should have a CLS score of less than 0.1. While preloading doesn’t directly affect CLS, it reduces the chances of delayed assets causing layout shifts, where page elements unexpectedly move as they load.
Understanding Render-Blocking
Certain resources, like CSS and JavaScript, can be render-blocking. The browser must load and process these files before displaying anything on the screen. For example, if a page’s main CSS file is render-blocking, the browser will wait to display it until it has fully downloaded and processed that CSS file. This waiting period can delay the page’s First Paint, or the moment when content first appears on the screen. By preloading render-blocking resources, you can reduce this waiting period and make your page appear faster.
When Should You Use Preloading?
Preloading is most effective when specific assets are essential to the initial rendering of your page. It’s beneficial to use preloading when:
- Critical above-the-fold content depends on specific assets, such as a hero image, key JavaScript functionality, or primary fonts. This ensures that users see the main content right away.
- Large images or background images that appear at the top of the page must load quickly so users aren’t staring at blank spaces or placeholders.
- Custom fonts that are essential to brand identity and the user experience, helping to avoid a Flash of Unstyled Text (FOUT) where the text initially displays in a fallback font until the custom font loads.
- Key scripts and stylesheets required for the page’s core functionality are loaded early to avoid render-blocking delays.
Advantages and Disadvantages of Preloading
While preloading is an incredible tool, it can also be abused and cause other issues:
Advantages
- Improved Loading Speed: By preloading critical assets, you reduce the time essential content appears, improving the Largest Contentful Paint (LCP) metric.
- Enhanced User Experience (UX): Faster loading times for essential resources enhance the user experience by reducing the wait time for visible, interactive content.
- SEO Benefits: Since Core Web Vitals are a ranking factor, improving these metrics can positively impact search engine rankings, especially for mobile users.
Disadvantages
- Increased Resource Usage: Preloading consumes bandwidth, which can be inefficient if assets are preloaded unnecessarily or are not needed immediately. It’s essential to identify and prioritize only the most critical resources.
- Potential Browser Overloading: If too many resources are preloaded, the browser may become overwhelmed with requests, which can delay other important assets. If the browser struggles to manage all preloaded resources, this can result in longer load times overall.
- Complexity in Implementation: Determining which resources are worth preloading requires understanding how the page loads and which elements are render-blocking. It requires careful testing to balance resource loading priorities without overwhelming the browser.
How to Write Preload Directives for Each Type of Asset
Different assets have unique requirements when it comes to preloading. Here’s a guide for setting up preloading for various types of resources:
1. Fonts
Custom fonts are often render-blocking, especially if essential for above-the-fold content. Users may experience a Flash of Unstyled Text (FOUT) when fonts load slowly, where fallback fonts display until the custom font loads. You can prevent this by using the <link rel="preload">
tag with appropriate attributes:
<link rel="preload" href=" as="font" type="font/woff2" crossorigin="anonymous">
By preloading fonts, you ensure they are available as soon as needed, preventing layout shifts and improving user experience. In WordPress, I was able to preload the fonts hosted on my server by including the following function in functions.php
in my child theme.
Preload fonts from the site
function mtz_preload_fonts() {
$child_theme_uri = get_stylesheet_directory_uri();
// Preload each font on a separate line
echo '<link rel="preload" href="' . $child_theme_uri . '/fonts/opensans_regular/OpenSans-Regular-webfont.woff" as="font" type="font/woff" crossorigin="anonymous">';
echo '<link rel="preload" href="' . $child_theme_uri . '/fonts/opensans_semibolditalic/OpenSans-SemiboldItalic-webfont.woff" as="font" type="font/woff" crossorigin="anonymous">';
echo '<link rel="preload" href="' . $child_theme_uri . '/fonts/muli_bold/muli-bold-webfont.woff" as="font" type="font/woff" crossorigin="anonymous">';
}
add_action('wp_head', 'mtz_preload_fonts');
2. Images
Preloading is ideal for critical images, such as a hero image at the top of the page. To preload images, use the <link rel="preload">
tag with as="image"
:
<link rel="preload" href=" as="image">
However, limit preloading to only essential images. Loading too many images at once can overload the browser, delaying the page load.
3. CSS Stylesheets
CSS files are often render-blocking by default, meaning the browser must download and apply them before displaying any content. To help prioritize the main stylesheet, you can preload it with:
<link rel="preload" href=" as="style">
If you preload CSS, remember to include a regular <link rel="stylesheet">
tag for fallback purposes since the browser may not immediately apply preloaded stylesheets.
4. JavaScript Files
Preloading JavaScript is useful for scripts that must be executed immediately to ensure proper page functionality. Be mindful that JavaScript files can also be render-blocking, so it’s a good idea only to preload essential scripts:
<link rel="preload" href=" as="script">
Avoid preloading JavaScript files that aren’t crucial for initial page functionality. If the browser is overloaded with render-blocking scripts, this can slow down rendering.
Conclusion
Preloading is a valuable technique for optimizing a website’s loading performance and enhancing Core Web Vitals, particularly the LCP metric. When used thoughtfully, preloading critical resources ensures that essential content loads quickly, leading to a better user experience and potential SEO benefits. However, overusing preloading can backfire, slowing the page and wasting resources.
Focus on preloading only the most important above-the-fold assets, such as fonts, hero images, and essential CSS or JavaScript files. By understanding which resources are render-blocking and implementing preloading strategically, you can make your website load faster, creating a smoother user experience and increasing your site’s visibility in search results.