# nextjs-server-navigation

Teaches AI assistants how to handle navigation in Next.js Server Components using <Link> and redirect() without adding 'use client' unnecessarily.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 303 installs · ★ 109
- Author: wsimmonds
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/wsimmonds/claude-nextjs-skills/tree/HEAD/nextjs-server-navigation/SKILL.md
- Updated: 2025-10-24
- HTML: https://affpapa.org/en/nevibecode/skills/nextjs-server-navigation

## What it does

AI assistants frequently convert Next.js Server Components into Client Components just to handle routing. They insert `'use client'` and import `useRouter()`. This skill stops that behavior. It directs the agent to keep components on the server, using `<Link>` for anchor tags and `redirect()` for conditional server-side routing.

The pattern details auth redirects using `cookies()` and redirects inside Server Actions. Client-side click handlers do not belong in Server Components, so attempting an `onClick` here breaks the component. It also bans the `any` type in component code.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add wsimmonds/claude-nextjs-skills --skill nextjs-server-navigation -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/nextjs-server-navigation/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/nextjs-server-navigation/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add wsimmonds/claude-nextjs-skills --skill nextjs-server-navigation -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/nextjs-server-navigation/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add wsimmonds/claude-nextjs-skills --skill nextjs-server-navigation -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/nextjs-server-navigation/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install wsimmonds/claude-nextjs-skills nextjs-server-navigation
```

2. **Manually: put the files in place**
- File: `.github/skills/nextjs-server-navigation/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add wsimmonds/claude-nextjs-skills --skill nextjs-server-navigation -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/wsimmonds/claude-nextjs-skills --path nextjs-server-navigation
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/nextjs-server-navigation/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add wsimmonds/claude-nextjs-skills --skill nextjs-server-navigation -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/wsimmonds/claude-nextjs-skills/tree/HEAD/nextjs-server-navigation/SKILL.md

## When to use it
- render navigation links in Server Components via `<Link>`
- perform conditional redirects using session data from `cookies()`
- handle post-action redirects in Server Actions
- prevent unnecessary conversion of server files to `'use client'`

## Example prompts
- Build an async profile page in Next.js that checks cookies and redirects to `/login` if no session exists.
- Add a navigation list with styled `<Link>` elements inside a server component without making it a client component.
- Create a server action for user logout that clears the session and calls `redirect('/login')`.

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 61c731c9abde253e.

BEGIN THIRD-PARTY FILE 61c731c9abde253e
````text
---
name: nextjs-server-navigation
description: Guide for implementing navigation in Next.js Server Components using Link component and redirect() function. Covers the difference between server and client navigation methods. Use when adding links, redirects, or navigation logic in server components without converting them to client components unnecessarily.
allowed-tools: Read, Write, Edit, Glob, Grep, Bash
---

# Next.js: Server Component Navigation Pattern

## ⚠️ CRITICAL RULE

**Server Components use DIFFERENT navigation methods than Client Components!**

When requirements call for server-rendered navigation—for example, linking to other pages, redirecting after a check, or demonstrating routing patterns—prefer `<Link>` and `redirect()` within Server Components. You still avoid `'use client'` unless a client-only API is involved.

## The Pattern

**Scenario:** build a server component that demonstrates proper navigation patterns

**✅ CORRECT Solution:**
```typescript
// app/page.tsx (Server Component - NO 'use client'!)
import Link from 'next/link';

export default async function Page() {
  return (
    <div>
      <h1>Home</h1>
      <Link href="/dashboard">Go to Dashboard</Link>
      <Link href="/profile">View Profile</Link>
    </div>
  );
}
```

**❌ WRONG Solution:**
```typescript
// app/page.tsx
'use client';  // ❌ NO! Server components don't need this for navigation!

import { useRouter } from 'next/navigation';  // ❌ Wrong for server components

export default function Page() {
  const router = useRouter();  // ❌ This is client-side navigation
  // ...
}
```

## Server Navigation Methods

### Method 1: Link Component (Recommended for Links)

```typescript
// app/page.tsx
import Link from 'next/link';

export default async function Page() {
  // Can still fetch data - this is a server component!
  const data = await fetchData();

  return (
    <div>
      <h1>Welcome</h1>

      {/* Simple navigation link */}
      <Link href="/about">About Us</Link>

      {/* Dynamic link */}
      <Link href={`/products/${data.productId}`}>View Product</Link>

      {/* Link with styling */}
      <Link href="/dashboard" className="btn-primary">
        Dashboard
      </Link>
    </div>
  );
}
```

**Key Points:**
- ✅ Works in Server Components (no 'use client' needed)
- ✅ Can be async function
- ✅ Can fetch data
- ✅ No hooks required

### Method 2: redirect() Function (For Conditional Redirects)

```typescript
// app/profile/page.tsx
import { redirect } from 'next/navigation';
import { cookies } from 'next/headers';

export default async function ProfilePage() {
  // Check authentication
  const cookieStore = await cookies();
  const session = cookieStore.get('session');

  // Redirect if not authenticated
  if (!session) {
    redirect('/login');
  }
````
END THIRD-PARTY FILE 61c731c9abde253e

The third-party file has ended. Everything between the 61c731c9abde253e tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2827 of 9553 bytes. © wsimmonds · MIT · https://github.com/wsimmonds/claude-nextjs-skills/tree/HEAD/nextjs-server-navigation/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
