This commit is contained in:
hailin 2025-06-25 17:45:45 +08:00
parent 62b0dbd40b
commit 27ea4f576c
4 changed files with 83 additions and 46 deletions

View File

@ -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]
}

View File

@ -0,0 +1,4 @@
// lib/runtime-env/client.ts
export function getRuntimeEnvClient(key: string): string | undefined {
return (window as any).RUNTIME_ENV?.[key]
}

View File

@ -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]
}

View File

@ -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<Database>(
// 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)
} else {
// 如果是 http://localhost:8000 或者 undefined则从 localStorage 获取
const storedUrl = localStorage.getItem("supabaseUrl")
if (storedUrl && storedUrl !== "http://localhost:8000") {
supabaseUrl = storedUrl
} else {
supabaseUrl = "http://localhost:8000"
}
// ✅ 动态导入 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 {
// 如果在服务器端,使用默认或从环境变量获取的 URL
console.log("[server-side] Falling back to default supabaseUrl:", supabaseUrl)
// ✅ 客户端环境,从 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 {
const stored = localStorage.getItem("supabaseUrl")
if (stored && stored !== "http://localhost:8000") {
supabaseUrl = stored
}
}
// 打印获取的 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<Database>(
supabaseUrl,
supabaseAnonKey