From 796e94ab801e8c31d4d31d8421a6fba98fae2bcf Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Sat, 23 Nov 2024 13:41:58 +0100 Subject: [PATCH] wip --- packages/supabase/src/client/server.ts | 59 ++++++++++--------- .../supabase/src/queries/cached-queries.ts | 10 ++-- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/packages/supabase/src/client/server.ts b/packages/supabase/src/client/server.ts index 9071f6f561..09a0e0ff0d 100644 --- a/packages/supabase/src/client/server.ts +++ b/packages/supabase/src/client/server.ts @@ -34,10 +34,11 @@ console.log = (...args) => { type CreateClientOptions = { admin?: boolean; schema?: "public" | "storage"; + balancer?: boolean; }; export const createClient = (options?: CreateClientOptions) => { - const { admin = false, ...rest } = options ?? {}; + const { admin = false, balancer = false, ...rest } = options ?? {}; const cookieStore = cookies(); @@ -53,35 +54,35 @@ export const createClient = (options?: CreateClientOptions) => { } : {}; - return createServerClient( - process.env.NEXT_PUBLIC_SUPABASE_URL!, - key, - { - ...rest, - cookies: { - get(name: string) { - return cookieStore.get(name)?.value; - }, - set(name: string, value: string, options: CookieOptions) { - try { - cookieStore.set({ name, value, ...options }); - } catch (error) {} - }, - remove(name: string, options: CookieOptions) { - try { - cookieStore.set({ name, value: "", ...options }); - } catch (error) {} - }, + const url = balancer + ? process.env.NEXT_PUBLIC_SUPABASE_LOAD_BALANCER_URL! + : process.env.NEXT_PUBLIC_SUPABASE_URL!; + + return createServerClient(url, key, { + ...rest, + cookies: { + get(name: string) { + return cookieStore.get(name)?.value; + }, + set(name: string, value: string, options: CookieOptions) { + try { + cookieStore.set({ name, value, ...options }); + } catch (error) {} }, - auth, - global: { - headers: { - // Pass user agent from browser - "user-agent": headers().get("user-agent") as string, - // https://supabase.com/docs/guides/platform/read-replicas#experimental-routing - "sb-lb-routing-mode": "alpha-all-services", - }, + remove(name: string, options: CookieOptions) { + try { + cookieStore.set({ name, value: "", ...options }); + } catch (error) {} }, }, - ); + auth, + global: { + headers: { + // Pass user agent from browser + "user-agent": headers().get("user-agent") as string, + // https://supabase.com/docs/guides/platform/read-replicas#experimental-routing + ...(balancer && { "sb-lb-routing-mode": "alpha-all-services" }), + }, + }, + }); }; diff --git a/packages/supabase/src/queries/cached-queries.ts b/packages/supabase/src/queries/cached-queries.ts index c0ae86aa7a..d2f5cd2cb6 100644 --- a/packages/supabase/src/queries/cached-queries.ts +++ b/packages/supabase/src/queries/cached-queries.ts @@ -342,7 +342,7 @@ export const getUserInvites = async () => { export const getTrackerProjects = async ( params: Omit, ) => { - const supabase = createClient(); + const supabase = createClient({ balancer: true }); const user = await getUser(); const teamId = user?.data?.team_id; @@ -361,7 +361,7 @@ export const getTrackerProjects = async ( export const getTrackerRecordsByRange = async ( params: Omit, ) => { - const supabase = createClient(); + const supabase = createClient({ balancer: true }); const user = await getUser(); const teamId = user?.data?.team_id; @@ -384,7 +384,7 @@ export const getTrackerRecordsByRange = async ( export const getBurnRate = async ( params: Omit, ) => { - const supabase = createClient(); + const supabase = createClient({ balancer: true }); const user = await getUser(); const teamId = user?.data?.team_id; @@ -403,7 +403,7 @@ export const getBurnRate = async ( export const getRunway = async ( params: Omit, ) => { - const supabase = createClient(); + const supabase = createClient({ balancer: true }); const user = await getUser(); const teamId = user?.data?.team_id; @@ -422,7 +422,7 @@ export const getRunway = async ( export const getCategories = async ( params?: Omit, ) => { - const supabase = createClient(); + const supabase = createClient({ balancer: true }); const user = await getUser(); const teamId = user?.data?.team_id;