Cursorcursorintermediate

Optimize Core Web Vitals in Next.js (Cursor Prompt)

Cursor prompt to optimize LCP, CLS, and INP in a Next.js app with images, fonts, and script loading.

What you'll get

Optimized Core Web Vitals with improved LCP, zero CLS, and responsive INP across all pages.

The Prompt

Optimize Core Web Vitals (LCP, CLS, INP) in this Next.js app.

FILES TO REVIEW AND MODIFY:
- All page.tsx files — Check for LCP image optimization
- src/app/layout.tsx — Font loading and script optimization
- next.config.ts — Image optimization config
- All components with images — Replace <img> with next/image
- All components with dynamic content — Fix CLS issues

OPTIMIZATIONS:
1. LCP (Largest Contentful Paint):
   - Add priority prop to above-the-fold hero images in next/image
   - Preload critical fonts using next/font with display: 'swap'
   - Move critical CSS inline; defer non-critical CSS
   - Add sizes prop to all next/image components to prevent unnecessary large image downloads

2. CLS (Cumulative Layout Shift):
   - Add explicit width and height to all next/image components
   - Add aspect-ratio CSS to containers that load dynamic content
   - Use loading skeletons that match content dimensions
   - Set font-display: swap and reserve space for web fonts with size-adjust
   - Avoid injecting content above existing content after load

3. INP (Interaction to Next Paint):
   - Debounce expensive event handlers (search, filter, scroll)
   - Use startTransition for non-urgent state updates
   - Move heavy computations to Web Workers or useMemo
   - Lazy load below-fold components with dynamic() and suspense

4. General:
   - Add next/script with strategy='lazyOnload' for analytics and third-party scripts
   - Enable ISR or static generation for pages that don't need real-time data
   - Add Cache-Control headers for static assets
   - Configure image remotePatterns in next.config.ts

DO NOT:
- Remove existing functionality
- Change the visual design
- Remove analytics or tracking scripts (just defer them)

Replace these variables

VariableReplace with
[TARGET_LCP]Target LCP in seconds (e.g., under 2.5s)

Tips for best results

Run Lighthouse in Chrome DevTools before and after to measure improvement.

The biggest LCP win is usually adding priority to your hero image and using next/font.

Follow-up prompts

Add performance monitoring

Add real-user monitoring with web-vitals library that sends CWV metrics to an analytics endpoint.

Related prompts