This commit is contained in:
hailin 2025-05-21 19:57:36 +08:00
parent 12b5de8964
commit 5cebed6c67
1 changed files with 61 additions and 11 deletions

View File

@ -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<Database>(
// 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<Database>(
supabaseUrl,
supabaseAnonKey
)
)