Optimizing your largest contentful paint (LCP) can be hard
Here are 8 tips, with code snippets and visuals, to help your pages load fast for optimal SEO and UX
🧵👇
First, how is LCP measured?
> 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
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
Importantly - be sure to optimize and prioritize your images. Use nextgen formats, optimal sizing, and eager prioritize your LCP image and lazy load others:
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 web.dev/extract-critic…
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.
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
Want to measure your LCP and see which tip above might help improve it the most?
Oh ya! And don't forget to measure measure measure your site speed. Prefer tools that emulate real world devices like @RealWebPageTest and Google Pagespeed Insights over running Lighthouse in your desktop browser that may not match typical real world device specs
In short, JS VMs try to assume a shape of an object using a hidden class. When the shape changes, this can lead to a deopt
It's the same reason why setting a property to null/undefined can be faster than deleting:
A Map, on the other hand, is optimized for this very use case of frequently adding and removing keys (see the "performance" table row here): developer.mozilla.org/en-US/docs/Web…
Well, you are in luck. Image components provided @cloudinary, @nextjs, @GatsbyJS, and @nuxt_js do many of these optimizations (like nextgen format, dynamic srcset, etc) for you completely automatically!
If you are wondering "between scripts, images, and iframes, which would have the most impact on my site speed to put time into optimizing" - I got you: builder.io/c/performance-…