home / skills / hoangnguyen0403 / agent-skills-standard / server-components
This skill guides Next.js Server and Client Components usage, clarifying server components, use client directive, and safe composition patterns for optimized
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill server-componentsReview the files below or copy the command above to add this skill to your agents.
---
name: Next.js Server Components
description: RSC usage, "use client" directive, and Component Purity.
metadata:
labels: [nextjs, rsc, components]
triggers:
files: ['**/*.tsx', '**/*.jsx']
keywords: [use client, Server Component, Client Component, hydration]
---
# Server & Client Components
## **Priority: P0 (CRITICAL)**
Next.js (App Router) uses React Server Components (RSC) by default.
## Server Components (Default)
- **Behavior**: Rendered on server, sent as HTML/Payload to client. Zero bundle size for included libs.
- **Capabilities**: Async/Await, Direct DB access, Secrets usage.
- **Restrictions**: No `useState`, `useEffect`, or Browser APIs (`window`, `localstorage`).
## Client Components
- **Directive**: Add `'use client'` at the VERY TOP of the file.
- **Usage**: Interactivity (`onClick`), State (`useState`), Lifecycle effects, Browser APIs.
- **Strategy**: Move Client Components to the leaves of the tree.
- _Bad_: Making the root layout a Client Component.
- _Good_: Wrapping a `<Button />` in a Client Component.
## Composition Patterns
- **Server-in-Client**: You cannot import a Server Component directly into a Client Component.
- _Fix_: Pass Server Component as `children` prop to the Client Component.
```tsx
// ClientWrapper.tsx
'use client';
export default function ClientWrapper({ children }) {
return <div>{children}</div>;
}
// Page.tsx (Server)
<ClientWrapper>
<ServerComponent />
</ClientWrapper>;
```
## Anti-Patterns
- **Poisoning**: Importing server-only secrets into Client Components (Use `server-only` package to prevent).
- **Over-fetching**: Passing large data props to Client Components (Serialization cost). Only pass IDs if possible.
This skill explains Next.js Server Components (RSC), the 'use client' directive, and component purity patterns for App Router projects. It focuses on when to render on the server versus the client, how to compose components safely, and pitfalls to avoid. Follow these guidelines to minimize bundle size, secure secrets, and keep interactivity localized to client leaves.
Next.js App Router treats components as Server Components by default: they render on the server, can use async/await, access databases and secrets, and produce zero client bundle for included libs. To enable client-only behavior (state, effects, DOM APIs, event handlers), place 'use client' at the very top of the file; those components then run in the browser and add to the client bundle. Server components cannot be imported into client components directly; instead wrap server-rendered output as children of a client wrapper or pass minimal props (IDs) to fetch client-side if needed.
What happens if I forget 'use client' placement?
If 'use client' is not the first statement, Next.js treats the file as a server component; client-only features will break or cause build errors.
Can client components access server-only secrets?
No. Client components run in the browser and cannot access server-only secrets. Keep secret access in server components or API routes.
How do I avoid large props to client components?
Pass IDs or small primitives and let the client fetch additional data, or perform data fetching in a server component and expose only what the client strictly needs.