🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Performance••By Komal Nakrani

Fix Largest Contentful Paint (LCP) on Astro Sites

Fix Largest Contentful Paint on Astro sites: find the LCP element, cut TTFB, load the hero image with priority, preload fonts, inline CSS, and avoid client-only heroes.

#Astro#LCP#Largest Contentful Paint#Core Web Vitals

Largest Contentful Paint (LCP) measures how long it takes for the biggest image or text block in the viewport to appear. Google rates it good at 2.5 seconds or less and poor above 4 seconds, at the 75th percentile of real visits. On Astro sites, slow LCP almost always has one of four causes: a slow server response, a hero image that loads late (often lazy-loaded), a web font that delays text, or a hero rendered by a client-side island. Each has a direct fix.

This guide shows how to find your LCP element, break its time into parts, and fix each part with Astro 7 code.

Key Takeaways

  • Find the LCP element first; the fix depends on whether it’s an image or text.
  • LCP splits into TTFB, resource load delay, resource load time and render delay.
  • Add priority to the hero <Image />: it sets loading="eager" and fetchpriority="high".
  • Use the Fonts API with preload and font-display: swap so text paints quickly.
  • Never render the hero with client:only; keep the LCP element in server-rendered HTML.

Step 1: Find the LCP element

You can’t fix LCP without knowing which element it is, and it often isn’t the one you’d guess. Check in two places:

  1. PageSpeed Insights. Run the URL. The report shows field LCP from Chrome users (if available) and, in the lab section, the LCP element with a breakdown of its timing.
  2. Chrome DevTools. Open the Performance panel and reload. The live metrics view shows LCP and highlights the element. Throttle the network and CPU to approximate a mid-range phone.

The LCP element is usually a hero image, a large heading, or the first paragraph on text-heavy pages. On mobile, it’s often different from desktop, so check both.

Step 2: Break largest contentful paint into its parts

LCP time is the sum of four parts:

Part What it is Typical Astro cause
Time to first byte (TTFB) Until the first byte of HTML arrives Slow on-demand render, no CDN, redirects
Resource load delay From TTFB until the LCP image starts downloading Lazy-loaded hero, image in CSS, image discovered by JavaScript
Resource load duration Downloading the image Oversized file, no modern format, no responsive sizes
Element render delay From download until it paints Render-blocking CSS or fonts, client-side rendering

Text LCP elements skip the two resource parts, so for text, only TTFB and render delay matter. PageSpeed Insights shows this breakdown, which tells you which section below to start with.

Fix 1: Reduce TTFB

For a static Astro site on Cloudflare Pages, Netlify, Vercel or GitHub Pages, TTFB is usually low, because HTML is served from a CDN edge near the visitor. If it isn’t:

  • Check for redirects. A visit to example.com that redirects to www.example.com/ and then adds a trailing slash costs two extra round trips. Link to canonical URLs directly; the trailing slash tip helps you keep them consistent.
  • Prerender what you can. On-demand pages wait for your code and data. If a page is the same for everyone, remove prerender = false.
  • Cache on-demand pages at the CDN with s-maxage and stale-while-revalidate, or Astro 7’s route caching. The Astro caching explainer covers both.
  • Put a CDN in front of self-hosted servers. See what a CDN does for an Astro site.

Fix 2: Load the hero image early

The most common LCP mistake is a lazy-loaded hero. Astro’s <Image /> lazy-loads by default, which is right for images below the fold and wrong for the hero. Add priority:

---
// src/components/Hero.astro
import { Image } from 'astro:assets';
import heroImage from '../assets/hero.jpg';
---
<section class="hero">
  <Image
    src={heroImage}
    alt="Studio workspace with a large monitor showing a website design"
    widths={[640, 960, 1280, 1920]}
    sizes="100vw"
    priority
  />
  <h1>Websites that load before you blink</h1>
</section>

priority sets loading="eager", decoding="sync" and fetchpriority="high", so the browser requests the image as soon as it parses the tag, ahead of other images. Use it on one image per page: the one that’s actually the LCP element.

Also avoid these patterns, which hide the image from the browser’s preload scanner:

  • Hero as a CSS background-image. The browser only discovers it after downloading and parsing CSS. Use an <Image /> with object-fit: cover positioned behind the text instead.
  • Hero injected by JavaScript (a carousel that builds slides in a script). Render the first slide in HTML.
  • Hero in an island with client:only. The image doesn’t exist until the framework loads. See Fix 5.

Fix 3: Make the hero image smaller

