Maximize App Performance by Optimizing Web Fonts

Rate this content
Bookmark

You've just landed on a web page and you try to click a certain element, but just before you do, an ad loads on top of it and you end up clicking that thing instead.


That…that’s a layout shift. Everyone, developers and users alike, know that layout shifts are bad. And the later they happen, the more disruptive they are to users. In this workshop we're going to look into how web fonts cause layout shifts and explore a few strategies of loading web fonts without causing big layout shifts.


Table of Contents:

What’s CLS and how it’s calculated?

How fonts can cause CLS?

Font loading strategies for minimizing CLS

Recap and conclusion

This workshop has been presented at Vue.js London 2023, check out the latest edition of this JavaScript Conference.

FAQ

Cumulative Layout Shift (CLS) is a measure of visual stability, quantifying unexpected layout shifts during a webpage's lifespan. It's calculated using the impact fraction and the viewport distance fraction, both decimal values from 0 to 1, representing the percentage of the viewport impacted and the distance unstable elements have traveled, respectively.

To minimize CLS caused by web fonts, use efficient loading strategies like hosting your own fonts, using fewer font files, selecting the appropriate font formats such as WOFF2 for better compression, and utilizing the 'font-display' CSS property to control text visibility during font loading.

FOIT (Flash of Invisible Text) occurs when text remains invisible while the web font loads. FOUT (Flash of Unstyled Text) happens when a fallback or system font is displayed temporarily until the web font fully loads, potentially causing layout shifts.

A good CLS score ranges from 0.0 to 0.1, indicating minimal layout shifts. Scores from 0.1 to 0.25 need improvement, and scores above 0.25 are considered poor, leading to a bad user experience due to significant visual instability.

Hosting your own web fonts can enhance performance by reducing the number of HTTP roundtrips required for font files, ensuring faster loading times and greater reliability compared to relying on external services like Google Fonts.

The 'font-display' CSS property controls how text is displayed during font loading. Options like 'swap', 'fallback', and 'optional' dictate whether and when to show fallback text, directly influencing the occurrence and magnitude of layout shifts.

The CLS score can vary due to differences in device capabilities, browser behavior, and network speeds. Slower devices or networks might delay font loading, increasing the likelihood and severity of layout shifts.

Tools like Sentry can be used to monitor CLS in real-time production environments, providing insights into performance issues and their impact on user experience across different sessions and conditions.

Lazar Nikolov
Lazar Nikolov
49 min
10 May, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This workshop focuses on maximizing app performance by optimizing web fonts. It covers topics such as cumulative layout shift (CLS) and how fonts cause it, as well as strategies for CLS minimization. The importance of monitoring the actual CLS score in production is emphasized. Techniques for optimizing web fonts include using system fonts, hosting your own fonts, and leveraging browser capabilities. The workshop also explores font display properties and the impact of font loading and swapping on performance. Tools like Sentry can be used for monitoring and analyzing CLS scores.

1. Maximizing App Performance with Web Fonts

Short description:

We're going to be talking about maximizing app performance by optimizing web fonts. We'll discuss cumulative layout shift (CLS) and how fonts cause it. Then, we'll move on to CLS minimization strategies and demonstrate some of them.

Hello, everyone. Thank you for joining in this beautiful workshop. We're going to be talking about maximizing app performance by optimizing web fonts. So fonts, we need to load them. And there are ways to load the fonts so it doesn't hurt our website, right? That's the workshop about. This is not a follow-along workshop. It's more informative, and we're going to be trying out things. So yeah, feel free to just pay attention. No need to code.

Okay, let's start. My name is Lazar Nikolov. I'm a developer advocate at Sentry. I'm a full stack engineer, been coding since 2013, professionally since 2015. And the agenda for today is first we're going to be talking about CLS, cumulative layout shift in general, and how fonts cause CLS. And we're going to move to CLS minimization strategies or how to minimize the CLS. And then I have a small demo prepared for you with some of the strategies.

CLS and how fonts cause it. For those of you who don't know, CLS is this. It's a layout shift, right? We have an order form. There's a yes, place my order and no, go back button. And just before the user presses on the no go back button, an ad shows up and it pushes the whole form below. And then the user clicks on the place order, which is not really what the user intended, right? That's bad UX. And the appearance of the ad at the top and the pushing down of the layout, that's called a layout shift. Okay. So they're bad. Layout shifts are bad. And on this page specifically, I guess the page will be penalized. Because the layout shift is pretty big. And the penalization happens with a bad CLS score.

2. Calculating and Monitoring CLS Score

Short description:

The CLS score is calculated as the product of the impact fraction and the viewport distance fraction. A good CLS score is within the range of 0.0 to 0.1, while a score above 0.25 requires significant improvement. It's important to note that the CLS score seen in tools like Chrome debug tools may not be the same as what users experience. Factors such as internet speeds and device performance can impact the CLS score. Implementing tools like Sentry can help monitor the actual CLS score in production and provide additional performance metrics and crash reporting.

And the penalization happens with a bad CLS score. So how does that score get calculated? It is the product of the impact fraction and the viewport distance fraction. So what are these two? The impact fraction, both of these values are decimal values from 0 to 1. And they represent the percentage of the changes relative to the viewport.

So the impact fraction is what percentage of the viewport has been impacted with the shift. So for example, if the header at the top, the title, how is CLS calculated? If that part went at the center of the screen, it got pushed down at the center of the screen, that's like 50% of the viewport, right? Because it moved half of the screen down. So it's going to be 0.5 times the distance that the unstable elements have traveled, and that is the viewport distance fraction. I guess in this case would be 0.5 again. And yeah, that's how CLS is being calculated. And there are tools, there are tools to calculate CLS. During the demo, we're going to use just the Chrome debug tools, and I'm going to show you how to calculate the CLS. But what's important to know...

Yeah, what is a good CLS score? Yeah. This is always better. But your goal should be to stay within 0.0 to 0.1 range, okay? So that is a good score, from 0.1 to 0.25. It needs work. And above 0.25, I just named it oof, because we're not going to talk about that. It definitely needs a lot of work. And what's most important to know about the CLS score is that the one that you see in your tool, for example, in Chrome debug tools, is not the one that your users are going to experience, doesn't necessarily have to be.

So yeah, not every device will give you the same CLS score. Outside factors like internet speeds or the device performance really impact the CLS score. So yeah, it is not the one that you see in Chrome. For example, on a slower internet, or, I don't know, if you're on a hiking trip and you have a 10-year-old Android phone, I guess the CLS will be bigger because the way that the phone loads, yeah, the device is not going to be able to handle that really quick. Maybe the internet coverage is bad because you're on a hiking trip. So the internet will also affect the CLS. And also, to keep an eye on what the actual CLS score is in production, you will need to implement tools like Sentry. You can measure your CLS score while you're in development, sure, but in order to keep an eye on it in production, you do need external tools like Sentry. And also, the side benefits of implementing Sentry in your app would be other performance metrics and web vitals. And you also get crash reporting. And I'm going to show you briefly at the end what you get.

QnA