This commit is contained in:
hailin 2025-05-20 19:52:32 +08:00
parent 0fbcdbe7dc
commit dfa51fc419
1 changed files with 10 additions and 10 deletions

View File

@ -44,16 +44,15 @@ import { createBrowserClient } from "@supabase/ssr";
import { Database } from "@/supabase/types";
// 缓存 Supabase 客户端实例
let _supabase: ReturnType<typeof createBrowserClient<Database>> | null = null;
let _supabase: SupabaseClient<Database> | null = null;
// 创建 Supabase 客户端的工厂方法,改为同步初始化
export function createClient() {
// 如果客户端已创建,直接返回已创建的客户端
if (_supabase) return _supabase;
// 创建 Supabase 客户端的工厂方法
export async function createClient() {
if (_supabase) return _supabase; // 如果客户端已创建,直接返回
// 从 window.RUNTIME_ENV 中同步获取 SUPABASE_URL
const url = window?.RUNTIME_ENV?.SUPABASE_URL || "http://localhost:8000"; // 默认回退地址
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!; // 保留写死的 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);
@ -61,6 +60,7 @@ export function createClient() {
}
// 返回已初始化的 Supabase 客户端实例
export const supabase = () => {
return createClient(); // 直接返回已经初始化好的 Supabase 客户端实例
export const supabase = async () => {
const client = await createClient(); // 等待客户端初始化
return client; // 返回已初始化的客户端实例
};