Skip to content

Commit

Permalink
Chore: Implement singleton pattern for createClientComponentClient fu…
Browse files Browse the repository at this point in the history
…nction (#550)

* implement singleton pattern for createClientComponentClient
  • Loading branch information
dijonmusters authored May 21, 2023
1 parent 5d338e8 commit 008a08c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
50 changes: 25 additions & 25 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"mode": "pre",
"tag": "next",
"initialVersions": {
"@example/nextjs": "0.0.0",
"@example/nextjs-server-components": "0.0.0",
"@example/sveltekit": "0.0.0",
"config": "0.1.0",
"@supabase/auth-helpers-nextjs": "0.6.0",
"@supabase/auth-helpers-react": "0.3.1",
"@supabase/auth-helpers-remix": "0.1.8",
"@supabase/auth-helpers-shared": "0.3.3",
"@supabase/auth-helpers-sveltekit": "0.9.3",
"tsconfig": "0.1.1"
},
"changesets": [
"brave-lizards-thank",
"cyan-dancers-care",
"eighty-chefs-protect",
"eleven-radios-share",
"lazy-cows-shake",
"plenty-seas-build",
"silly-beds-watch",
"six-eggs-search",
"violet-frogs-know"
]
"mode": "pre",
"tag": "next",
"initialVersions": {
"@example/nextjs": "0.0.0",
"@example/nextjs-server-components": "0.0.0",
"@example/sveltekit": "0.0.0",
"config": "0.1.0",
"@supabase/auth-helpers-nextjs": "0.6.0",
"@supabase/auth-helpers-react": "0.3.1",
"@supabase/auth-helpers-remix": "0.1.8",
"@supabase/auth-helpers-shared": "0.3.3",
"@supabase/auth-helpers-sveltekit": "0.9.3",
"tsconfig": "0.1.1"
},
"changesets": [
"brave-lizards-thank",
"cyan-dancers-care",
"eighty-chefs-protect",
"eleven-radios-share",
"lazy-cows-shake",
"plenty-seas-build",
"silly-beds-watch",
"six-eggs-search",
"violet-frogs-know"
]
}
5 changes: 5 additions & 0 deletions .changeset/silly-laws-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-nextjs': patch
---

Implement singleton pattern for createClientComponentClient to simplify implementation
38 changes: 25 additions & 13 deletions packages/nextjs/src/clientComponentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { SupabaseClient } from '@supabase/supabase-js';

let supabase: SupabaseClient | undefined;

export function createClientComponentClient<
Database = any,
Expand All @@ -27,18 +30,27 @@ export function createClientComponentClient<
);
}

return createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
const _supabase =
supabase ??
createSupabaseClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
}
},
auth: {
storageKey: cookieOptions?.name,
storage: new BrowserCookieAuthStorageAdapter(cookieOptions)
}
},
auth: {
storageKey: cookieOptions?.name,
storage: new BrowserCookieAuthStorageAdapter(cookieOptions)
}
});
});

// For SSG and SSR always create a new Supabase client
if (typeof window === 'undefined') return _supabase;
// Create the Supabase client once in the client
if (!supabase) supabase = _supabase;

return supabase;
}

0 comments on commit 008a08c

Please sign in to comment.