From a6df6e8c444b9707ecf80aeb2c6abff41f0327e6 Mon Sep 17 00:00:00 2001 From: wass08 Date: Wed, 11 Feb 2026 16:02:55 +0900 Subject: [PATCH] fix env check at wrong time --- packages/db/src/client.ts | 29 ++++++++++++++++++++++------- packages/db/src/server.ts | 35 +++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/packages/db/src/client.ts b/packages/db/src/client.ts index 6e328bca..5771823d 100644 --- a/packages/db/src/client.ts +++ b/packages/db/src/client.ts @@ -1,17 +1,32 @@ import { createClient } from '@supabase/supabase-js' import type { Database } from './types' -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL -const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY +let _supabase: ReturnType> | null = null -if (!supabaseUrl || !supabaseAnonKey) { - throw new Error( - 'Missing Supabase environment variables. Add NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY to your .env.local file.' - ) +function getSupabase() { + if (!_supabase) { + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL + const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY + + if (!supabaseUrl || !supabaseAnonKey) { + throw new Error( + 'Missing required environment variables: NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY. Please configure them in your deployment settings or .env.local file.' + ) + } + + _supabase = createClient(supabaseUrl, supabaseAnonKey) + } + return _supabase } /** * Supabase client for client-side use with anon key * Uses Row Level Security (RLS) policies + * + * Initialized lazily to avoid requiring env vars at build time */ -export const supabase = createClient(supabaseUrl, supabaseAnonKey) +export const supabase = new Proxy({} as ReturnType>, { + get(_target, prop) { + return Reflect.get(getSupabase(), prop) + }, +}) diff --git a/packages/db/src/server.ts b/packages/db/src/server.ts index 1de4837b..0c03c256 100644 --- a/packages/db/src/server.ts +++ b/packages/db/src/server.ts @@ -1,23 +1,38 @@ import { createClient } from '@supabase/supabase-js' import type { Database } from './types' -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL -const supabaseServiceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY +let _supabaseAdmin: ReturnType> | null = null -if (!supabaseUrl || !supabaseServiceRoleKey) { - throw new Error( - 'Missing Supabase environment variables. Add NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY to your .env.local file.' - ) +function getSupabaseAdmin() { + if (!_supabaseAdmin) { + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL + const supabaseServiceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY + + if (!supabaseUrl || !supabaseServiceRoleKey) { + throw new Error( + 'Missing required environment variables: NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY. Please configure them in your deployment settings or .env.local file.' + ) + } + + _supabaseAdmin = createClient(supabaseUrl, supabaseServiceRoleKey, { + auth: { + persistSession: false, + autoRefreshToken: false, + }, + }) + } + return _supabaseAdmin } /** * Supabase client for server-side use with service role key * Bypasses Row Level Security (RLS) - use with caution * Always filter by user_id to enforce permissions + * + * Initialized lazily to avoid requiring env vars at build time */ -export const supabaseAdmin = createClient(supabaseUrl, supabaseServiceRoleKey, { - auth: { - persistSession: false, - autoRefreshToken: false, +export const supabaseAdmin = new Proxy({} as ReturnType>, { + get(_target, prop) { + return Reflect.get(getSupabaseAdmin(), prop) }, })