From 27ea4f576c0f04dc10691fab7f9df18decae48f2 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 25 Jun 2025 17:45:45 +0800 Subject: [PATCH] . --- chatdesk-ui/lib/get-runtime-env.ts | 24 ------ chatdesk-ui/lib/runtime-env/client.ts | 4 + chatdesk-ui/lib/runtime-env/server.ts | 13 ++++ chatdesk-ui/lib/supabase/browser-client.ts | 88 ++++++++++++++++------ 4 files changed, 83 insertions(+), 46 deletions(-) delete mode 100644 chatdesk-ui/lib/get-runtime-env.ts create mode 100644 chatdesk-ui/lib/runtime-env/client.ts create mode 100644 chatdesk-ui/lib/runtime-env/server.ts diff --git a/chatdesk-ui/lib/get-runtime-env.ts b/chatdesk-ui/lib/get-runtime-env.ts deleted file mode 100644 index db3bc21..0000000 --- a/chatdesk-ui/lib/get-runtime-env.ts +++ /dev/null @@ -1,24 +0,0 @@ -// lib/get-runtime-env.ts -import { headers } from "next/headers" - -export function getRuntimeEnv(key: string): string | undefined { - // 包装成闭包 → 防止构建期静态优化 - const getFromEnv = () => process.env[key] - - if (typeof window === "undefined") { - // SSR 逻辑 - if (key === "SUPABASE_URL") { - const h = headers() - const proto = h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http") - const rawHost = h.get("x-forwarded-host") ?? h.get("host")! - const hostname = rawHost.split(",")[0].split(":")[0].trim() - return `${proto}://${hostname}:8000` - } - - // 默认 fallback 到 env - return getFromEnv() - } - - // 客户端 - return (window as any).RUNTIME_ENV?.[key] -} diff --git a/chatdesk-ui/lib/runtime-env/client.ts b/chatdesk-ui/lib/runtime-env/client.ts new file mode 100644 index 0000000..4ab2c9e --- /dev/null +++ b/chatdesk-ui/lib/runtime-env/client.ts @@ -0,0 +1,4 @@ +// lib/runtime-env/client.ts +export function getRuntimeEnvClient(key: string): string | undefined { + return (window as any).RUNTIME_ENV?.[key] +} diff --git a/chatdesk-ui/lib/runtime-env/server.ts b/chatdesk-ui/lib/runtime-env/server.ts new file mode 100644 index 0000000..0b64b01 --- /dev/null +++ b/chatdesk-ui/lib/runtime-env/server.ts @@ -0,0 +1,13 @@ +// lib/runtime-env/server.ts +import { headers } from "next/headers" + +export function getRuntimeEnvServer(key: string): string | undefined { + if (key === "SUPABASE_URL") { + const h = headers() + const proto = h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http") + const rawHost = h.get("x-forwarded-host") ?? h.get("host")! + const hostname = rawHost.split(",")[0].split(":")[0].trim() + return `${proto}://${hostname}:8000` + } + return process.env[key] +} diff --git a/chatdesk-ui/lib/supabase/browser-client.ts b/chatdesk-ui/lib/supabase/browser-client.ts index 6c076c4..a3d5540 100644 --- a/chatdesk-ui/lib/supabase/browser-client.ts +++ b/chatdesk-ui/lib/supabase/browser-client.ts @@ -53,40 +53,84 @@ +// import { createBrowserClient } from "@supabase/ssr" +// //import { getRuntimeEnv } from "@/lib/ipconfig" +// import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 +// import { Database } from "@/supabase/types" + +// // 动态获取 URL,防止静态打包成 localhost:8000 +// let supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" + +// // 仅在浏览器端使用 localStorage +// if (typeof window !== "undefined") { +// // 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage +// if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) { +// localStorage.setItem("supabaseUrl", supabaseUrl) +// } else { +// // 如果是 http://localhost:8000 或者 undefined,则从 localStorage 获取 +// const storedUrl = localStorage.getItem("supabaseUrl") +// if (storedUrl && storedUrl !== "http://localhost:8000") { +// supabaseUrl = storedUrl +// } else { +// supabaseUrl = "http://localhost:8000" +// } +// } +// } else { +// // 如果在服务器端,使用默认或从环境变量获取的 URL +// console.log("[server-side] Falling back to default supabaseUrl:", supabaseUrl) +// } + +// // 打印获取的 URL +// console.log("=======>>Supabase URL:", supabaseUrl); + +// // 编译时固定匿名 key(前端安全公开) +// const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! + +// // 导出单例,兼容所有调用旧代码方式 +// export const supabase = createBrowserClient( +// supabaseUrl, +// supabaseAnonKey +// ) + + + import { createBrowserClient } from "@supabase/ssr" -//import { getRuntimeEnv } from "@/lib/ipconfig" -import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 import { Database } from "@/supabase/types" -// 动态获取 URL,防止静态打包成 localhost:8000 -let supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" +let supabaseUrl = "http://localhost:8000" -// 仅在浏览器端使用 localStorage -if (typeof window !== "undefined") { - // 如果 URL 不是 http://localhost:8000 且不为 undefined,则存储到 localStorage - if (supabaseUrl !== "http://localhost:8000" && supabaseUrl !== undefined) { - localStorage.setItem("supabaseUrl", supabaseUrl) +// ✅ 动态导入 SSR 版本的 getRuntimeEnv,仅在服务器端执行 +if (typeof window === "undefined") { + (async () => { + const { getRuntimeEnvServer } = await import("@/lib/runtime-env/server") + const url = getRuntimeEnvServer("SUPABASE_URL") + if (url) { + supabaseUrl = url + console.log("[SSR] Using SUPABASE_URL from getRuntimeEnvServer:", url) + } + })() +} else { + // ✅ 客户端环境,从 window.RUNTIME_ENV 或 localStorage 获取 + const runtimeEnv = (window as any).RUNTIME_ENV + const envUrl = runtimeEnv?.SUPABASE_URL + + if (envUrl && envUrl !== "http://localhost:8000") { + supabaseUrl = envUrl + localStorage.setItem("supabaseUrl", envUrl) } else { - // 如果是 http://localhost:8000 或者 undefined,则从 localStorage 获取 - const storedUrl = localStorage.getItem("supabaseUrl") - if (storedUrl && storedUrl !== "http://localhost:8000") { - supabaseUrl = storedUrl - } else { - supabaseUrl = "http://localhost:8000" + const stored = localStorage.getItem("supabaseUrl") + if (stored && stored !== "http://localhost:8000") { + supabaseUrl = stored } } -} else { - // 如果在服务器端,使用默认或从环境变量获取的 URL - console.log("[server-side] Falling back to default supabaseUrl:", supabaseUrl) -} -// 打印获取的 URL -console.log("=======>>Supabase URL:", supabaseUrl); + console.log("[CSR] Using SUPABASE_URL:", supabaseUrl) +} // 编译时固定匿名 key(前端安全公开) const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! -// 导出单例,兼容所有调用旧代码方式 +// 导出单例,兼容所有旧代码方式 export const supabase = createBrowserClient( supabaseUrl, supabaseAnonKey