mirror of
https://github.com/Luchexx/wacrm.git
synced 2026-11-02 09:19:51 +00:00
Fixes two runtime errors:
1. "Lock 'lock:sb-xxx-auth-token' was released because another request
stole it" — caused by multiple Supabase client instances competing
for the same auth storage lock.
2. "Failed to fetch conversations: {}" — related to #1. When the auth
lock is stolen mid-request, the session becomes temporarily
unavailable and queries fail.
The codebase had 65 call sites invoking createClient() — each creating
a new browser client with its own auth listener. This change makes
createClient() cache and return a single shared instance.
Also improved error logging in conversation-list.tsx to show Supabase
error fields (message/details/hint/code) instead of an empty {}.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
19 lines
621 B
TypeScript
19 lines
621 B
TypeScript
import { createBrowserClient } from '@supabase/ssr'
|
|
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
|
|
// Singleton instance — one client shared across the whole browser session.
|
|
// Creating multiple clients causes auth-lock contention ("Lock was released
|
|
// because another request stole it") and intermittent fetch failures.
|
|
let browserClient: SupabaseClient | undefined
|
|
|
|
export function createClient() {
|
|
if (browserClient) return browserClient
|
|
|
|
browserClient = createBrowserClient(
|
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
|
)
|
|
|
|
return browserClient
|
|
}
|