Navigating the Waters of Core Web Vitals in 2024
Tags: webdev, programming, news, webperf
Published: 2024-02-06 07:49:10

In the ever-evolving domain of web development, delivering an optimal user experience is paramount, yet for many years, there were no clear standards for what constitutes a good user experience. However, this changed in May 2020, when Google introduced the **Core Web Vitals**, a set of standardised user-centric metrics, which help site owners and developers have a better understanding of how visitors experience their websites.

This article aims to provide some more insights on these metrics through examples, while also casting light on the significant upcoming changes with the **Interaction to Next Paint (INP)** metric set to replace First Input Delay (FID) in March 2024.

Table of contents:
- [How to measure Core Web Vitals](#measuring-core-web-vitals)
- [Largest Contentful Paint (LCP)](#lcp)
- [First Input Delay (FID)](#fid)
- [Cumulative Layout Shift (CLS)](#cls)
- [The introduction of Interaction to Next Paint (INP)](#inp)

<a name="measuring-core-web-vitals" href="#measuring-core-web-vitals"></a>
To begin with, if we want to measure our web application's Core Web Vitals scores we can use the [PageSpeed Insights](https://pagespeed.web.dev/), which provides access to [Chrome User Experience (CrUX)](https://developer.chrome.com/docs/crux/) data in the top section labeled "Discover what your real users are experiencing".

![Pagespeed Insights example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rab55mrujyy43cgv0he1.png)

## Reviewing the Core Web Vitals until 2024: LCP, FID, and CLS

<a name="lcp" href="#lcp"></a>
**Largest Contentful Paint (LCP)** measures the load speed of the main content, focusing on the point in the page load timeline when the primary content (e.g. main banner section) has been loaded — a key metric for understanding user experience. It essentially <u>marks the time at which the page's main content has become visible to the user</u>, providing a realistic snapshot of when they can start consuming the page's information.

_The LCP thresholds. Source: [web.dev](https://web.dev)_
![The LCP thresholds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smttvk3x26e6jikzl1s2.png)


Let's say we want to improve the LCP score in a website. To improve the load time of the main content, we would have to first fine tune (e.g. compress / optimise image size per viewport) any asset(s) included in the main content and postpone (e.g. lazy load) the load of other parts of the page (e.g. header, side navigation, footer). On the other hand, if we wanted to have a very bad LCP, we would place a large, uncompressed hero image as a main banner and make sure that other parts of the page are being loaded before it does.

<a name="fid" href="#fid"></a>
**First Input Delay (FID)** measures the page's responsiveness to <u>initial</u> user interactions, such as clicks, taps, or key presses, highlighting the real-world experience users have with the interactivity of a site. This metric is essential because it <u>measures the time from when a user first interacts with a page to the time when the browser is actually able to begin processing event handlers</u> in response to that interaction.

_The FID thresholds. Source: [web.dev](https://web.dev)_
![The FID thresholds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mgq5epkql18b1ovgt5f.png)

For better FID we would split a hefty JavaScript bundle into smaller chunks which can speed up parsing and execution. On the contrary, a monolithic JavaScript file that blocks the main thread (e.g. with many async methods) can lead to noticeable delays in interactive responsiveness, resulting in poor FID performance.

<a name="cls" href="#cls"></a>
**Cumulative Layout Shift (CLS)** gauges the stability of a page's layout, <u>measuring how much content shifts unexpectedly as the site loads</u> and during its interaction. This metric is critical for user experience, as unpredictable movements can lead to mistaken clicks, difficulty in reading content, and overall frustration for the user.

_The CLS thresholds. Source: [web.dev](https://web.dev)_
![The CLS thresholds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hw6trc388jb7bz3use2b.png)

To eliminate CLS, we have to reserve space for images or other types of content before they are fully loaded on the page. Using placeholders before images and assigning fixed dimensions for media assets, prevent unexpected layout shifts as content loads. In general content that loads asynchronously without reserved space can cause disruptive shifts, increasing CLS.

<a name="inp" href="#inp"></a>
## Core Web Vitals in 2024: The Introduction of INP

Starting from March 2024, **Interaction to Next Paint (INP)** is set to offer a more detailed measure of a page's responsiveness by tracking the delay between user interactions and the visual updates that follow. 

_The lifecycle of an interaction. Source: [web.dev](https://web.dev)_
![The lifecycle of an interaction.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99k54dh2l980bnqmimvo.png)

Significantly, **INP will replace FID**, providing a broader evaluation of a page's interactive performance. INP is considered a better metric than FID, particularly because it offers a more comprehensive understanding of a website's interactivity and responsiveness **across the entirety of a user's session**, not just at the beginning, since the INP score is <u>measured when the user leaves the page by aggregating all interactions the user made with the page throughout the page’s lifetime</u> and returning the worst-measured score.

So in order to ensure we're delivering user experiences with good responsiveness and our web app has a good INP score, we have to make sure that the worst measured INP of our page is:

- <=200 milliseconds. Then this means that our page has good responsiveness.
- If our INP is >200 milliseconds and <=500 milliseconds, this means that our page's responsiveness needs improvement.
- If our page's worst INP is >500 milliseconds then we will receive a poor INP score.

Optimising a website for INP would involve streamlining JavaScript execution, via task chunking and **prioritising essential scripts** to enhance interaction responsiveness. Applying lazy loading for non-critical content and prioritising crucial assets can improve the perception of speed for users. State management is also crucial, e.g. **avoiding unnecessary state updates and using caching techniques** can help in reducing rerendering cycles and improving response times to user inputs. Last but not least, **implementing debouncing or throttling** for rapid user interactions such as typing or scrolling, can prevent excessive function calls that might lead to UI jank or delayed responses.

By focusing on these areas, we can enhance the responsiveness of our web app and overall improve its Core Web Vitals scores, including the INP metric, leading to a smoother and more engaging user experience. Regular testing and continuous optimisation should be an essential part of our development cycle to timely address performance issues as our application evolves.