Instruction file imported from Abdi1717/LifeFlow4 (
.cursor/rules/performance_optimization.mdc). Copyright stays with the author.
Next.js Performance Optimization
-
Development Speed Optimization
- Use
npm run dev:fast(with environment flags) for rapid development iterations - Configure
.npmrcwith development-focused optimizations - Optimize
tsconfig.jsonwith performance-focused watch options - Use the cache cleaning script (
npm run clean) when encountering strange compilation errors
- Use
-
Configuration Best Practices
- Remove unsupported or deprecated experimental features from
next.config.js - Use only well-tested, supported features specific to your Next.js version
- Follow the pattern:
reactStrictMode: falsefor development,truefor production - Optimize image formats with
images.formats: ['image/avif', 'image/webp']
- Remove unsupported or deprecated experimental features from
-
Code Splitting
- Use dynamic imports with the
lazyImportutility function fromlib/utils.ts - Split large components, especially on route-specific pages
- Follow the pattern:
const HeavyComponent = lazyImport( () => import('@/components/heavy-component'), { ssr: false, displayName: 'HeavyComponent' } ); - Use dynamic imports with the
-
Context Provider Optimization
- Load context providers only when needed by specific routes
- Wrap route-specific providers in dynamic imports
- Keep common providers in shared layouts
- Avoid deep provider nesting with composition where possible
-
ShadCN UI Component Rules
- Follow this pattern for selects with "All" options (avoid empty string values):
<Select value={filter} onValueChange={setFilter}> <SelectContent> {/* ✅ DO: Use a non-empty string like "all" */} <SelectItem value="all">All Items</SelectItem> {/* ❌ DON'T: Use empty string values */} <SelectItem value="">All Items</SelectItem> </SelectContent> </Select>- Match state handling for these components:
const [filter, setFilter] = useState('all'); // Filter logic using the value const filtered = useMemo(() => { return items.filter(item => filter === 'all' ? true : item.category === filter ); }, [items, filter]); -
Bundle Size Reduction
- Use proper dynamic imports with the
{ ssr: false }option for client-only components - Include only necessary Tailwind CSS classes
- Avoid unnecessary dependencies, especially for basic UI needs already covered by ShadCN
- Use
next/imagefor automatic size optimization and WebP/AVIF conversion
- Use proper dynamic imports with the
-
Preventing Unnecessary Re-renders
- Memoize expensive components with
React.memo - Memoize complex calculations with
useMemo - Memoize callback functions with
useCallback - Use optimized state management: prefer
useState+useCallback+ context over Redux for most needs
- Memoize expensive components with
-
Diagnostics & Troubleshooting
- Check for "deoptimized the styling of X" warnings (normal for large dependencies)
- Use browser devtools Network tab to identify slow-loading assets
- For component performance, use React DevTools Profiler
- If app remains slow after optimizations, check for memory leaks using Chrome performance tools