Core Web Vitals
Both applications automatically track Core Web Vitals metrics:- LCP (Largest Contentful Paint) - Loading performance
- INP (Interaction to Next Paint) - Interactivity
- CLS (Cumulative Layout Shift) - Visual stability
- FCP (First Contentful Paint) - Initial rendering
- TTFB (Time to First Byte) - Server response time
Next.js Web Vitals (@workspace/web)
The Next.js app uses theuseReportWebVitals hook:
React SPA Web Vitals (@workspace/spa)
The SPA uses theweb-vitals library with OpenTelemetry integration:
reportWebVitals() on key pages:
Bundle Analysis
Next.js Bundle Analysis
Analyze your Next.js bundle to identify optimization opportunities:- Module sizes
- Tree-shaking effectiveness
- Code splitting efficiency
- Duplicate dependencies
React SPA Bundle Analysis
The SPA usesrollup-plugin-visualizer for bundle analysis:
html/visualizer-stats.html to explore:
- Chunk sizes
- Module dependencies
- Tree map visualization
- Redundant code
Optimization Techniques
Code Splitting
- Next.js
- React SPA
Use dynamic imports with
next/dynamic:Image Optimization
- Next.js
- React SPA
Use the optimized Image component:Next.js automatically:
- Serves WebP/AVIF formats
- Resizes images on-demand
- Lazy loads by default
- Prevents CLS with explicit dimensions
Font Optimization
- Next.js
- React SPA
Use Benefits:
next/font for automatic font optimization:- Zero layout shift
- Self-hosted fonts
- Automatic subsetting
- Preloading
React Compiler
Both applications use the React Compiler for automatic memoization:- Memoizes components
- Optimizes re-renders
- Reduces manual
useMemo/useCallbackusage - Improves overall performance
Package Optimization
Optimize dependencies to reduce bundle size:Tree-shaking
Tree-shaking
Ensure your imports support tree-shaking:
Dynamic imports for heavy dependencies
Dynamic imports for heavy dependencies
Load heavy libraries only when needed:
Analyze dependencies
Analyze dependencies
Check for duplicate or outdated packages:
Caching Strategies
Next.js
Leverage Next.js caching mechanisms:Service Workers (SPA)
The SPA uses Vite PWA for offline caching:Performance Monitoring Tools
Capo.js
Optimize HTML
<head> element ordering for better performanceUnlighthouse
Scan and measure performance across all pages
Web Vitals
Learn about Core Web Vitals and optimization
Web.dev Performance
Comprehensive performance learning guide
Performance Checklist
Measure baseline
- Run Lighthouse audits
- Check Web Vitals scores
- Analyze bundle sizes
- Review network waterfalls
Optimize critical path
- Minimize blocking resources
- Inline critical CSS
- Preload key resources
- Use font-display: swap
Reduce bundle size
- Analyze and remove unused code
- Code-split large dependencies
- Use dynamic imports
- Enable compression (gzip/brotli)
Optimize images
- Use modern formats (WebP/AVIF)
- Implement lazy loading
- Add explicit dimensions
- Use responsive images
Best Practices
Prioritize above-the-fold content
Prioritize above-the-fold content
Load critical content first:
- Use
priorityprop on hero images - Inline critical CSS
- Defer non-critical JavaScript
- Preload fonts and key assets
Implement progressive enhancement
Implement progressive enhancement
Build a solid foundation:
- Start with HTML
- Layer on CSS
- Enhance with JavaScript
- Ensure core functionality without JS
Use proper caching headers
Use proper caching headers
Configure aggressive caching for static assets:And short-lived caching for dynamic content:
Monitor real user metrics
Monitor real user metrics
Track actual user experience:
- Set up RUM (Real User Monitoring)
- Analyze field data, not just lab data
- Segment by device, network, geography
- Act on performance budgets
Resources
Next.js Performance
Official Next.js optimization guide
Vite Performance
Vite performance best practices
web.dev
Google’s web development best practices
Grafana Observability
Monitor performance with Grafana
