alirezasaremi.com logo

Alireza Saremi

Optimizing Next.js for Lighthouse and Core Web Vitals (Beyond the Basics)

2025-04-08

Next.js

Achieving a perfect score in Google Lighthouse and excelling in Core Web Vitals is not just about passing a test; it leads to a faster and more enjoyable user experience. Next.js offers many built‑in features for optimization, but there are advanced strategies you can apply to push your scores even higher. This article covers image optimization, JavaScript and CSS tuning, font loading and third‑party script management.

Table of Contents

1. Understanding Core Web Vitals and Lighthouse

Core Web Vitals are a set of metrics that measure real‑world user experience: Largest Contentful Paint (LCP),First Input Delay (FID) and Cumulative Layout Shift (CLS). Lighthouse runs audits and provides scores in categories like performance, accessibility and SEO. By focusing on these metrics, you not only improve your Lighthouse score but also make your site feel faster and more stable.

Common performance killers include oversized images, unoptimised JavaScript bundles and blocking fonts. The following sections outline concrete steps to mitigate each of these.

2. Image Optimization Strategies

Images are often the largest assets on a page. Next.js provides thenext/image component, which automatically resizes and converts images to modern formats like WebP. Use the fillattribute to specify the layout and avoid cumulative layout shift. Lazy loading is built in, so offscreen images do not block the initial render.

import Image from 'next/image';

export default function Hero() {
  return (
    <div className="relative w-full h-64">
      <Image src="/banner.jpg" alt="Banner" fill priority />
    </div>
  );
}

Beyond using next/image, optimise your images at the source. Resize pictures to the display size, compress them and prefer vector formats like SVG when possible. Serving content via a CDN reduces latency for global audiences.

3. JavaScript and CSS Optimization

Large JavaScript bundles delay time to interactive. Use dynamic imports to split your code by route or component, and leverage lazy loading for heavy features. Analyze your bundle with next build --analyzeto identify large dependencies. Remove unused packages and prefer smaller alternatives.

For CSS, avoid blocking the render. Inline critical styles or use server‑side rendering to deliver CSS quickly. Tools like Tailwind’s JIT compiler generate only the classes you need, reducing file size.

4. Fonts and Third‑Party Scripts

Custom fonts can cause flash of unstyled text (FOUT) and slow down rendering. Use the next/font module to load fonts withdisplay: swap and self‑host them to avoid network latency. Preload only the weights and subsets you use.

Third‑party scripts—analytics, ads, chat widgets—often block the main thread. Load them lazily using Next.js’s <Script>component with strategy="lazyOnload" or offload them to a Web Worker via tools like Partytown. Keep track of each script’s impact and remove anything that doesn’t provide real value.

5. Conclusion

Perfect Lighthouse scores are achievable with a thoughtful approach to performance. Optimise images using next/image and CDNs, split your JavaScript with dynamic imports, streamline your CSS and treat fonts and third‑party scripts with care. These techniques not only boost your metrics but also create a better experience for your users.