Skip to content

fix: cache supabase browser client as singleton to prevent recreation…#59

Open
MayurKharat0390 wants to merge 3 commits into
AditthyaSS:mainfrom
MayurKharat0390:fix/16
Open

fix: cache supabase browser client as singleton to prevent recreation…#59
MayurKharat0390 wants to merge 3 commits into
AditthyaSS:mainfrom
MayurKharat0390:fix/16

Conversation

@MayurKharat0390
Copy link
Copy Markdown

Description

This Pull Request implements a singleton caching pattern for the Supabase browser client inside src/lib/supabase.ts.

The Problem

In the original implementation, the getSupabaseBrowserClient() function initialized a brand new Supabase client instance using createClient() on every single invocation. Because React components, hooks, and context providers call this helper frequently throughout the lifecycle of the application:

  • It leads to duplicate WebSocket and Realtime postgres subscription channel allocations.
  • It causes excessive memory overhead and potential event listener leaks on the client side.
  • It can quickly saturate browser and server-side socket connections.

The Solution

  • Introduced a module-level variable cachedBrowserClient to cache the initialized client.
  • Modified getSupabaseBrowserClient() to check for the cached instance first and return it if present.
  • Instantiates the Supabase browser client only once on the client side, ensuring a correct and efficient singleton pattern.

Closes

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@MayurKharat0390 is attempting to deploy a commit to the aditthyass' projects Team on Vercel.

A member of the Team first needs to authorize it.

@AditthyaSS
Copy link
Copy Markdown
Owner

Hey @MayurKharat0390 , thank you for the contribution

I noticed that there are merge conflicts in this PR

Please sync your branch with the latest main branch, resolve the conflicts, and push the updated changes. Once the conflicts are resolved, I will continue with the review process

Let me know if you need any assistance while resolving them. Thanks again for the contribution ⭐

@MayurKharat0390
Copy link
Copy Markdown
Author

Hey @AditthyaSS,

I have synced the branch with the latest main branch and resolved the merge conflicts in src/lib/supabase.ts by keeping the explicitly typed client singleton.

Additionally, I noticed that the main branch was failing to compile due to a missing unique constraint on the Review model (which was causing a type error in the reviews upsert API). I added the missing constraint @@unique([userId, entityType, entityId]) to prisma/schema.prisma and ran type generation, which has fully resolved the build compilation failure.

Everything compiles successfully and is ready for your review! ⭐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Supabase Browser Client Is Recreated on Every Access

2 participants