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

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 2023React Advanced 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 🤐)
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.
The Clinic.js Workshop
JSNation 2022JSNation 2022
71 min
The Clinic.js Workshop
Workshop
Rafael Gonzaga
Rafael Gonzaga
Learn the ways of the clinic suite of tools, which help you detect performance issues in your Node.js applications. This workshop walks you through a number of examples, and the knowledge required to do benchmarking and debug I/O and Event Loop issues.

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 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize 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
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
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
Watch video: Optimizing HTML5 Games: 10 Years of Learnings
PlayCanvas is an open-source game engine used by game developers worldwide. Optimization is crucial for HTML5 games, focusing on load times and frame rate. Texture and mesh optimization can significantly reduce download sizes. GLTF and GLB formats offer smaller file sizes and faster parsing times. Compressing game resources and using efficient file formats can improve load times. Framerate optimization and resolution scaling are important for better performance. Managing draw calls and using batching techniques can optimize performance. Browser DevTools, such as Chrome and Firefox, are useful for debugging and profiling. Detecting device performance and optimizing based on specific devices can improve game performance. Apple is making progress with WebGPU implementation. HTML5 games can be shipped to the App Store using Cordova.
How React Compiler Performs on Real Code
React Advanced 2024React Advanced 2024
31 min
How React Compiler Performs on Real Code
Top Content
I'm Nadia, a developer experienced in performance, re-renders, and React. The React team released the React compiler, which eliminates the need for memoization. The compiler optimizes code by automatically memoizing components, props, and hook dependencies. It shows promise in managing changing references and improving performance. Real app testing and synthetic examples have been used to evaluate its effectiveness. The impact on initial load performance is minimal, but further investigation is needed for interactions performance. The React query library simplifies data fetching and caching. The compiler has limitations and may not catch every re-render, especially with external libraries. Enabling the compiler can improve performance but manual memorization is still necessary for optimal results. There are risks of overreliance and messy code, but the compiler can be used file by file or folder by folder with thorough testing. Practice makes incredible cats. Thank you, Nadia!