feat: implement Supabase client with safe query handling and error management
This commit is contained in:
parent
7ad27799f9
commit
16825648ed
@ -1,55 +1,66 @@
|
|||||||
import { createClient } from '@supabase/supabase-js';
|
import { createClient } from '@supabase/supabase-js';
|
||||||
|
|
||||||
// Function to create a supabase client that doesn't fail build if env vars are missing
|
|
||||||
const createSupabaseClient = () => {
|
const createSupabaseClient = () => {
|
||||||
// For Coolify docker deployment with build args
|
|
||||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||||
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
|
||||||
|
|
||||||
// Skip errors during build
|
if (typeof window === 'undefined') { // Server-side or build process
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
if (!supabaseUrl || !supabaseAnonKey) {
|
if (!supabaseUrl || !supabaseAnonKey) {
|
||||||
// During build/SSR, return null client - this prevents build errors
|
console.log('Build/SSR: Supabase URL or Anon Key is missing. Client not initialized.');
|
||||||
console.log('Building without Supabase credentials. Will connect at runtime if available.');
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
// If URL and Key are present, try to create client but catch errors during build
|
||||||
// In browser, log helpful messages but don't block rendering
|
try {
|
||||||
if (!supabaseUrl) {
|
const client = createClient(supabaseUrl, supabaseAnonKey);
|
||||||
console.warn('Warning: Missing Supabase URL. Please configure NEXT_PUBLIC_SUPABASE_URL.');
|
// 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
|
||||||
}
|
}
|
||||||
if (!supabaseAnonKey) {
|
} else { // Client-side (browser)
|
||||||
console.warn('Warning: Missing Supabase Anon Key. Please configure NEXT_PUBLIC_SUPABASE_ANON_KEY.');
|
if (!supabaseUrl || !supabaseAnonKey) {
|
||||||
|
console.warn('Client-side: Supabase URL or Anon Key is missing. Please check environment variables.');
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Only create client if we have credentials
|
|
||||||
if (supabaseUrl && supabaseAnonKey) {
|
|
||||||
try {
|
try {
|
||||||
return createClient(supabaseUrl, supabaseAnonKey);
|
return createClient(supabaseUrl, supabaseAnonKey);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error initializing Supabase client:', error);
|
console.error(`Client-side: Error initializing Supabase client: ${error.message}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the client (can be null during build or if credentials missing)
|
|
||||||
export const supabase = createSupabaseClient();
|
export const supabase = createSupabaseClient();
|
||||||
|
|
||||||
// Helper function for components to safely use supabase
|
|
||||||
export const safeQuery = async (queryFn) => {
|
export const safeQuery = async (queryFn) => {
|
||||||
if (!supabase) {
|
if (!supabase) {
|
||||||
console.warn('Supabase client not available. Check your environment variables.');
|
const buildTimeMessage = 'Build time: Supabase client not initialized for pre-rendering, due to missing or problematic credentials.';
|
||||||
return { data: null, error: { message: 'Database connection not available' } };
|
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 {
|
try {
|
||||||
return await queryFn(supabase);
|
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 (error) {
|
||||||
console.error('Error in Supabase query:', error);
|
// Catch unexpected errors during the queryFn execution itself
|
||||||
return { data: null, error: { message: 'Error connecting to database' } };
|
console.error('Unexpected error in Supabase query function:', error.message);
|
||||||
|
return { data: null, error: { message: `Unexpected error during database operation: ${error.message}` } };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user