react-best-practices
React & Next.js Performance Patterns
Performance optimization guide for React and Next.js applications, based on Vercel Engineering practices. 8 categories organized by impact.
When to Apply
- Writing new React components or Next.js pages
- Implementing data fetching (client or server-side)
- Reviewing code for performance issues
- Optimizing bundle size or load times
Quick Reference
1. Async Patterns (CRITICAL)
- Prevent waterfall chains in API routes -- start promises early, await late
- Defer await until needed -- move await into branches that use it
- Dependency-based parallelization --
Promise.all()with.then()chaining - Strategic Suspense boundaries -- stream content with
<Suspense>
2. Bundle Optimization (CRITICAL)
references/bundle-optimization.md
- Avoid barrel file imports -- import directly from source files
- Conditional module loading -- load only when feature is activated
- Defer non-critical third-party libraries -- load after hydration
- Dynamic imports for heavy components --
next/dynamicwithssr: false - Preload on user intent -- preload on hover/focus
3. Server-Side Performance (HIGH)
references/server-performance.md
after()for non-blocking operations -- logging, analytics after response- Authenticate Server Actions -- treat as public endpoints
- Cross-request LRU caching -- share data across sequential requests
React.cache()deduplication -- per-request with primitive args- Avoid duplicate RSC serialization -- transform in client, not server
- Parallel fetching via composition -- restructure component tree
- Minimize serialization at boundaries -- pass only needed fields
4. Client-Side Patterns (MEDIUM-HIGH)
- Deduplicate global event listeners --
useSWRSubscription - Version and minimize localStorage -- schema versioning, try-catch
- Passive event listeners --
{ passive: true }for scroll performance - SWR for automatic deduplication -- caching and revalidation
5. Re-render Optimization (MEDIUM)
references/rerender-optimization.md
- Defer state reads to usage point -- read in callbacks, not render
- Narrow effect dependencies -- use primitives, not objects
- Derive state during render -- no state + effect for computed values
- Functional setState -- stable callbacks, no stale closures
- Hoist default non-primitive props -- stable defaults for
memo() - Extract to memoized components -- skip computation with early returns
- Interaction logic in event handlers -- not state + effect
- useRef for transient values -- avoid re-render on frequent updates
6. Rendering Performance (MEDIUM)
references/rendering-performance.md
- Animate SVG wrapper -- hardware-accelerated CSS on
<div>, not<svg> - CSS
content-visibility: auto-- defer off-screen rendering - Hoist static JSX -- extract constants outside components
- Prevent hydration mismatch -- inline script for client-only data
useTransitionover manual loading states -- built-in pending state
7. JavaScript Performance (LOW-MEDIUM)
- Avoid layout thrashing -- batch DOM reads and writes
- Cache repeated function calls -- module-level Map
- Cache storage API calls -- in-memory cache for localStorage/cookies
- Build index Maps for lookups -- O(1) instead of O(n)
.find() - Loop for min/max -- O(n) instead of O(n log n) sort
8. Advanced Patterns (LOW)
references/advanced-patterns.md
- Store event handlers in refs -- stable effect subscriptions
- Initialize app once per load -- module-level guard
More from jgamaraalv/ts-dev-kit
bullmq
BullMQ queue system reference for Redis-backed job queues, workers, flows, and schedulers. Use when: (1) creating queues and workers with BullMQ, (2) adding jobs (delayed, prioritized, repeatable, deduplicated), (3) setting up FlowProducer parent-child job hierarchies, (4) configuring retry strategies, rate limiting, or concurrency, (5) implementing job schedulers with cron/interval patterns, (6) preparing BullMQ for production (graceful shutdown, Redis config, monitoring), or (7) debugging stalled jobs or connection issues
46owasp-security-review
Review code and architectures against the OWASP Top 10:2025 — the ten most critical web application security risks. Use when: (1) reviewing code for security vulnerabilities, (2) auditing a feature or codebase against OWASP categories, (3) providing remediation guidance for identified vulnerabilities, (4) writing new code and needing secure coding patterns. Triggers: 'review for security', 'OWASP audit', 'check for vulnerabilities','security checklist', 'is this code secure', 'security review', 'fix vulnerability'.
42ioredis
ioredis v5 reference for Node.js Redis client — connection setup, RedisOptions, pipelines, transactions, Pub/Sub, Lua scripting, Cluster, and Sentinel. Use when: (1) creating or configuring Redis connections (standalone, cluster, sentinel), (2) writing Redis commands with ioredis (get/set, pipelines, multi/exec), (3) setting up Pub/Sub or Streams, (4) configuring retryStrategy, TLS, or auto-pipelining, (5) working with Redis Cluster options (scaleReads, NAT mapping), or (6) debugging ioredis connection issues. Important: use named import `import { Redis } from 'ioredis'` for correct TypeScript types with NodeNext.
35nextjs-best-practices
Next.js App Router best practices — file conventions, RSC boundaries, data patterns, async APIs, metadata, error handling, route handlers, image/font optimization, bundling. Use when writing, reviewing, or debugging Next.js App Router code.
29ui-ux-guidelines
Review UI code for Web Interface Guidelines compliance. Use when asked to review UI, check accessibility, audit design, review UX, or check against best practices.
26service-worker
Service Worker API implementation guide — registration, lifecycle management, caching strategies, push notifications, and background sync. Use when: (1) creating or modifying service worker files (sw.js), (2) implementing offline-first caching (cache-first, network-first, stale-while-revalidate), (3) setting up push notifications or background sync, (4) debugging service worker registration, scope, or update issues, (5) implementing navigation preload, (6) user mentions 'service worker', 'sw.js', 'offline support', 'cache strategy', 'push notification', 'background sync', 'workbox alternative', or 'PWA caching'.
25