zebbern/claude-code-guide / skills/nextjs-developer
nextjs-developer
UnverifiedUse when building Next.js 14+ applications with App Router, server components, or server actions. Invoke for full-stack features, performance optimization, SEO implementation, production deployment.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.8 KB |
| references/app-router.md | markdown | 6.9 KB |
| references/data-fetching.md | markdown | 10.3 KB |
| references/deployment.md | markdown | 10.5 KB |
| references/server-actions.md | markdown | 9.6 KB |
| references/server-components.md | markdown | 8.2 KB |
references/server-components.md
View on GitHub# React Server Components
## Server Components (Default)
```tsx
// app/page.tsx - Server Component by default
import { db } from "@/lib/db"
export default async function Page() {
// Data fetching in Server Component
const users = await db.user.findMany()
return (
<div>
<h1>Users</h1>
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
)
}
```
## Benefits of Server Components
- **Zero bundle size** - Server Components don't add JavaScript to client bundle
- **Direct backend access** - Query databases, read files, use secrets
- **Automatic code splitting** - Only Client Components add to bundle
- **Streaming** - Send UI progressively as data loads
- **No client-side waterfalls** - Fetch all data in parallel on server
## Client Components
```tsx
// components/counter.tsx
"use client" // Required directive
import { useState } from "react"
export function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>
}
```
## When to Use Client Components
Use `'use client'` when you need:
- **Interactivity** - onClick, onChange, event handlers
- **State** - useState, useReducer
- **Effects** - useEffect, useLayoutEffect
- **Browser APIs** - localStorage, window, document
- **Custom hooks** - Any hook using client-only features
- **Class components** - Component lifecycle methods
## Composition Pattern
```tsx
// app/page.tsx - Server Component
import { ClientWrapper } from "./client-wrapper"
import { db } from "@/lib/db"
export default async function Page() {
const data = await db.query()
return (
<div>
{/* Server Component content */}
<h1>Server Content</h1>
{/* Pass data to Client Component */}
<ClientWrapper initialData={data}>
{/* Server Component as children */}
<ServerSidebar />
</ClientWrapper>
</div>
)
}
// components/client-wrapper.tsx
;("use client")
export function ClientWrapper({
children,
initialData,
}: {
children: React.ReactNode
initialData: Data
}) {
const [data, setData] = useState(initialData)
return (
<div>
{/* Client Component UI */}
<button onClick={() => refresh()}>Refresh</button>
{/* Server Component children */}
{children}
</div>
)
}
```
## Streaming with Suspense
```tsx
// app/page.tsx
import { Suspense } from "react"
import { SlowComponent } from "./slow-component"
import { FastComponent } from "./fast-component"
export default function Page() {
return (
<div>
{/* Renders immediately */}
<FastComponent />
{/* Shows fallback while loading */}
<Suspense fallback={<div>Loading...</div>}>
<SlowComponent />
</Suspense>
</div>
)
}
// components/slow-component.tsx
async function getData() {
await new Promise((resolve) => setTimeout(resolve, 3000))
return { data: "Loaded!" }
}
export async function SlowComponent() {
const data = await getData()
return <div>{data.data}</div>
}
```
## Parallel Data Fetching
```tsx
// app/dashboard/page.tsx
async function getUser() {
return fetch("https://api.example.com/user")
}
async function getPosts() {
return fetch("https://api.example.com/posts")
}
export default async function Dashboard() {
// Fetch in parallel
const [user, posts] = await Promise.all([getUser(), getPosts()])
return (
<div>
<UserProfile user={user} />
<PostsList posts={posts} />
</div>
)
}
```
## Sequential Data Fetching
```tsx
// app/artist/[id]/page.tsx
async function getArtist(id: string) {
return fetch(`https://api.example.com/artists/${id}`)
}
async function getAlbums(artistId: string) {
return fetch(`https://api.example.com/artists/${artistId}/albums`)
}
export default async function ArtistPage({ params }: { params: { id: string } }) {
// Sequential: albums depends on artist
const artist = await getArtist(params.id)
const albums = await getAlbums(artist.id)
return (
<div>
<h1>{artist.name}</h1>
<Albums albums={albums} />
</div>
)
}
```
## Preloading Data
```tsx
// lib/data.ts
import { cache } from "react"
export const getUser = cache(async (id: string) => {
return db.user.findUnique({ where: { id } })
})
// components/user-profile.tsx
export async function UserProfile({ userId }: { userId: string }) {
const user = await getUser(userId)
return <div>{user.name}</div>
}
// app/page.tsx
import { getUser } from "@/lib/data"
import { UserProfile } from "@/components/user-profile"
export default async function Page() {
// Preload
getUser("123")
return (
<div>
{/* This will use cached result */}
<UserProfile userId="123" />
</div>
)
}
```
## Server Component Patterns
### Pattern: Layout with Data Fetching
```tsx
// app/dashboard/layout.tsx
import { auth } from "@/lib/auth"
import { db } from "@/lib/db"
export default async function DashboardLayout({ children }: { children: React.ReactNode }) {
const session = await auth()
const user = await db.user.findUnique({ where: { id: session.userId } })
return (
<div>
<Sidebar user={user} />
<main>{children}</main>
</div>
)
}
```
### Pattern: Conditional Client Components
```tsx
// app/page.tsx
import { ClientComponent } from "./client-component"
export default async function Page() {
const data = await fetchData()
// Only render Client Component when needed
if (data.requiresInteractivity) {
return <ClientComponent data={data} />
}
return <div>{data.content}</div>
}
```
### Pattern: Server Component with Client Island
```tsx
// app/blog/[slug]/page.tsx
import { LikeButton } from "./like-button"
export default async function BlogPost({ params }: { params: { slug: string } }) {
const post = await getPost(params.slug)
return (
<article>
{/* Server-rendered content */}
<h1>{post.title}</h1>
<div dangerouslySetInnerHTML={{ __html: post.content }} />
{/* Client island for interactivity */}
<LikeButton postId={post.id} initialLikes={post.likes} />
</article>
)
}
```
## Context in Server/Client Components
```tsx
// app/providers.tsx
"use client"
import { ThemeProvider } from "next-themes"
export function Providers({ children }: { children: React.ReactNode }) {
return <ThemeProvider>{children}</ThemeProvider>
}
// app/layout.tsx
import { Providers } from "./providers"
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<Providers>{children}</Providers>
</body>
</html>
)
}
```
## Third-Party Components
```tsx
// components/carousel-wrapper.tsx
"use client"
import { Carousel } from "third-party-carousel"
export function CarouselWrapper({ items }: { items: Item[] }) {
return <Carousel items={items} />
}
// app/page.tsx
import { CarouselWrapper } from "@/components/carousel-wrapper"
export default async function Page() {
const items = await fetchItems()
return <CarouselWrapper items={items} />
}
```
## Edge Runtime
```tsx
// app/api/route.ts
export const runtime = "edge"
export async function GET() {
return new Response("Hello from Edge!")
}
// app/page.tsx
export const runtime = "edge"
export default async function Page() {
return <div>Edge-rendered page</div>
}
```
## Quick Reference
| Capability | Server Component | Client Component |
| -------------- | ------------------ | ---------------------- |
| Data fetching | ✅ Yes | ⚠️ Use SWR/React Query |
| Backend access | ✅ Yes (DB, files) | ❌ No |
| Event handlers | ❌ No | ✅ Yes |
| State/Effects | ❌ No | ✅ Yes |
| Browser APIs | ❌ No | ✅ Yes |
| Bundle size | 0 KB | Adds to bundle |
| Streaming | ✅ Yes | ❌ No |
## Best Practices
1. **Default to Server Components** - Only use 'use client' when needed
2. **Move Client Components down** - Push them to leaves of component tree
3. **Pass data down** - Fetch in Server Components, pass to Client Components
4. **Use composition** - Nest Server Components inside Client Components via children
5. **Cache expensive operations** - Use React cache() for deduplication
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 117k | Updated | Claude CodeCodex | Unverified | Install |