This commit is contained in:
parent
a0fd937b79
commit
af377672d9
|
|
@ -1,7 +1,21 @@
|
||||||
// lib/ipconfig.ts
|
// 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<string, string> | null = null
|
||||||
|
|
||||||
export function getRuntimeEnv(key: string): string | undefined {
|
export function getRuntimeEnv(key: string): string | undefined {
|
||||||
|
if (_env) return _env[key]
|
||||||
|
|
||||||
if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") {
|
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]
|
||||||
}
|
}
|
||||||
|
|
@ -1,54 +1,35 @@
|
||||||
|
// import { createBrowserClient } from "@supabase/ssr"
|
||||||
|
// import { getRuntimeEnv } from "@/lib/ipconfig"
|
||||||
|
// import { Database } from "@/supabase/types"
|
||||||
|
|
||||||
|
// export const supabase = createBrowserClient<Database>(
|
||||||
|
// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
||||||
|
// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! // ✅ 编译时写死,照你意图
|
||||||
|
// )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import { createBrowserClient } from "@supabase/ssr"
|
||||||
import { Database } from "@/supabase/types"
|
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<typeof createBrowserClient<Database>> | null = null
|
||||||
|
|
||||||
export const supabase = createBrowserClient<Database>(
|
export async function createClient() {
|
||||||
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
|
||||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
let _supabase: ReturnType<typeof createBrowserClient> | null = null
|
|
||||||
|
|
||||||
export async function initSupabase(providedCookieStore?: ReturnType<typeof serverCookies>) {
|
|
||||||
if (typeof window === "undefined") {
|
|
||||||
// SSR 路径:用服务端 Supabase 客户端
|
|
||||||
const cookieStore = providedCookieStore ?? serverCookies()
|
|
||||||
return createServerClient<Database>(
|
|
||||||
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 注入,并复用客户端实例
|
|
||||||
if (_supabase) return _supabase
|
if (_supabase) return _supabase
|
||||||
|
|
||||||
await waitForRuntimeEnv()
|
const url = await getRuntimeSupabaseUrl()
|
||||||
|
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! // 你要求写死,保留
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
_supabase = createBrowserClient(url, key)
|
_supabase = createBrowserClient(url, key)
|
||||||
return _supabase
|
return _supabase
|
||||||
}
|
}
|
||||||
|
|
||||||
async function waitForRuntimeEnv(retry = 50, interval = 50) {
|
async function getRuntimeSupabaseUrl(retry = 30, interval = 50): Promise<string> {
|
||||||
for (let i = 0; i < retry; i++) {
|
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))
|
await new Promise((r) => setTimeout(r, interval))
|
||||||
}
|
}
|
||||||
throw new Error("window.RUNTIME_ENV not ready after wait")
|
throw new Error("RUNTIME_ENV.SUPABASE_URL not ready")
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue