This commit is contained in:
hailin 2025-05-20 22:35:11 +08:00
parent e37fbd129a
commit b6f22fda2c
1 changed files with 15 additions and 4 deletions

View File

@ -10,12 +10,23 @@
let _env: Record<string, string> | null = null
export function getRuntimeEnv(key: string): string | undefined {
// 强制每次从 window.RUNTIME_ENV 获取,而不依赖缓存
if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") {
_env = window.RUNTIME_ENV // 更新 _env确保使用最新的环境变量
// 打印缓存的 _env 变量和每次调用时的 key
console.log("Checking env for key:", key);
// 如果 _env 存在,打印出缓存的 _env 并返回对应的值
if (_env) {
console.log("Returning cached _env:", _env);
return _env[key]
}
// 如果没有 window.RUNTIME_ENV回退到 process.env
// 如果 window.RUNTIME_ENV 存在,打印出从 window.RUNTIME_ENV 获取的值
if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") {
_env = window.RUNTIME_ENV // 更新 _env 为最新的环境变量
console.log("Updated _env from window.RUNTIME_ENV:", _env);
return _env[key]
}
// 如果没有 window.RUNTIME_ENV回退到 process.env打印出 process.env 和当前 key
console.log("Falling back to process.env for key:", key);
return process.env[key]
}