DEV Community

Cover image for How to optimize your LCP (with visuals!)
Steve Sewell
Steve Sewell

Posted on

How to optimize your LCP (with visuals!)

Largest Contentful Paint, or LCP, is a measurement of how fast your pages load. But it has even more significance: it impacts not only your user experience (namely: bounce rate), but also SEO

It can be hard to optimize without a bit of guidance, so I hope the below tips help you out:

How LCP is measured

First, how exactly is LCP measured. From web.dev:

The Largest Contentful Paint (LCP) metric reports the render time of the largest image or text block visible within the viewport, relative to when the page first started loading

LCP definition

Use a CDN and pregeneration

As much as possible, serve all pages and content pregenerated from a CDN cache. This ensures that response times are fast, and content can be visible as quickly as possible

Learn more: https://web.dev/optimize-lcp/#route-users-to-a-nearby-cdn

CDN optimization

Preconnect to important origins

Preconnect to external domains to establish early connections to important origins using <link rel="preconnect">:

Learn more: https://web.dev/uses-rel-preconnect/

Code example of preconnecting

Optimize your images

Importantly - be sure to optimize and prioritize your images. Use nextgen formats, optimal sizing, and eager prioritize your LCP image and lazy load others

This thread goes into great detail on recommended image optimization best practices:

Remove blocking JS

It's 2022, there is no good reason to use blocking JS anymore. Make sure to load all JS asynchronously, or even better, in a worker with Partytown

Learn more:

Remove blocking JS code example

Remove blocking CSS

Similarly, as much as possible, remove blocking externally fetched CSS.

CSS-in-JS libraries like styled-components or emotion can help a lot with this, or tools that can extract and inline critical CSS.

Learn more: https://web.dev/extract-critical-css/

Remove blocking CSS code example

Reduce JS execution

JS execution time can really hurt your LCP time as well. Be sure to optimize your site or app to require as little JS as possible to boot up.

Some projects that can help automate this for you include Qwik, Partytown, Marko, and Astro

Low-JS frameworks

Use API-driven services

Then, no site is complete without ways of managing and optimizing content. As much as possible, prefer API driven services for best performance, as opposed to those that require in-browser JS.

Some great services for managing content, assets, and tests include Builder.io, Cloudinary, LaunchDarkly, and Contentful

API-driven services

Measure, measure, measure!

Be sure to measure your LCP on real world emulate devices (read: not your powerful desktop computer!). Some great tools for this

Next Steps

To explore more about optimizing your LCP - this is a great post going into more detail on many of the topics mentioned above!

PS - this post was inspired by this great tweet from Filip Rakowski, go follow him!

Top comments (4)

Collapse
 
nikmikhailov_ profile image
Nik Mikhailov

The speed of my site was most affected by the connection of the CDN. G-core just speeded up my site and lowered my bounce rate. Strangely, some people continue to ignore the CDN to this day, and this is such a cool option to speed up the site.

Collapse
 
honorbertt profile image
Ho Norbert

I agree, it is strange to underestimate the impact from the CDN. Do you use free CDN or pay?

Collapse
 
nikmikhailov_ profile image
Nik Mikhailov

At first I tried the free one, which is quite good, by the way, with free SSL and anti-ddos. As I realized that this is a good product, I connected a paid plan. The price is nice.

Collapse
 
henrihelvetica profile image
Henri Helvetica • Edited

Appreciate the WebPageTest mention re: LCP. There are so many ways to optimize the LCP, and I can point to ways that it can actually get hammered, and how you can insure bad Core Web Vitals and Lighthouse scores. Two yrs in a row, I've had presentations on LCP and images. Both were part of an extremely insightful annual event called Image Ready:
This Is Your LCP On Images by Patrick Hulce in 2020
Images And LCP by Tim Kadlec in 2021

Enjoy.