56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
import { createClient } from '@supabase/supabase-js';
|
|
|
|
// Function to create a supabase client that doesn't fail build if env vars are missing
|
|
const createSupabaseClient = () => {
|
|
// For Coolify docker deployment with build args
|
|
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
|
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
|
|
|
// Skip errors during build
|
|
if (typeof window === 'undefined') {
|
|
if (!supabaseUrl || !supabaseAnonKey) {
|
|
// During build/SSR, return null client - this prevents build errors
|
|
console.log('Building without Supabase credentials. Will connect at runtime if available.');
|
|
return null;
|
|
}
|
|
} else {
|
|
// In browser, log helpful messages but don't block rendering
|
|
if (!supabaseUrl) {
|
|
console.warn('Warning: Missing Supabase URL. Please configure NEXT_PUBLIC_SUPABASE_URL.');
|
|
}
|
|
if (!supabaseAnonKey) {
|
|
console.warn('Warning: Missing Supabase Anon Key. Please configure NEXT_PUBLIC_SUPABASE_ANON_KEY.');
|
|
}
|
|
}
|
|
|
|
// Only create client if we have credentials
|
|
if (supabaseUrl && supabaseAnonKey) {
|
|
try {
|
|
return createClient(supabaseUrl, supabaseAnonKey);
|
|
} catch (error) {
|
|
console.error('Error initializing Supabase client:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
// Create the client (can be null during build or if credentials missing)
|
|
export const supabase = createSupabaseClient();
|
|
|
|
// Helper function for components to safely use supabase
|
|
export const safeQuery = async (queryFn) => {
|
|
if (!supabase) {
|
|
console.warn('Supabase client not available. Check your environment variables.');
|
|
return { data: null, error: { message: 'Database connection not available' } };
|
|
}
|
|
|
|
try {
|
|
return await queryFn(supabase);
|
|
} catch (error) {
|
|
console.error('Error in Supabase query:', error);
|
|
return { data: null, error: { message: 'Error connecting to database' } };
|
|
}
|
|
};
|