From cdf6f8caf303d5f6d94ff6b214041bf10d79b252 Mon Sep 17 00:00:00 2001 From: SinachPat Date: Wed, 6 May 2026 23:24:43 +0100 Subject: [PATCH] fix: use dot notation for NEXT_PUBLIC env vars in browserClient Next.js only statically inlines NEXT_PUBLIC_* variables when accessed via dot notation (process.env.NEXT_PUBLIC_SUPABASE_URL). The generic requireEnv helper used bracket notation (process.env[name]) which Next.js cannot replace at build time, causing the browser bundle to always see undefined regardless of what is set in the Vercel dashboard. Co-Authored-By: Claude Sonnet 4.6 --- packages/app/src/lib/supabase.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/app/src/lib/supabase.ts b/packages/app/src/lib/supabase.ts index 767735e..1e407db 100644 --- a/packages/app/src/lib/supabase.ts +++ b/packages/app/src/lib/supabase.ts @@ -14,12 +14,17 @@ function requireEnv(name: string): string { return val; } -/** Browser-safe Supabase client (anon key, RLS enforced). */ +/** Browser-safe Supabase client (anon key, RLS enforced). + * + * IMPORTANT: NEXT_PUBLIC_* vars must be accessed via dot notation so Next.js + * can statically inline them into the client bundle at build time. Dynamic + * bracket access (process.env[name]) is not replaced and yields undefined. */ export function browserClient(): DbClient { - return createClient( - requireEnv('NEXT_PUBLIC_SUPABASE_URL'), - requireEnv('NEXT_PUBLIC_SUPABASE_ANON_KEY'), - ) as unknown as DbClient; + const url = process.env.NEXT_PUBLIC_SUPABASE_URL; + const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + if (!url) throw new Error('Missing required environment variable: NEXT_PUBLIC_SUPABASE_URL'); + if (!key) throw new Error('Missing required environment variable: NEXT_PUBLIC_SUPABASE_ANON_KEY'); + return createClient(url, key) as unknown as DbClient; } /** Server-only Supabase client (service-role key, bypasses RLS). */