black friday sale
Reasons and Solutions for Slow Loading (Speed Up Website)

Reasons and Solutions for Slow Loading (Speed Up Website)

With a website, visitors not only expect rich content but also demand swift and seamless access to information. Improving website speed not only enhances overall performance but also positively impacts user retention, SEO rankings, and conversion rates. As a result, optimizing website speed has become a top priority for businesses and web developers alike.

Below are some common reasons that lead to slow website loading (sorted by descending level of impact as I think):

Continue reading "Reasons and Solutions for Slow Loading (Speed Up Website)"

How to Delay The Execution of JavaScript to Boost Page Speed

How to Delay JavaScript to Boost Page Speed

In recent years, approaching users on multiple channels such as advertising websites, social networks, or live chat services has become more and more popular. And then, using scripts to insert those services into a website is the most important technique. However, using too many 3rd party scripts will cause a slower loading speed of the website and its Page Speed scores may be worse. At that time, you often have just 2 options: accept the slow loading or remove some scripts. After a while of researching, we found a way to have one more option. It’s delaying the execution or loading of JavaScript. Let's see how!

Continue reading "How to Delay JavaScript to Boost Page Speed"

Gutenberg vs Page Builders - What is Better and Faster? In-depth Comparison

Gutenberg vs. Page Builders - What is Better & Faster? In-depth Comparison

You can build a website with WordPress without much coding skill, or even without touching any line of code. The reason lies in many wonderful tools which help you do it easily. The most popular tools, it’s no doubt to say, are Gutenberg and page builders. At the moment, they have their pros and cons, so it’s not easy to decide which one is the best and overwhelms the other.

Continue reading "Gutenberg vs. Page Builders - What is Better & Faster? In-depth Comparison"

How to Test a Plugin's Performance and Security

How to Test a Plugin's Performance and Security

Plugins can bring many advantages to your WordPress website. However, you should consider carefully before using one because it may contain viruses and malicious codes. They also may affect your website loading speed/performance. To check if a plugin has an issue, especially for a newly launched plugin, you can use various useful tools and follow the steps below.

Continue reading "How to Test a Plugin's Performance and Security"

Review Top 5+ Cache Plugins to Speed Up Your Website Speed

Review Top 5+ Cache Plugins to Speed Up Your Website Speed

When users visit a website, loading speed may be one of the most important things that they care about. It directly affects the user experience and their decision to stay or leave the website. Therefore, webmasters always try to speed up their sites. There are many methods to do it, and using cache plugins is one of them.

Continue reading "Review Top 5+ Cache Plugins to Speed Up Your Website Speed"

How to configure WP Rocket plugin to maximize speed

How to Configure WP Rocket Plugin to Maximize Speed

Since search engines place so much significance on the speed of the loading time, it became vital to maximizing the website’s speed. Improving your website’s speed brings along a lot of benefits. Faster websites prove to have better conversions, higher search engine rankings, and further content reach. Another problem with slow loading time is that visitors instantly abandon pages that don’t load within a few seconds after opening!

Continue reading "How to Configure WP Rocket Plugin to Maximize Speed"

The Fastest Way To Load Google Fonts In WordPress (Part 2)

The Fastest Way To Load Google Fonts In WordPress (Part 2)

Google Fonts is always a bottleneck for website performance. The Google Fonts' CSS is a render-blocking resource, which means that the browser won't render any processed content until the CSS is loaded. It also causes a blank space when the font is being loaded.

In the previous post of this series, I have shown you a way to get rid of these problems using a script from PerfPerfPerf. Since then, Google Fonts has some updates and the PerfPerfPerf's becomes outdated. And we need a better way to load Google Fonts now!

Google Fonts loading problems

Before going into the solution, let's summarize 2 issues, as they are the most important problems with Google Fonts. And these are the problems we're going to resolve in this article.

Google Fonts Render Blocking CSS

When Google Fonts is being loaded, no further content (text, images, CSS, JavaScript, etc.) is loaded. That means your whole page is blocked until Google Fonts finishes loading its CSS.

This problem is critical because it freezes your website when loading and increases the First Contentful Paint (FCP) of the page. FCP is an important, user-centric metric for measuring perceived load speed because it marks the first point in the page load timeline where the user can see anything on the screen—a fast FCP helps reassure the user that something is happening. It's a part of the Web Core Vitals and is an SEO factor. So if you have it high, your site might get a lower ranking.

