Three different ways to fetch data in Next.js - Server Component, Client Component, and Server Actions
Error
Failed to fetch health status
How it works:
Click the button to check backend health
How it works:
Click the button to check backend health
How it works:
| Feature | Server Component | Client Component | Server Action |
|---|---|---|---|
| Where it runs | Server only | Browser only | Server only |
| When it fetches | During page render | On button click | On button click |
| API endpoint visible? | No | Yes (in network tab) | No (hidden) |
| Best for | Initial page load, SEO | Interactive fetching | Mutations, secure operations |
| Loading state | Suspense boundary | useState | useTransition |
| JavaScript bundle | Smaller | Larger | Smaller |
| Use case | Static content, data display | User interactions, real-time | Form submissions, updates |
Server Components (Blue)
Best for initial page load and SEO. Data is fetched on the server and sent as HTML. No JavaScript needed for rendering.
Client Components (Green)
Best for interactive features that need state, effects, or event handlers. Fetches data from the browser using API routes.
Server Actions (Purple)
Best for mutations and secure operations. Called from client components but run on the server. API endpoints are completely hidden from users.
Open your browser's Network tab and compare the requests:
/api/health