Skip to content
clerk-nextjs-patterns logo

Next.js Patterns

clerk-nextjs-patterns

Advanced Next.js patterns - middleware, Server Actions, caching with Clerk.

NeverSight/skills_feed0installs214stars

SKILL.md

Full skill instructions

Next.js Patterns

For basic setup, see setup/.

Impact Levels

  • CRITICAL - Breaking bugs, security holes
  • HIGH - Common mistakes
  • MEDIUM - Optimization

References

ReferenceImpact
references/​server-vs-client.mdCRITICAL - await auth() vs hooks
references/​middleware-strategies.mdHIGH - Public-first vs protected-first
references/​server-actions.mdHIGH - Protect mutations
references/​api-routes.mdHIGH - 401 vs 403
references/​caching-auth.mdMEDIUM - User-scoped caching

Mental Model

Server vs Client = different auth APIs:

  • Server: await auth() from @clerk/​nextjs/​server (async!)
  • Client: useAuth() hook from @clerk/​nextjs (sync)

Never mix them. Server Components use server imports, Client Components use hooks.

Minimal Pattern

// Server Component
import { auth } from '@clerk/​nextjs/​server'

export default async function Page() {
  const { userId } = await auth()  // MUST await!
  if (!userId) return <p>Not signed in</​p>
  return <p>Hello {userId}</​p>
}

Common Pitfalls

SymptomCauseFix
undefined userId in Server ComponentMissing awaitawait auth() not auth()
Auth not working on API routesMissing matcherAdd `'/(api
Cache returns wrong user's dataMissing userId in keyInclude userId in unstable_cache key
Mutations bypass authUnprotected Server ActionCheck auth() at start of action
Wrong HTTP error codeConfused 401/​403401 = not signed in, 403 = no permission

See Also

  • setup/
  • managing-orgs/

Docs

Next.js SDK