diff --git a/chatdesk-ui/lib/supabase/browser-client.ts b/chatdesk-ui/lib/supabase/browser-client.ts index e6ac883..c37a97f 100644 --- a/chatdesk-ui/lib/supabase/browser-client.ts +++ b/chatdesk-ui/lib/supabase/browser-client.ts @@ -8,7 +8,51 @@ // ) -// lib/supabase/browser-client.ts +// // lib/supabase/browser-client.ts +// import { createBrowserClient } from "@supabase/ssr" +// import { getRuntimeEnv } from "@/lib/ipconfig" +// import { Database } from "@/supabase/types" + +// // 动态获取 URL,防止静态打包成 localhost:8000 +// let supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" + +// // 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage +// if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) { +// localStorage.setItem("supabaseUrl", supabaseUrl) +// } else { +// // 如果是 http://localhost:8000 或者 undefined,则从 localStorage 获取 +// const storedUrl = localStorage.getItem("supabaseUrl") +// if (storedUrl && storedUrl !== "http://localhost:8000") { +// supabaseUrl = storedUrl +// } else { +// supabaseUrl = "http://localhost:8000" +// } +// } + +// // 打印获取的 URL +// console.log("=======>>Supabase URL:", supabaseUrl); + +// // 编译时固定匿名 key(前端安全公开) +// const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! + +// // 导出单例,兼容所有调用旧代码方式 +// export const supabase = createBrowserClient( +// supabaseUrl, +// supabaseAnonKey +// ) + + + + + + + + + + + + + import { createBrowserClient } from "@supabase/ssr" import { getRuntimeEnv } from "@/lib/ipconfig" import { Database } from "@/supabase/types" @@ -16,17 +60,23 @@ import { Database } from "@/supabase/types" // 动态获取 URL,防止静态打包成 localhost:8000 let supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" -// 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage -if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) { - localStorage.setItem("supabaseUrl", supabaseUrl) -} else { - // 如果是 http://localhost:8000 或者 undefined,则从 localStorage 获取 - const storedUrl = localStorage.getItem("supabaseUrl") - if (storedUrl && storedUrl !== "http://localhost:8000") { - supabaseUrl = storedUrl +// 仅在浏览器端使用 localStorage +if (typeof window !== "undefined") { + // 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage + if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) { + localStorage.setItem("supabaseUrl", supabaseUrl) } else { - supabaseUrl = "http://localhost:8000" + // 如果是 http://localhost:8000 或者 undefined,则从 localStorage 获取 + const storedUrl = localStorage.getItem("supabaseUrl") + if (storedUrl && storedUrl !== "http://localhost:8000") { + supabaseUrl = storedUrl + } else { + supabaseUrl = "http://localhost:8000" + } } +} else { + // 如果在服务器端,使用默认或从环境变量获取的 URL + console.log("[server-side] Falling back to default supabaseUrl:", supabaseUrl) } // 打印获取的 URL @@ -39,4 +89,4 @@ const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! export const supabase = createBrowserClient( supabaseUrl, supabaseAnonKey -) \ No newline at end of file +)