Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Advanced Clerk auth patterns for Next.js: Server Actions, middleware, caching, and App Router integration.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/server-vs-client.md
1# Server vs Client (CRITICAL)23## CRITICAL: Always `await auth()`45```tsx6// WRONG7const { userId } = auth(); // undefined!89// CORRECT10const { userId } = await auth();11```1213## When to Use1415- **Server Components** - Initial load, SEO, DB queries16- **Client Components** - Interactive UI, sign out, token fetching1718## Import Rules1920```tsx21// Server Components22import { auth, currentUser } from '@clerk/nextjs/server';2324// Client Components25'use client';26import { useAuth, useUser } from '@clerk/nextjs';27```2829## Server Component3031```tsx32import { auth, currentUser } from '@clerk/nextjs/server';3334export default async function DashboardPage() {35const { isAuthenticated } = await auth();36if (!isAuthenticated) return <div>Please sign in</div>;3738const user = await currentUser();39return <h1>Welcome, {user?.firstName}!</h1>;40}41```4243> **Core 2 ONLY (skip if current SDK):** `isAuthenticated` is not available. Use `const { userId } = await auth()` and check `if (!userId)` instead.4445## Client Component4647```tsx48'use client';49import { useUser, useAuth } from '@clerk/nextjs';5051export function UserDashboard() {52const { isLoaded, isSignedIn, user } = useUser();53const { signOut } = useAuth();5455if (!isLoaded) return <div>Loading...</div>;56if (!isSignedIn) return <div>Not signed in</div>;5758return (59<div>60<p>Hello, {user.firstName}!</p>61<button onClick={() => signOut()}>Sign out</button>62</div>63);64}65```6667## Hybrid Pattern6869```tsx70// Server: fetch initial data71import { currentUser } from '@clerk/nextjs/server';72import { ProfileForm } from './ProfileForm';7374export default async function ProfilePage() {75const user = await currentUser();76if (!user) return <div>Please sign in</div>;77return <ProfileForm initialData={{ firstName: user.firstName }} />;78}7980// Client: handle interactions81'use client';82import { useUser } from '@clerk/nextjs';8384export function ProfileForm({ initialData }) {85const { user } = useUser();86return <form>...</form>;87}88```8990## Conditional Rendering9192Use `<Show>` for client-side conditional rendering based on auth state:9394```tsx95import { Show } from '@clerk/nextjs';9697<Show when="signed-in" fallback={<div>Please sign in</div>}>98<Dashboard />99</Show>100```101102> **Core 2 ONLY (skip if current SDK):** Use `<SignedIn>` and `<SignedOut>` components instead of `<Show>`.103104[Docs](https://clerk.com/docs/reference/nextjs/auth)105