DEV Community

pawan deore
pawan deore

Posted on

Improving Core Web Vitals for Modern Web Development: A Guide to Faster Sites

In today’s web landscape, user experience is critical to the success of any website. Core Web Vitals, a set of metrics introduced by Google, play a crucial role in measuring the performance of a website, impacting both user experience and search engine rankings. These metrics specifically assess three key aspects: Loading performance (Largest Contentful Paint or LCP), Interactivity (First Input Delay or FID), and Visual Stability (Cumulative Layout Shift or CLS).

If you're developing or optimizing websites, it's essential to understand these metrics and how to improve them to enhance both user satisfaction and SEO rankings. In this article, we'll explore Core Web Vitals, highlight potential bottlenecks, and offer strategies (with code examples) to boost your website’s performance.


Key Metrics of Core Web Vitals:

  1. Largest Contentful Paint (LCP): Measures loading performance. To provide a good user experience, LCP should occur within 2.5 seconds of when the page first starts loading.
  2. First Input Delay (FID): Measures interactivity. A good FID score is less than 100 milliseconds.
  3. Cumulative Layout Shift (CLS): Measures visual stability. A CLS score of less than 0.1 ensures that users don’t experience unexpected layout shifts while interacting with the page.

Bottlenecks and Solutions for Core Web Vitals

1. Improving Largest Contentful Paint (LCP)

LCP is impacted by large images, slow server response times, and heavy CSS or JavaScript. One of the simplest ways to improve LCP is by lazy loading images and ensuring critical CSS is loaded as early as possible.

Solution: Lazy Loading Images

Modern browsers support native lazy loading for images. By adding the loading="lazy" attribute, you ensure that images below the fold are only loaded when the user scrolls to them, reducing the initial page load time.

Example:

<img src="large-image.jpg" alt="Example image" loading="lazy">
Enter fullscreen mode Exit fullscreen mode

Another way to optimize LCP is by preloading the most important resource like the hero image or key fonts that affect the visual above-the-fold content.

Preloading critical assets:

<link rel="preload" href="/path-to-important-image.jpg" as="image">
Enter fullscreen mode Exit fullscreen mode

Improving First Input Delay (FID)

FID is usually affected by heavy JavaScript execution, which can delay the browser’s response to user input. One of the most effective techniques to improve FID is to defer non-critical JavaScript and break up long-running tasks.

Solution: Defer and optimize JavaScript execution

By deferring JavaScript, you ensure the critical part of your page (HTML and CSS) loads first. You can either use the defer attribute in your script tags or dynamically load scripts only after the main content has been rendered.

Example:

<script src="heavy-script.js" defer></script>
Enter fullscreen mode Exit fullscreen mode

For larger web applications, consider breaking JavaScript tasks into smaller chunks or utilizing a web worker to handle background tasks and offload the main thread.

Using Web Workers:

if (window.Worker) {
  const worker = new Worker('worker.js');
  worker.postMessage('Process heavy data');
}
Enter fullscreen mode Exit fullscreen mode

Improving Cumulative Layout Shift (CLS)

CLS occurs when elements on the page move unexpectedly while loading. Common culprits are images without defined dimensions or dynamically injected content. To reduce CLS, always reserve space for elements, especially images and ads, and avoid injecting content late in the page load process.

Solution: Specify image dimensions
Set explicit width and height attributes for images or use CSS to ensure the layout space is reserved.

Example:

<img src="banner.jpg" alt="Banner" width="600" height="400">
Alternatively, use CSS to define aspect ratios for responsive images.
Enter fullscreen mode Exit fullscreen mode

Example:

img {
  aspect-ratio: 3 / 2;
  width: 100%;
  height: auto;
}
Enter fullscreen mode Exit fullscreen mode

Advanced Techniques to Boost Core Web Vitals

Optimize Critical CSS: Minify and inline the critical CSS for above-the-fold content to ensure faster rendering.

Use a CDN: Reduce server response times by utilizing Content Delivery Networks (CDNs) to deliver assets faster from locations closer to users.

Implement Partytown for Third-Party Scripts: Third-party scripts like Google Analytics can heavily impact FID and CLS. Consider using Partytown, a library that offloads third-party scripts to a web worker, minimizing the impact on the main thread.

Top comments (0)