This commit is contained in:
hailin 2025-05-20 19:59:22 +08:00
parent 466cd5f37a
commit 61f8f0f2ba
1 changed files with 7 additions and 10 deletions

View File

@ -51,20 +51,17 @@ import { Database } from "@/supabase/types";
let _supabase: SupabaseClient<Database> | null = null;
// 创建 Supabase 客户端的工厂方法
export async function createClient() {
if (_supabase) return _supabase; // 如果客户端已创建,直接返回
export function createClient() {
if (_supabase) return _supabase; // 如果客户端已经初始化,直接返回
// 获取动态的 SUPABASE_URL
const url = window?.RUNTIME_ENV?.SUPABASE_URL ?? "http://localhost:8000";
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!; // 使用环境变量中的匿名密钥
// 获取动态的 SUPABASE_URL(同步获取)
const url = window?.RUNTIME_ENV?.SUPABASE_URL ?? "http://localhost:8000"; // 默认回退值
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!; // 使用环境变量中的匿名密钥
// 初始化并缓存 Supabase 客户端实例
_supabase = createBrowserClient(url, key);
return _supabase;
}
// 返回已初始化的 Supabase 客户端实例
export const supabase = async () => {
const client = await createClient(); // 等待客户端初始化
return client; // 返回已初始化的客户端实例
};
// 返回已初始化的同步 Supabase 客户端实例
export const supabase = createClient;