Engineering Insights

Mastering Core Web Vitals: Fixing LCP, INP, and CLS in Production Single-Page Apps

Why Core Web Vitals Determine Google Search Rankings

Google uses Core Web Vitals as a direct page experience ranking signal. In 2024, Google officially replaced First Input Delay (FID) with Interaction to Next Paint (INP), raising the bar for modern client-side JavaScript applications.

1. Largest Contentful Paint (LCP) Optimization

LCP measures when the largest visual element above the fold becomes visible. Target score: < 2.5 seconds.

  • Image Preloading: Use <link rel="preload" as="image" href="..." fetchpriority="high"> for hero visuals.
  • Font Display Swap: Apply font-display: swap to eliminate FOIT (Flash of Unstyled Text).
  • Avoid Lazy-Loading Hero Elements: Never add loading="lazy" to images above the fold.

2. Interaction to Next Paint (INP) Reduction

INP assesses overall responsiveness during user interactions (clicks, taps, keypresses). Target score: < 200 ms.

  • Break Up Long Tasks: Yield execution to the main thread using setTimeout(..., 0) or requestIdleCallback().
  • Debounce Heavy Event Handlers: Throttle search inputs, scroll listeners, and dynamic DOM re-renders.
  • Defer Non-Critical JS: Code-split non-immediate modules with dynamic imports (import()).

3. Cumulative Layout Shift (CLS) Elimination

CLS measures unexpected visual layout jumps. Target score: < 0.1.

  • Explicit Aspect Ratios: Always define width and height or CSS aspect-ratio on images and container placeholders.
  • Reserve Space for Ads and Banners: Use CSS skeleton loaders or fixed container min-heights before dynamic content hydrates.
  • CSS Containment: Use contain: layout to restrict layout calculations to isolated subtrees.