Blank space while loading Google Fonts

The FOIT (Flash Of Invisible Text) effect that causes bad user experience and bad FCP (First Contentful Paint)
The FOIT (Flash Of Invisible Text) effect that causes bad user experience and bad FCP (First Contentful Paint)

Another problem with Google Fonts is that when it's being loaded, the text is completely vanished before your eyes. Only until it's finished loading, you can see the text. This effect is called FOIT (Flash Of Invisible Text).

That causes a really bad experience for your users. And if the loading time is huge, your users might think your website is broken (because they see nothing!).

And FOIT also affects the First Contentful Paint (FCP) as well, and thus might affect your search ranking.

How to fix Google Fonts problems

Load Google Fonts asynchronously

To solve the 1st problem of render-blocking, we need to load Google Fonts asynchronously. That means we'll load the Google Fonts' CSS without blocking loading or rendering other resources.

There are 2 solutions:

Preloading CSS with rel="preload"

preload is a mechanism to load resources without any effect on loading the other ones. It also puts a higher priority on the resources that are preloaded. That means these resources will be load before other resources. You can read more about this on MDN.

Let's assume you want to load Roboto font. Google gives you the HTML like this:

<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet">

To make the CSS preloaded, you need to change it to:

<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="preload" as="style" onload="this.rel='stylesheet'">

The CSS is now preloaded. And when it finishes loading, it'll be applied.

The preload technique works well in all modern browsers.

Using media type

Another solution is using media type, which is supported by all browsers. This technique is created by Filament Group. The HTML markup is simple as follows:

<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet" media="print" onload="this.media='all'">

What it does is loads the CSS for the print-based media. In other words, the CSS is applied only when the users try to print the page. The loading process is now asynchronous (since we're on browsers). And when it's done, the CSS is applied for all media (which includes browsers).

I've been testing this technique for a while and see a good improvement in font loading. You can test on MetaBox.io and docs.metabox.io websites to see how it works.

Using font-display: swap

To solve the 2nd problem with a blank is when loading fonts, we need to use font-display: swap. Basically, it allows us to display the text first with a fallback font family (usually serif or sans-serif). And when the font is loaded, it swaps the text into the wanted font. This effect is called FOUT (Flash Of Unstyled Text). Unlike FOIT (Flash Of Invisible Text), users still see the text while the font is loaded. So, the user experience is better. And also the FCP (First Contentful Paint) is lower.

The good news is Google Fonts now supports font-display: swap by default. You can see it in the font URL like this:

<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">

Notice the display=swap in the URL. The CSS loaded for the font will have font-display: swap.

This technique works in all modern browsers, and it's safe to use.

Just a note that the solution in the 1st part of this series, which uses PerfPerfPerf script, tries to add font-display: swap to the Google Fonts' CSS. It's not needed anymore.

So the final code to load Google fonts now is either of these:

<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="preload" as="style" onload="this.rel='stylesheet'">
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet" media="print" onload="this.media='all'">

Apply Google Fonts loading techniques in WordPress

The best way to apply the techniques above is adding the code into your theme's header.php file. Or you can create a child theme and modify the header.php file. So your header.php file will look like this:

<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet" media="print" onload="this.media='all'">
    <?php wp_head(); ?>
</head>
// Other code

I find it's harder to do this if you use page builders. You need to disable default Google Fonts and apply the code above. The only plugin I found that disables the Google Fonts is Autoptimize. Unfortunately, it removes our manual link tag, too.

The best solution for page builders is using Autoptimize to preload the Google Fonts. You can turn it on the settings page:

Preload Google Fonts in WordPress with Autoptimize
Preload Google Fonts in WordPress with Autoptimize

You can see it in action on GretaThemes website.

Conclusion

After applying the new techniques to our websites, I'm quite happy with the result. The loading speed is increased and the FOUT effect is minimal. Users probably don't notice the font change (swapping). And of course, the Google PageSpeed Insights score is higher than before (in my test for MetaBox.io's homepage, it has 100 scores for desktops).

I think optimizing the loading speed for Google Fonts is very important for both users and search engines. And you should do it now. Feel free to discuss this with us on the Facebook group, or leave a comment below.