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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
bbef7d73f8
commit
cdf6f8caf3
@@ -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). */
|
||||
|
||||
Reference in New Issue
Block a user