reactjsreact-nativeexposupabaseclerk

Does supabase-js createClient create a singleton?


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?


Solution

  • 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:

    is it best practice to create a new supabase client in every api route?

    Yes, it is best practice to create a new Supabase client any time you need to use it. The createClient function is a super lightweight call - it basically just sets up an object to be able to make fetch calls when you want to request data.

    source