Once the image starts early, the download time is about bytes:

  • Use an imported image from src/, not a file in public/. Astro converts it to WebP by default and generates the sizes you ask for. Files in public/ are served as-is.
  • Provide responsive widths with widths and sizes (or layout="full-width"), so phones download a phone-sized image instead of a 2,400 px original.
  • Consider AVIF with <Picture formats={['avif', 'webp']} /> for photographic heroes, which are often smaller at the same quality.
  • Lower quality slightly with quality="mid" or a number; heroes behind text rarely need maximum quality.

The responsive images tip and the Image component guide cover every option.

Fix 4: Stop fonts from delaying text

When the LCP element is a heading or paragraph, web fonts are often the bottleneck. With font-display: block (the default for many font services), text stays invisible until the font downloads.

Astro’s Fonts API downloads fonts at build time, serves them from your site, and can preload them:

// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';

export default defineConfig({
  fonts: [
    {
      provider: fontProviders.google(),
      name: 'Inter',
      cssVariable: '--font-body',
      weights: [400, 700],
      display: 'swap',
    },
  ],
});
---
// src/layouts/BaseLayout.astro
import { Font } from 'astro:assets';
---
<html lang="en">
  <head>
    <Font cssVariable="--font-body" preload />
  </head>
  <body style="font-family: var(--font-body)">
    <slot />
  </body>
</html>

display: 'swap' paints text immediately in a fallback font, and preload adds <link rel="preload"> for the font files so they download early. Load only the weights you use; every extra weight is another file. The preload fonts tip covers which files to preload.

Fix 5: Keep the LCP element in server-rendered HTML

An island with client:only renders nothing on the server. The hero appears only after its framework’s JavaScript downloads, parses and runs, which can add seconds on a phone:

<!-- Slow LCP: the hero doesn't exist until React loads -->
<HeroCarousel client:only="react" />

<!-- Fast LCP: HTML is server-rendered; interactivity hydrates later -->
<HeroCarousel client:visible />

Without client:only, Astro renders the component’s HTML at build time, so the first slide paints with the HTML, and hydration adds interactivity afterward. Better still, if the hero doesn’t need to be interactive, make it a plain .astro component with no directive at all. The client directive guide explains the trade-offs.

Fix 6: Remove render-blocking CSS and scripts

Large external stylesheets block rendering until they download. Astro inlines stylesheets under 4 kB by default (build.inlineStylesheets: 'auto'); for small sites, 'always' removes the extra request entirely. Keep component styles in components so each page loads only what it uses. Processed <script> tags are modules and don’t block rendering, but is:inline scripts in the <head> do; keep those tiny. Third-party scripts in the <head> (tag managers, A/B testing tools that hide the page until they run) are a frequent hidden cause of render delay. The inline critical CSS tip goes further.

Fix 7: Don’t let animations hide the hero

A fade-in or slide-in on the hero, starting from opacity: 0, delays LCP until the animation makes the element visible. If you animate the hero, animate transform from a visible state, or skip the animation for the LCP element. The same applies to view transition animations on the first page load.

An LCP checklist for Astro

  1. Identify the LCP element on mobile and desktop.
  2. Serve HTML from a CDN; avoid redirect chains.
  3. Hero image: imported from src/, <Image /> with priority, responsive widths and sizes.
  4. No CSS background or JavaScript-injected hero images.
  5. Fonts through the Fonts API with display: 'swap' and preload.
  6. No client:only above the fold.
  7. Small, inlined CSS; no blocking third-party scripts in the <head>.
  8. Verify with field data after deploying, because lab results don’t always match real visitors.

Komal themes render heroes as server-side HTML with Astro’s image components, so the checklist mostly comes down to marking your chosen hero image with priority; you can run any Astro theme preview through PageSpeed Insights to compare. The Astro performance guide covers the other metrics, and web.dev’s LCP optimization guide explains the four-part breakdown in detail.

FAQ

What is a good Largest Contentful Paint score?

2.5 seconds or less at the 75th percentile of page visits is good. Between 2.5 and 4 seconds needs improvement, and above 4 seconds is poor.

Why is my Astro hero image slowing down LCP?

Usually because it’s lazy-loaded, which is the default for <Image />, or because it’s a CSS background or rendered by JavaScript, so the browser finds it late. Add priority to the hero <Image /> and keep it in the HTML.

What does the priority prop do on Astro’s Image component?

It sets loading="eager", decoding="sync" and fetchpriority="high" on the image, so the browser downloads it immediately with high priority. Use it only on the above-the-fold image that is the LCP element.

Do web fonts affect LCP?

Yes, when the LCP element is text. If text is hidden until the font loads, LCP waits for the font. Use font-display: swap and preload the font files, which Astro’s Fonts API supports with display and the <Font preload /> component.

Why is my LCP different on mobile and desktop?

The LCP element, image size, network and CPU all differ. A mobile layout may make a heading the LCP element instead of an image, and slower phones take longer to render. Check and fix both separately.