From 2dcc050d55db1f53c9cffe345944b07a003c4c4a Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 21 May 2025 19:48:45 +0800 Subject: [PATCH] . --- chatdesk-ui/lib/ipconfig.ts | 131 ++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/chatdesk-ui/lib/ipconfig.ts b/chatdesk-ui/lib/ipconfig.ts index ba058e3..13af269 100644 --- a/chatdesk-ui/lib/ipconfig.ts +++ b/chatdesk-ui/lib/ipconfig.ts @@ -7,87 +7,88 @@ // } -// 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): Promise { +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) - return Promise.resolve(_env[key]) + 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 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() - }) + 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 Promise.resolve(val) // 服务端直接返回 process.env 的值 + return val } + + + + +// 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 的值 +// }