| name | clerk-performance-tuning |
| description | Optimize Clerk authentication performance.
Use when improving auth response times, reducing latency,
or optimizing Clerk SDK usage.
Trigger with phrases like "clerk performance", "clerk optimization",
"clerk slow", "clerk latency", "optimize clerk".
|
| allowed-tools | Read, Write, Edit, Grep |
| version | 1.14.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","clerk","performance","authentication"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
Clerk Performance Tuning
Overview
Optimize Clerk authentication for best performance. Covers middleware optimization, user data caching, token handling, lazy loading, and edge runtime configuration.
Prerequisites
- Clerk integration working
- Performance monitoring in place (Lighthouse, Web Vitals)
- Understanding of Next.js rendering strategies
Instructions
Step 1: Optimize Middleware (Skip Static Assets)
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
const isPublicRoute = createRouteMatcher(['/', '/sign-in(.*)', '/sign-up(.*)', '/api/webhooks(.*)'])
export default clerkMiddleware(async (auth, req) => {
if (!isPublicRoute(req)) {
await auth.protect()
}
})
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico)).*)',
'/(api|trpc)(.*)',
],
}
Step 2: Cache User Data
import { auth, currentUser } from '@clerk/nextjs/server'
import { cache } from 'react'
export getAuthUser = ( () => {
{ userId } = ()
(!userId)
()
})