Skip to content

Commit 5d54650

Browse files
perf(auth): skip getUser() when no auth cookies present
The middleware was calling getUser() on EVERY request including unauthenticated ones (bots, prefetches, public pages). This caused 13,000+ auth API requests per hour with zero users, burning through Supabase rate limits. Now checks for sb-* cookies first — if none exist, returns immediately without hitting the Supabase Auth API. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 69ea1a4 commit 5d54650

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

lib/supabase/proxy.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ export async function updateSession(request: NextRequest): Promise<{ response: N
4848
// supabase.auth.getClaims(). A simple mistake could make it very hard to debug
4949
// issues with users being randomly logged out.
5050

51-
// IMPORTANT: DO NOT REMOVE auth.getUser()
52-
// A simple mistake could make it very hard to debug issues with users being randomly logged out.
53-
// Returns the user so callers don't need a second getUser() network call.
51+
// IMPORTANT: Only call getUser() when auth cookies exist.
52+
// Without this check, every unauthenticated request (bots, prefetches, public pages)
53+
// makes a wasted network call to Supabase Auth — causing thousands of unnecessary
54+
// requests that burn through rate limits.
5455
let user: User | null = null
56+
const hasAuthCookies = request.cookies.getAll().some(c => c.name.startsWith('sb-'))
57+
if (!hasAuthCookies) {
58+
return { response: supabaseResponse, user: null }
59+
}
60+
5561
try {
5662
const { data } = await supabase.auth.getUser()
5763
user = data.user

0 commit comments

Comments
 (0)