From af377672d90fc4110720019295f1f27e16449a83 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 20 May 2025 17:40:14 +0800 Subject: [PATCH] . --- chatdesk-ui/lib/ipconfig.ts | 20 +++++-- chatdesk-ui/lib/supabase/browser-client.ts | 63 ++++++++-------------- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/chatdesk-ui/lib/ipconfig.ts b/chatdesk-ui/lib/ipconfig.ts index 7b550ba..094bfc7 100644 --- a/chatdesk-ui/lib/ipconfig.ts +++ b/chatdesk-ui/lib/ipconfig.ts @@ -1,7 +1,21 @@ // lib/ipconfig.ts +// export function getRuntimeEnv(key: string): string | undefined { +// if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") { +// return window.RUNTIME_ENV[key]; +// } +// return process.env[key]; +// } + + +let _env: Record | null = null + export function getRuntimeEnv(key: string): string | undefined { + if (_env) return _env[key] + if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") { - return window.RUNTIME_ENV[key]; + _env = window.RUNTIME_ENV + return _env[key] } - return process.env[key]; -} + + return process.env[key] +} \ No newline at end of file diff --git a/chatdesk-ui/lib/supabase/browser-client.ts b/chatdesk-ui/lib/supabase/browser-client.ts index bc00c53..cd00eb1 100644 --- a/chatdesk-ui/lib/supabase/browser-client.ts +++ b/chatdesk-ui/lib/supabase/browser-client.ts @@ -1,54 +1,35 @@ +// import { createBrowserClient } from "@supabase/ssr" +// import { getRuntimeEnv } from "@/lib/ipconfig" +// import { Database } from "@/supabase/types" + +// export const supabase = createBrowserClient( +// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", +// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! // ✅ 编译时写死,照你意图 +// ) + + + + +import { createBrowserClient } from "@supabase/ssr" import { Database } from "@/supabase/types" -import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 -import { createBrowserClient, createServerClient } from "@supabase/ssr" -import { cookies as serverCookies } from "next/headers" +let _supabase: ReturnType> | null = null -export const supabase = createBrowserClient( - getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", - process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! -) - - -let _supabase: ReturnType | null = null - -export async function initSupabase(providedCookieStore?: ReturnType) { - if (typeof window === "undefined") { - // SSR 路径:用服务端 Supabase 客户端 - const cookieStore = providedCookieStore ?? serverCookies() - return createServerClient( - getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", - process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, - { - cookies: { - get(name: string) { - return cookieStore.get(name)?.value - } - } - } - ) - } - - // CSR 路径:等待 RUNTIME_ENV 注入,并复用客户端实例 +export async function createClient() { if (_supabase) return _supabase - await waitForRuntimeEnv() - - const url = window.RUNTIME_ENV?.SUPABASE_URL - const key = window.RUNTIME_ENV?.SUPABASE_ANON_KEY - - if (!url || !key) { - throw new Error("Missing SUPABASE_URL or SUPABASE_ANON_KEY in RUNTIME_ENV") - } + const url = await getRuntimeSupabaseUrl() + const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! // 你要求写死,保留 _supabase = createBrowserClient(url, key) return _supabase } -async function waitForRuntimeEnv(retry = 50, interval = 50) { +async function getRuntimeSupabaseUrl(retry = 30, interval = 50): Promise { for (let i = 0; i < retry; i++) { - if (window.RUNTIME_ENV?.SUPABASE_URL && window.RUNTIME_ENV?.SUPABASE_ANON_KEY) return + const url = window?.RUNTIME_ENV?.SUPABASE_URL + if (url) return url await new Promise((r) => setTimeout(r, interval)) } - throw new Error("window.RUNTIME_ENV not ready after wait") -} \ No newline at end of file + throw new Error("RUNTIME_ENV.SUPABASE_URL not ready") +}