server-components
Explains Next.js Server Components, client directives, and data fetching patterns.
Install
mkdir -p .claude/skills/server-components && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/5924" && unzip -o skill.zip -d .claude/skills/server-components && rm skill.zipInstalls to .claude/skills/server-components
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
This skill should be used when the user asks about "Server Components", "Client Components", "'use client' directive", "when to use server vs client", "RSC patterns", "component composition", "data fetching in components", or needs guidance on React Server Components architecture in Next.js.Key capabilities
- →Distinguish between Server and Client component execution environments
- →Insert 'use client' directives at appropriate boundary levels
- →Map component tree structure to execution location
- →Optimize data fetching for server-side execution
How it works
Analyzes the React component tree and identifies the 'client boundary' to ensure server-side code does not leak into browser-only hooks.
Inputs & outputs
When to use server-components
- →Determining server vs client component usage
- →Implementing 'use client' directives
- →Optimizing data fetching patterns
- →Structuring component trees
About this skill
React Server Components in Next.js
Overview
React Server Components (RSC) allow components to render on the server, reducing client-side JavaScript and enabling direct data access. In Next.js App Router, all components are Server Components by default.
Server vs Client Components
Server Components (Default)
Server Components run only on the server:
// app/users/page.tsx (Server Component - default)
async function UsersPage() {
const users = await db.user.findMany() // Direct DB access
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
)
}
Benefits:
- Direct database/filesystem access
- Keep sensitive data on server (API keys, tokens)
- Reduce client bundle size
- Automatic code splitting
Client Components
Add 'use client' directive for interactivity:
// components/counter.tsx
'use client'
import { useState } from 'react'
export function Counter() {
const [count, setCount] = useState(0)
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
)
}
Use Client Components for:
useState,useEffect,useReducer- Event handlers (
onClick,onChange) - Browser APIs (
window,document) - Custom hooks with state
The Mental Model
Think of the component tree as having a "client boundary":
Server Component (page.tsx)
├── Server Component (header.tsx)
├── Client Component ('use client') ← boundary
│ ├── Client Component (child)
│ └── Client Component (child)
└── Server Component (footer.tsx)
Key rules:
- Server Components can import Client Components
- Client Components cannot import Server Components
- You can pass Server Components as
childrento Client Components
Composition Patterns
Pattern 1: Server Data → Client Interactivity
Fetch data in Server Component, pass to Client:
// app/products/page.tsx (Server)
import { ProductList } from './product-list'
export default async function ProductsPage() {
const products = await getProducts()
return <ProductList products={products} />
}
// app/products/product-list.tsx (Client)
'use client'
export function ProductList({ products }: { products: Product[] }) {
const [filter, setFilter] = useState('')
const filtered = products.filter(p =>
p.name.includes(filter)
)
return (
<>
<input onChange={e => setFilter(e.target.value)} />
{filtered.map(p => <ProductCard key={p.id} product={p} />)}
</>
)
}
Pattern 2: Children as Server Components
Pass Server Components through children prop:
// components/client-wrapper.tsx
'use client'
export function ClientWrapper({ children }: { children: React.ReactNode }) {
const [isOpen, setIsOpen] = useState(false)
return (
<div>
<button onClick={() => setIsOpen(!isOpen)}>Toggle</button>
{isOpen && children} {/* Server Component content */}
</div>
)
}
// app/page.tsx (Server)
import { ClientWrapper } from '@/components/client-wrapper'
import { ServerContent } from '@/components/server-content'
export default function Page() {
return (
<ClientWrapper>
<ServerContent /> {/* Renders on server! */}
</ClientWrapper>
)
}
Pattern 3: Slots for Complex Layouts
Use multiple children slots:
// components/dashboard-shell.tsx
'use client'
interface Props {
sidebar: React.ReactNode
main: React.ReactNode
}
export function DashboardShell({ sidebar, main }: Props) {
const [collapsed, setCollapsed] = useState(false)
return (
<div className="flex">
{!collapsed && <aside>{sidebar}</aside>}
<main>{main}</main>
</div>
)
}
Data Fetching
Async Server Components
Server Components can be async:
// app/posts/page.tsx
export default async function PostsPage() {
const posts = await fetch('https://api.example.com/posts')
.then(res => res.json())
return (
<ul>
{posts.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
)
}
Parallel Data Fetching
Fetch multiple resources in parallel:
export default async function DashboardPage() {
const [user, posts, analytics] = await Promise.all([
getUser(),
getPosts(),
getAnalytics(),
])
return (
<Dashboard user={user} posts={posts} analytics={analytics} />
)
}
Streaming with Suspense
Stream slow components:
import { Suspense } from 'react'
export default function Page() {
return (
<div>
<Header /> {/* Renders immediately */}
<Suspense fallback={<PostsSkeleton />}>
<SlowPosts /> {/* Streams when ready */}
</Suspense>
</div>
)
}
Decision Guide
Use Server Component when:
- Fetching data
- Accessing backend resources
- Keeping sensitive info on server
- Reducing client JavaScript
- Component has no interactivity
Use Client Component when:
- Using state (
useState,useReducer) - Using effects (
useEffect) - Using event listeners
- Using browser APIs
- Using custom hooks with state
Common Mistakes
- Don't add
'use client'unnecessarily - it increases bundle size - Don't try to import Server Components into Client Components
- Do serialize data at boundaries (no functions, classes, or dates)
- Do use the children pattern for composition
Resources
For detailed patterns, see:
references/server-vs-client.md- Complete comparison guidereferences/composition-patterns.md- Advanced compositionexamples/data-fetching-patterns.md- Data fetching examples
When not to use it
- →Standard client-side only React applications without Next.js App Router
- →Non-React framework architecture tasks
Prerequisites
Limitations
- →Only applies to Next.js App Router architecture
- →Cannot override hardware-level browser API requirements for Client Components
How it compares
It explicitly enforces the strict unidirectional import rules of Next.js App Router rather than treating all components as equivalent.
Compared to similar skills
server-components side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| server-components (this skill) | 1 | 7mo | No flags | Intermediate |
| nextjs-best-practices | 31 | 6mo | No flags | Intermediate |
| javascript-typescript-typescript-scaffold | 3 | 4mo | Review | Beginner |
| frontend-structure | 0 | 4mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by davepoon
View all by davepoon →You might also like
nextjs-best-practices
davila7
Next.js App Router principles. Server Components, data fetching, routing patterns.
javascript-typescript-typescript-scaffold
sickn33
You are a TypeScript project architecture expert specializing in scaffolding production-ready Node.js and frontend applications. Generate complete project structures with modern tooling (pnpm, Vite, N
frontend-structure
Kwondongkyun
Next.js App Router 프로젝트 구조 설정 시 사용. app(페이지), components(UI), features(도메인), lib(유틸), hooks 폴더 구조.
open-notebook-lm-guidelines
RainLib
Core project guidelines, architecture, and UI styling instructions for the FerrisMind (openNotebookLm) AI workspace project. Use this whenever working on the frontend or backend of this project.
nextjs-developer
zenobi-us
Expert Next.js developer mastering Next.js 14+ with App Router and full-stack features. Specializes in server components, server actions, performance optimization, and production deployment with focus on building fast, SEO-friendly applications.
landing-page-guide-v2
bear2u
Create distinctive, high-converting landing pages that combine proven conversion elements with exceptional design quality. Build beautiful, memorable landing pages using Next.js 14+ and ShadCN UI that avoid generic AI aesthetics while following the 11 essential elements framework.