Next.js website for Rocky Mountain Vending company featuring: - Product catalog with Stripe integration - Service areas and parts pages - Admin dashboard with Clerk authentication - SEO optimized pages with JSON-LD structured data Co-authored-by: Cursor <cursoragent@cursor.com>
4.4 KiB
Lighthouse Testing Guide
This guide explains how to test Lighthouse performance scores locally and achieve 100% scores across all categories.
Prerequisites
-
Install dependencies:
npm install # or pnpm install -
Ensure Chrome/Chromium is installed (required for Lighthouse CLI)
Running Lighthouse Tests
Test Production Build (Recommended)
This is the most accurate way to test Lighthouse scores, as it tests the actual production-optimized bundle:
npm run lighthouse:build
This command will:
- Build the production bundle (
next build) - Start the production server (
next start) - Run Lighthouse tests on multiple pages
- Display scores and metrics
- Stop the server automatically
Test Dev Server (Less Accurate)
For quick testing during development (less accurate scores):
npm run lighthouse:dev
Note: Dev server scores are typically lower than production scores due to unoptimized code and development overhead.
Understanding the Results
Score Categories
Lighthouse tests four main categories, each scored 0-100:
- Performance: Measures page load speed and Core Web Vitals
- Accessibility: Checks for accessibility best practices
- Best Practices: Validates modern web development practices
- SEO: Evaluates search engine optimization
Core Web Vitals
The script also reports these critical metrics:
- FCP (First Contentful Paint): Time until first content appears (target: < 1.8s)
- LCP (Largest Contentful Paint): Time until main content loads (target: < 2.5s)
- CLS (Cumulative Layout Shift): Visual stability measure (target: < 0.1)
- TBT (Total Blocking Time): Time page is blocked from interaction (target: < 200ms)
- SI (Speed Index): How quickly content is visually displayed (target: < 3.4s)
Target Scores
For 100% scores, all categories must achieve:
- Performance: 100/100
- Accessibility: 100/100
- Best Practices: 100/100
- SEO: 100/100
Performance Optimizations Implemented
Image Optimization
- Next.js Image optimization enabled (
unoptimized: false) - Automatic WebP/AVIF format conversion
- Responsive image sizes
- Proper width/height attributes to prevent layout shift
- Lazy loading for below-the-fold images
Third-Party Scripts
- LeadConnector script loaded with
afterInteractivestrategy - Resource hints (preconnect/dns-prefetch) for external domains
- Scripts don't block initial page render
Caching Headers
- Static assets cached for 1 year
- Images cached with immutable flag
- Fonts cached for optimal performance
Bundle Optimization
- Code splitting enabled
- Tree shaking for unused code
- Bundle analyzer available (
npm run analyze)
Troubleshooting
Low Performance Scores
- Check image optimization: Ensure images are using Next.js Image component
- Verify production build: Always test production builds, not dev server
- Check bundle size: Run
npm run analyzeto identify large dependencies - Review third-party scripts: Ensure they're loaded asynchronously
Images Not Optimizing
If images aren't being optimized:
- Ensure
unoptimized: falseinnext.config.mjs - Check that images are using Next.js
Imagecomponent, not<img>tags - Verify image domains are in
remotePatternsif using external images
Layout Shift Issues
- Ensure all images have explicit
widthandheightattributes - Use
fillprop with aspect-ratio containers for responsive images - Avoid dynamic content that changes layout after load
Continuous Integration
The .lighthouserc.js file is configured for Lighthouse CI. To use it:
-
Install Lighthouse CI:
npm install -g @lhci/cli -
Run CI tests:
npm run lighthouse:ci
Best Practices
- Always test production builds: Dev server scores are not representative
- Test multiple pages: Different pages may have different performance characteristics
- Monitor Core Web Vitals: These directly impact user experience and SEO
- Regular testing: Run Lighthouse tests before deploying major changes
- Bundle analysis: Regularly check bundle size with
npm run analyze