This commit is contained in:
parent
079596c208
commit
75a231ee56
|
|
@ -7,29 +7,87 @@
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// let _env: Record<string, string> | null = null
|
||||||
|
|
||||||
|
// export function getRuntimeEnv(key: string): string | undefined {
|
||||||
|
// console.log("============>>Getting Supabase API URL.")
|
||||||
|
|
||||||
|
// if (typeof window !== "undefined") {
|
||||||
|
// if (!_env && typeof window.RUNTIME_ENV !== "undefined") {
|
||||||
|
// _env = window.RUNTIME_ENV
|
||||||
|
// console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (_env) {
|
||||||
|
// console.log("[browser-side]Returning cached API from _env:", _env)
|
||||||
|
// return _env[key]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// console.log("[browser-side]No window.RUNTIME_ENV found in browser")
|
||||||
|
// return undefined
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 服务端始终动态读取 process.env
|
||||||
|
// const val = process.env[key]
|
||||||
|
// console.log("[server-side] Falling back to process.env for key:", key, "value:", val)
|
||||||
|
// return val
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let _env: Record<string, string> | null = null
|
let _env: Record<string, string> | null = null
|
||||||
|
|
||||||
export function getRuntimeEnv(key: string): string | undefined {
|
// 最大重试次数
|
||||||
console.log("============>>Getting Supabase API call URL.")
|
const MAX_RETRIES = 5
|
||||||
|
// 每次重试的延时(毫秒)
|
||||||
|
const RETRY_DELAY = 1000
|
||||||
|
|
||||||
|
export function getRuntimeEnv(key: string): Promise<string | undefined> {
|
||||||
|
console.log("============>>Getting Supabase API URL.")
|
||||||
|
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
if (!_env && typeof window.RUNTIME_ENV !== "undefined") {
|
if (!_env && typeof window.RUNTIME_ENV !== "undefined") {
|
||||||
_env = window.RUNTIME_ENV
|
_env = window.RUNTIME_ENV
|
||||||
console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env);
|
console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 尝试读取缓存的 _env
|
||||||
if (_env) {
|
if (_env) {
|
||||||
console.log("[browser-side]Returning cached API from _env:", _env)
|
console.log("[browser-side] Returning cached API from _env:", _env)
|
||||||
return _env[key]
|
return Promise.resolve(_env[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[browser-side]No window.RUNTIME_ENV found in browser")
|
console.log("[browser-side] No window.RUNTIME_ENV found in browser")
|
||||||
return undefined
|
|
||||||
|
// 延时并重试
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let retries = 0
|
||||||
|
|
||||||
|
// 定义一个重试的函数
|
||||||
|
const tryGetEnv = () => {
|
||||||
|
if (typeof window.RUNTIME_ENV !== "undefined") {
|
||||||
|
_env = window.RUNTIME_ENV
|
||||||
|
console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env)
|
||||||
|
resolve(_env[key]) // 成功获取,返回结果
|
||||||
|
} else {
|
||||||
|
retries += 1
|
||||||
|
if (retries <= MAX_RETRIES) {
|
||||||
|
console.log(`[browser-side] Retry ${retries}/${MAX_RETRIES} - Retrying in ${RETRY_DELAY}ms...`)
|
||||||
|
setTimeout(tryGetEnv, RETRY_DELAY) // 延时重试
|
||||||
|
} else {
|
||||||
|
console.log("[browser-side] Failed to retrieve window.RUNTIME_ENV after retries.")
|
||||||
|
resolve(undefined) // 重试次数耗尽,返回 undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tryGetEnv()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 服务端始终动态读取 process.env
|
// 服务端始终动态读取 process.env
|
||||||
const val = process.env[key]
|
const val = process.env[key]
|
||||||
console.log("[server-side] Falling back to process.env for key:", key, "value:", val)
|
console.log("[server-side] Falling back to process.env for key:", key, "value:", val)
|
||||||
return val
|
return Promise.resolve(val) // 服务端直接返回 process.env 的值
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,30 +8,6 @@
|
||||||
// )
|
// )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// // lib/supabase/browser-client.ts
|
|
||||||
// import { createBrowserClient } from "@supabase/ssr"
|
|
||||||
// import { getRuntimeEnv } from "@/lib/ipconfig"
|
|
||||||
// import { Database } from "@/supabase/types"
|
|
||||||
|
|
||||||
// // 动态获取 URL,防止静态打包成 localhost:8000
|
|
||||||
// const supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "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
|
|
||||||
// )
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// lib/supabase/browser-client.ts
|
// lib/supabase/browser-client.ts
|
||||||
import { createBrowserClient } from "@supabase/ssr"
|
import { createBrowserClient } from "@supabase/ssr"
|
||||||
import { getRuntimeEnv } from "@/lib/ipconfig"
|
import { getRuntimeEnv } from "@/lib/ipconfig"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue