This commit is contained in:
hailin 2025-05-20 19:46:00 +08:00
parent f6b8dcbf84
commit b258867873
1 changed files with 7 additions and 13 deletions

View File

@ -42,6 +42,7 @@
import { createBrowserClient } from "@supabase/ssr";
import { getRuntimeEnv } from "@/lib/ipconfig";
import { Database } from "@/supabase/types";
// 保存懒加载的 Supabase 客户端实例
@ -53,7 +54,7 @@ export async function createClient() {
if (_supabase) return _supabase;
// 获取动态的 SUPABASE_URL
const url = await getRuntimeSupabaseUrl();
const url = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000";
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!; // 写死的 anon key
// 创建并缓存 Supabase 客户端实例
@ -61,15 +62,8 @@ export async function createClient() {
return _supabase;
}
// 获取动态注入的 SUPABASE_URL
async function getRuntimeSupabaseUrl(retry = 30, interval = 50): Promise<string> {
for (let i = 0; i < retry; i++) {
const url = window?.RUNTIME_ENV?.SUPABASE_URL;
if (url) return url;
await new Promise((r) => setTimeout(r, interval));
}
throw new Error("RUNTIME_ENV.SUPABASE_URL not ready");
}
// 确保返回的 supabase 实例兼容原有的代码
export const supabase = createClient; // 保持原有方式调用 supabase
// 返回已初始化的 supabase 实例
export const supabase = async () => {
const client = await createClient(); // 确保客户端已初始化
return client; // 返回已初始化好的 supabase 客户端实例
};