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

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

Watch more workshops on topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
React Performance Debugging
React Advanced Conference 2023React Advanced Conference 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Master JavaScript Patterns
JSNation 2024JSNation 2024
145 min
Master JavaScript Patterns
Workshop
Adrian Hajdin
Adrian Hajdin
During this workshop, participants will review the essential JavaScript patterns that every developer should know. Through hands-on exercises, real-world examples, and interactive discussions, attendees will deepen their understanding of best practices for organizing code, solving common challenges, and designing scalable architectures. By the end of the workshop, participants will gain newfound confidence in their ability to write high-quality JavaScript code that stands the test of time.
Points Covered:
1. Introduction to JavaScript Patterns2. Foundational Patterns3. Object Creation Patterns4. Behavioral Patterns5. Architectural Patterns6. Hands-On Exercises and Case Studies
How It Will Help Developers:
- Gain a deep understanding of JavaScript patterns and their applications in real-world scenarios- Learn best practices for organizing code, solving common challenges, and designing scalable architectures- Enhance problem-solving skills and code readability- Improve collaboration and communication within development teams- Accelerate career growth and opportunities for advancement in the software industry
High-performance Next.js
React Summit 2022React Summit 2022
50 min
High-performance Next.js
Workshop
Michele Riva
Michele Riva
Next.js is a compelling framework that makes many tasks effortless by providing many out-of-the-box solutions. But as soon as our app needs to scale, it is essential to maintain high performance without compromising maintenance and server costs. In this workshop, we will see how to analyze Next.js performances, resources usage, how to scale it, and how to make the right decisions while writing the application architecture.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
React 18! Concurrent features! You might’ve already tried the new APIs like useTransition, or you might’ve just heard of them. But do you know how React 18 achieves the performance wins it brings with itself? In this talk, let’s peek under the hood of React 18’s performance features: - How React 18 lowers the time your page stays frozen (aka TBT) - What exactly happens in the main thread when you run useTransition() - What’s the catch with the improvements (there’s no free cake!), and why Vue.js and Preact straight refused to ship anything similar
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Our understanding of performance & user-experience has heavily evolved over the years. Web Developer Tooling needs to similarly evolve to make sure it is user-centric, actionable and contextual where modern experiences are concerned. In this talk, Addy will walk you through Chrome and others have been thinking about this problem and what updates they've been making to performance tools to lower the friction for building great experiences on the web.
Optimizing HTML5 Games: 10 Years of Learnings
JS GameDev Summit 2022JS GameDev Summit 2022
33 min
Optimizing HTML5 Games: 10 Years of Learnings
Top Content
The open source PlayCanvas game engine is built specifically for the browser, incorporating 10 years of learnings about optimization. In this talk, you will discover the secret sauce that enables PlayCanvas to generate games with lightning fast load times and rock solid frame rates.
Power Fixing React Performance Woes
React Advanced Conference 2023React Advanced Conference 2023
22 min
Power Fixing React Performance Woes
Top Content
Next.js and other wrapping React frameworks provide great power in building larger applications. But with great power comes great performance responsibility - and if you don’t pay attention, it’s easy to add multiple seconds of loading penalty on all of your pages. Eek! Let’s walk through a case study of how a few hours of performance debugging improved both load and parse times for the Centered app by several hundred percent each. We’ll learn not just why those performance problems happen, but how to diagnose and fix them. Hooray, performance! ⚡️