67 lines
3.0 KiB
JavaScript
67 lines
3.0 KiB
JavaScript
import { createClient } from '@supabase/supabase-js';
|
|
|
|
const createSupabaseClient = () => {
|
|
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
|
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
|
|
|
if (typeof window === 'undefined') { // Server-side or build process
|
|
if (!supabaseUrl || !supabaseAnonKey) {
|
|
console.log('Build/SSR: Supabase URL or Anon Key is missing. Client not initialized.');
|
|
return null;
|
|
}
|
|
// If URL and Key are present, try to create client but catch errors during build
|
|
try {
|
|
const client = createClient(supabaseUrl, supabaseAnonKey);
|
|
// console.log('Build/SSR: Supabase client potentially initialized with provided credentials.'); // Less verbose
|
|
return client;
|
|
} catch (error) {
|
|
console.warn(`Build/SSR: Error initializing Supabase client with provided credentials: ${error.message}. This might be due to invalid credentials or network issues during build. App will attempt to connect at runtime.`);
|
|
return null; // Prevent build failure if client creation itself errors with valid-looking but problematic creds
|
|
}
|
|
} else { // Client-side (browser)
|
|
if (!supabaseUrl || !supabaseAnonKey) {
|
|
console.warn('Client-side: Supabase URL or Anon Key is missing. Please check environment variables.');
|
|
return null;
|
|
}
|
|
try {
|
|
return createClient(supabaseUrl, supabaseAnonKey);
|
|
} catch (error) {
|
|
console.error(`Client-side: Error initializing Supabase client: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
};
|
|
|
|
export const supabase = createSupabaseClient();
|
|
|
|
export const safeQuery = async (queryFn) => {
|
|
if (!supabase) {
|
|
const buildTimeMessage = 'Build time: Supabase client not initialized for pre-rendering, due to missing or problematic credentials.';
|
|
const runTimeMessage = 'Runtime: Supabase client not available. Check environment variables and connection.';
|
|
|
|
// Check if this is build time (Node.js environment during `next build`)
|
|
if (typeof window === 'undefined' && process.env.NODE_ENV === 'production') {
|
|
console.log(buildTimeMessage);
|
|
return { data: null, error: { message: 'Supabase not available during build.' } };
|
|
} else {
|
|
console.warn(runTimeMessage);
|
|
return { data: null, error: { message: 'Supabase connection not available.' } };
|
|
}
|
|
}
|
|
|
|
try {
|
|
const result = await queryFn(supabase);
|
|
// Add a check for Supabase-specific errors if the query executes but returns an error object
|
|
if (result.error) {
|
|
console.error('Supabase query error:', result.error.message);
|
|
// Return a consistent error structure
|
|
return { data: null, error: { message: result.error.message } };
|
|
}
|
|
return result;
|
|
} catch (error) {
|
|
// Catch unexpected errors during the queryFn execution itself
|
|
console.error('Unexpected error in Supabase query function:', error.message);
|
|
return { data: null, error: { message: `Unexpected error during database operation: ${error.message}` } };
|
|
}
|
|
};
|