jeremylongshore/claude-code-plugins-plus-skills
clerk-pack
plugins/saas-packs/clerk-pack/skills/clerk-performance-tuning/SKILL.md
January 22, 2026
Select agents to install to:
npx add-skill https://github.com/jeremylongshore/claude-code-plugins-plus-skills/blob/main/plugins/saas-packs/clerk-pack/skills/clerk-performance-tuning/SKILL.md -a claude-code --skill clerk-performance-tuningInstallation paths:
.claude/skills/clerk-performance-tuning/# Clerk Performance Tuning
## Overview
Optimize Clerk authentication for best performance and user experience.
## Prerequisites
- Clerk integration working
- Performance monitoring in place
- Understanding of application architecture
## Instructions
### Step 1: Optimize Middleware
```typescript
// middleware.ts - Optimized configuration
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
// Pre-compile route matchers (done once at startup)
const isPublicRoute = createRouteMatcher([
'/',
'/sign-in(.*)',
'/sign-up(.*)',
'/api/public(.*)',
'/api/webhooks(.*)'
])
// Exclude static files from middleware processing
export const config = {
matcher: [
// Skip all static files and images
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// Always run for API routes
'/(api|trpc)(.*)'
]
}
export default clerkMiddleware(async (auth, request) => {
// Quick return for public routes
if (isPublicRoute(request)) {
return
}
// Protect other routes
await auth.protect()
})
```
### Step 2: Implement User Data Caching
```typescript
// lib/cached-user.ts
import { unstable_cache } from 'next/cache'
import { clerkClient, currentUser } from '@clerk/nextjs/server'
// Cache user data with Next.js cache
export const getCachedUser = unstable_cache(
async (userId: string) => {
const client = await clerkClient()
return client.users.getUser(userId)
},
['user-data'],
{
revalidate: 60, // 1 minute cache
tags: ['users']
}
)
// In-memory cache for very frequent lookups
const userCache = new Map<string, { data: any; expiry: number }>()
export async function getUserFast(userId: string) {
const cached = userCache.get(userId)
const now = Date.now()
if (cached && cached.expiry > now) {
return cached.data
}
const user = await getCachedUser(userId)
userCache.set(userId, {
data: user,
expiry: now + 30000 // 30 sec