How to optimize your LCP (with visuals!)

steve8708

Steve Sewell

Posted on March 29, 2022

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!

💖 💪 🙅 🚩
steve8708
Steve Sewell

Posted on March 29, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

GOTTA GO FAST!
javascript GOTTA GO FAST!

November 28, 2024

Make your web page faster
javascript Make your web page faster

September 23, 2024

Debouncing and Throttling
javascript Debouncing and Throttling

August 9, 2024

Loop Unrolling in JavaScript?
javascript Loop Unrolling in JavaScript?

July 23, 2024