diff --git a/chatdesk-ui/lib/ipconfig.ts b/chatdesk-ui/lib/ipconfig.ts index 13af269..6e269e9 100644 --- a/chatdesk-ui/lib/ipconfig.ts +++ b/chatdesk-ui/lib/ipconfig.ts @@ -9,86 +9,85 @@ +// let _env: Record | 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 | null = null + +// 最大重试次数 +const MAX_RETRIES = 5 +// 每次重试的延时(毫秒) +const RETRY_DELAY = 1000 + 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); + console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env) } + // 尝试读取缓存的 _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] } - console.log("[browser-side]No window.RUNTIME_ENV found in browser") - return undefined + console.log("[browser-side] No window.RUNTIME_ENV found in browser") + + let retries = 0 + // 定义一个同步延时重试的函数 + const tryGetEnvSync = () => { + if (typeof window.RUNTIME_ENV !== "undefined") { + _env = window.RUNTIME_ENV + console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env) + return _env[key] // 成功获取,返回结果 + } else { + retries += 1 + if (retries <= MAX_RETRIES) { + console.log(`[browser-side] Retry ${retries}/${MAX_RETRIES} - Retrying in ${RETRY_DELAY}ms...`) + // 延时并重试 + setTimeout(tryGetEnvSync, RETRY_DELAY) + } else { + console.log("[browser-side] Failed to retrieve window.RUNTIME_ENV after retries.") + return undefined // 重试次数耗尽,返回 undefined + } + } + } + + return tryGetEnvSync() // 调用同步重试方法 } // 服务端始终动态读取 process.env const val = process.env[key] console.log("[server-side] Falling back to process.env for key:", key, "value:", val) - return val + return val // 服务端直接返回 process.env 的值 } - - - - -// let _env: Record | null = null - -// // 最大重试次数 -// const MAX_RETRIES = 5 -// // 每次重试的延时(毫秒) -// const RETRY_DELAY = 1000 - -// export function getRuntimeEnv(key: string): Promise { -// 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) -// } - -// // 尝试读取缓存的 _env -// if (_env) { -// console.log("[browser-side] Returning cached API from _env:", _env) -// return Promise.resolve(_env[key]) -// } - -// console.log("[browser-side] No window.RUNTIME_ENV found in browser") - -// // 延时并重试 -// 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 -// const val = process.env[key] -// console.log("[server-side] Falling back to process.env for key:", key, "value:", val) -// return Promise.resolve(val) // 服务端直接返回 process.env 的值 -// }