What are Server Components?
React Server Components (RSC) are a new paradigm for building React applications. In NextJS App Router, all components are Server Components by default.Server Components render exclusively on the server (or at build time) and never ship JavaScript to the client.
Key Benefits
Zero Bundle Size
Server Components don’t add to client JavaScript bundle
Direct Backend Access
Access databases, file systems, and APIs directly
Better Performance
Less JavaScript to download and parse
Automatic Code Splitting
Only client components are split and loaded
Server vs Client Components
Let’s examine the difference with real examples from the course:Server Component Example
components/RSCDemo.js
Server Component Characteristics
Server Component Characteristics
- Can be
asyncfunctions - Can access backend resources directly
- Cannot use React hooks (useState, useEffect, etc.)
- Cannot use browser APIs
- Render once on the server
- Console logs appear in terminal, not browser
Client Component Example
components/ClientDemo.js
When to Use Each
Use Server Components for:
Use Client Components for:
Composing Server and Client Components
You can nest Server Components inside Client Components, but only as props:app/page.js
Data Fetching with Server Components
Server Components can beasync and fetch data directly:
components/DataFetchingDemo.js
Notice how we import
fs from Node.js directly! This code only runs on the server, so we have full access to server-side APIs.Real-World Example: Meals Page
Let’s look at a complete example from the Foodies app:app/meals/page.js
Why this is a Server Component
Why this is a Server Component
- No
'use client'directive - Declared as
asyncfunction - Directly calls
getMeals()which queries a database - No interactivity or browser APIs needed
- Renders once on server, sent as HTML to client
Streaming with Suspense
Server Components work seamlessly with React Suspense for progressive rendering:app/meals/page.js
Error Handling
Create anerror.js file to handle errors in Server Components:
app/meals/error.js
Loading States
Create aloading.js file for automatic loading UI:
app/meals/loading.js
NextJS automatically wraps your page in a Suspense boundary using
loading.js as the fallback.Component Decision Tree
Best Practices
Keep Client Components Small
Keep Client Components Small
Only add
'use client' to the smallest components that need it. This minimizes client JavaScript bundle size.Fetch Data as Close as Possible
Fetch Data as Close as Possible
Fetch data in the component that needs it, not in a parent layout. This enables better streaming and parallel data fetching.
Use Server Components by Default
Use Server Components by Default
Start with Server Components and only add
'use client' when you need interactivity or browser APIs.Leverage Composition
Leverage Composition
Pass Server Components as children to Client Components to preserve their server-only nature.
Performance Comparison
| Aspect | Client Components | Server Components |
|---|---|---|
| Bundle Size | Adds to bundle | Zero bundle impact |
| Initial Load | Slower (more JS) | Faster (less JS) |
| Data Fetching | Client-side fetch | Server-side fetch |
| Interactivity | Full interactivity | None |
| Re-rendering | On state changes | Only on navigation |
| Backend Access | Via APIs only | Direct access |
Next Steps
Server Actions
Learn how to handle form submissions and mutations
Data Fetching Patterns
Explore advanced data fetching strategies