Is it a (memory) issue to call the supabase createClient
function every time that you need the client?
I’m integrating Clerk with Supabase in my React Native app, and according to the docs you should get the latest Clerk JWT token before every fetch to supabase, because it is shortlived.
In some Next.js tutorials I find the pattern to create a new client before every fetch and pass it the latest token.
// use this function in every fetch
const supabaseClient = supabaseAccessToken => {
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY,
{
global: { headers: { Authorization: `Bearer ${supabaseAccessToken}` } }
}
);
return supabase;
};
I’m guessing that for Next.js this gets cleaned up well, or it’s a singleton, since this pattern is in official tutorials. What about in React Native? Can I call createClient
many times without worrying about memory?
2
Answers
Will answer my own question: I've found a comment from a supabase developer stating that it is best practice to always create a new client:
source
Creating a new Supabase client instance each time you need to make a fetch request can lead to unnecessary overhead and potential memory issues over time. This is because each call to createClient creates a new instance of the client, which could result in the accumulation of multiple client instances if not managed properly.
However, you can still update the JWT token for each request without creating a new client instance each time.
here is a better approach: