Back to Portfolio
#Next.js#Performance#SEO

Mastering Core Web Vitals in Next.js App Router

8 min read

Achieving perfect Core Web Vitals (CWV) in modern Next.js applications requires more than just deploying to Vercel. It demands a systematic approach to caching, server-side rendering (SSR), and strategic component architecture.

Understanding the Big Three

Largest Contentful Paint (LCP): Measures loading performance. To provide a good user experience, LCP should occur within 2.5 seconds of when the page first starts loading. In Next.js, optimizing images and leveraging the Edge network are key strategies.

First Input Delay (FID): Measures interactivity. We want pages to be interactive in under 100 milliseconds. This means minimizing main thread work and deferring non-critical JavaScript to avoid blocking user inputs.

Cumulative Layout Shift (CLS): Measures visual stability. A good CLS score is 0.1 or less. Always include width and height attributes on your Next.js `Image` components and reserve space for dynamic content to avoid layout jumps.

Strategic Caching in App Router

The App Router introduces a powerful, multi-layered caching system. By understanding the Request Memoization, Data Cache, Full Route Cache, and Router Cache, you can drastically reduce Time to First Byte (TTFB).

// Example of strategic revalidation
export const revalidate = 3600 // revalidate at most every hour

export default async function Page() {
  const data = await fetch('https://api.example.com/data', {
    next: { tags: ['collection'] }
  })
  // ...
}

Conclusion

By meticulously managing how your application fetches data, loads assets, and structures its DOM, you can consistently hit 90+ across all Core Web Vitals metrics, leading to better SEO rankings and a vastly improved user experience.