This commit is contained in:
parent
12b5de8964
commit
5cebed6c67
|
|
@ -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 { createBrowserClient } from "@supabase/ssr"
|
||||||
import { getRuntimeEnv } from "@/lib/ipconfig"
|
import { getRuntimeEnv } from "@/lib/ipconfig"
|
||||||
import { Database } from "@/supabase/types"
|
import { Database } from "@/supabase/types"
|
||||||
|
|
@ -16,17 +60,23 @@ import { Database } from "@/supabase/types"
|
||||||
// 动态获取 URL,防止静态打包成 localhost:8000
|
// 动态获取 URL,防止静态打包成 localhost:8000
|
||||||
let supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000"
|
let supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000"
|
||||||
|
|
||||||
// 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage
|
// 仅在浏览器端使用 localStorage
|
||||||
if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) {
|
if (typeof window !== "undefined") {
|
||||||
localStorage.setItem("supabaseUrl", supabaseUrl)
|
// 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage
|
||||||
} else {
|
if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) {
|
||||||
// 如果是 http://localhost:8000 或者 undefined,则从 localStorage 获取
|
localStorage.setItem("supabaseUrl", supabaseUrl)
|
||||||
const storedUrl = localStorage.getItem("supabaseUrl")
|
|
||||||
if (storedUrl && storedUrl !== "http://localhost:8000") {
|
|
||||||
supabaseUrl = storedUrl
|
|
||||||
} else {
|
} 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
|
// 打印获取的 URL
|
||||||
|
|
@ -39,4 +89,4 @@ const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||||
export const supabase = createBrowserClient<Database>(
|
export const supabase = createBrowserClient<Database>(
|
||||||
supabaseUrl,
|
supabaseUrl,
|
||||||
supabaseAnonKey
|
supabaseAnonKey
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue