AI summary by TwinMind
Why is Next.js so slow??
Theo - t3․gg
51m 31s
Introduction and Context
- Discussion sparked by DAX's criticism of server components causing ~1 second page navigations
- Most server component apps in wild feel slow/rough
- Problem attributed more to developer understanding than React/Next.js teams
- Need deep understanding of server component behavior for optimal performance
Historical Web Architecture Overview
- Traditional Web (Pre-SPA)
- User requests HTML → server processes → sends HTML response
- Link clicks/form submissions trigger new server requests
- Big delay between user action and browser response
- Server controls all content changes, not JavaScript
- Only content changes: server responses or marquee tags
- Single Page Apps (SPA) Evolution
- User requests HTML once regardless of URL
- Server sends same HTML with script tags for all routes
- JavaScript handles all dynamic behavior and routing
- Client-side navigation without server requests
- API calls separate from HTML delivery
- Clear client/server division
SPA Performance Characteristics
- Immediate Response Benefits
- User clicks → instant feedback (<50ms feels fast, >150ms feels unresponsive)
- JavaScript hijacks routing/history for instant navigation
- Loading states controlled by developer, not browser
- App-like experience vs traditional site behavior
- Loading State Management
- Component-level loading states during data fetching
- User sees something immediately on interaction
- Content may take longer to fully load (2.5+ seconds with waterfalls)
- Still feels faster due to immediate visual feedback
Server Components Architecture
- URLs matter again - different content for different routes
- CDN checks for cached static pages first
- If not cached → forwards to server → generates page → sends to user
- Requires smart CDN for optimal performance
- Without CDN, performs much worse than SPAs
Common Server Component Problems
- Navigation Issues
- Each navigation may require server round trip
- No instant navigation like SPAs
- Developers don't understand server-side patterns
- Missing loading states cause poor UX
- Demo Analysis
- Mock dashboard app with intentional 400ms delay
- First page: 576ms load time (feels terrible)
- Second/third pages: instant (static, no network requests)
- Problem: no immediate feedback on slow page
Solutions and Best Practices
- Loading.tsx File Pattern
- Simplest way to add loading states in Next.js
- Creates static HTML version with loading state
- Allows immediate response while server generates content
- Alternative: Suspense boundaries in components
- Next.js Rendering Hierarchy
- Root layout → dashboard layout → loading.tsx → page content
- Build process identifies static vs dynamic content
- Loading states create barriers for static generation
- Dynamic content wrapped in Suspense for streaming
- Advanced Features (Experimental)
- Partial Pre-rendering (PPR) - generates HTML up to Suspense boundaries
- Dynamic IO - smart async boundary detection
- Both still experimental but promising for future
Performance Comparison Analysis
- Traditional MPA: Fast content load (0.7s) but no immediate response
- Traditional SPA: Immediate response (0.05s) but slower content (2.5s)
- Bad RSC Implementation: Slow response (1.2s) with no immediate feedback
- Good RSC Implementation: Immediate response + fast content load
Key Performance Metrics
- TTFB (Time to First Byte) vs FCP (First Contentful Paint)
- Goal: minimize red bar (bad browser loading) and yellow bar (custom loading)
- RSCs done right achieve best of both worlds
- Critical: user feedback within 100ms for instant feel
HTMX Discussion
- Disagrees with HTMX stance on browser loading bars
- Browser default loading UX is poor
- Custom loading states significantly better than browser defaults
- Locality of response matters - feedback must appear where user clicked
- Rage clicks common when no immediate feedback provided
Technical Implementation Details
- Force dynamic flag required for pages without user-specific content
- Build output shows static (○) vs dynamic (λ) vs PPR (◐) routes
- Loading.js files embedded in client bundle for instant access
- Proper suspense placement critical for static generation
