This commit is contained in:
hailin 2025-06-25 17:53:25 +08:00
parent 452a58a74a
commit 9b8b705e97
1 changed files with 14 additions and 13 deletions

View File

@ -93,24 +93,27 @@
// ) // )
// lib/supabase/browser-client.ts
import { createBrowserClient } from "@supabase/ssr" import { createBrowserClient } from "@supabase/ssr"
import { Database } from "@/supabase/types" import { Database } from "@/supabase/types"
let supabaseUrl = "http://localhost:8000" let supabaseUrl = "http://localhost:8000"
// ✅ 动态导入 SSR 版本的 getRuntimeEnv仅在服务器端执行 // ✅ 服务端逻辑:使用 require 避免静态引入
if (typeof window === "undefined") { if (typeof window === "undefined") {
(async () => { try {
const { getRuntimeEnvServer } = await import("@/lib/runtime-env/server") const { getRuntimeEnvSSR } = require("@/lib/runtime-env/server")
const url = getRuntimeEnvServer("SUPABASE_URL") const envUrl = getRuntimeEnvSSR("SUPABASE_URL")
if (url) { if (envUrl) {
supabaseUrl = url supabaseUrl = envUrl
console.log("[SSR] Using SUPABASE_URL from getRuntimeEnvServer:", url) console.log("[SSR] SUPABASE_URL:", supabaseUrl)
} }
})() } catch (err) {
console.error("⚠️ Failed to load runtime env on SSR", err)
}
} else { } else {
// ✅ 客户端环境,从 window.RUNTIME_ENV 或 localStorage 获取 // ✅ 客户端逻辑
const runtimeEnv = (window as any).RUNTIME_ENV const runtimeEnv = (window as any).RUNTIME_ENV
const envUrl = runtimeEnv?.SUPABASE_URL const envUrl = runtimeEnv?.SUPABASE_URL
@ -124,13 +127,11 @@ if (typeof window === "undefined") {
} }
} }
console.log("[CSR] Using SUPABASE_URL:", supabaseUrl) console.log("[CSR] SUPABASE_URL:", supabaseUrl)
} }
// 编译时固定匿名 key前端安全公开 // ✅ 创建 supabase 客户端
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
// 导出单例,兼容所有旧代码方式
export const supabase = createBrowserClient<Database>( export const supabase = createBrowserClient<Database>(
supabaseUrl, supabaseUrl,
supabaseAnonKey supabaseAnonKey