diff --git a/apps/blogai/components/header.tsx b/apps/blogai/components/header.tsx index b803524..33fe66c 100644 --- a/apps/blogai/components/header.tsx +++ b/apps/blogai/components/header.tsx @@ -43,7 +43,7 @@ import { Trash2 } from "lucide-react"; import { useEffect } from "react"; -import { getRuntimeEnv } from "@/lib/ipconfig"; +// import { getRuntimeEnv } from "@/lib/ipconfig"; import { BadgeInfo, @@ -138,8 +138,43 @@ export function Header() { // } +// import { getRuntimeEnv } from "@/lib/ipconfig"; +// //import { headers } from "next/headers"; + +// export async function getWsBase() { +// let ip = await getRuntimeEnv("SUPABASE_URL"); +// if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 wsBase"); + +// let wsProtocol = "ws"; + +// if ( +// typeof window !== "undefined" && +// window.location && +// window.location.protocol === "https:" +// ) { +// wsProtocol = "wss"; +// ip = window.location.hostname; // ✅ 浏览器下安全替换为域名 +// } else { +// // ✅ SSR 场景 +// const hdrs = headers(); +// const forwardedProto = hdrs.get("x-forwarded-proto"); +// const hostHeader = hdrs.get("host"); + +// if (forwardedProto === "https") { +// wsProtocol = "wss"; +// } + +// if (hostHeader) { +// ip = hostHeader.includes(":") ? hostHeader.split(":")[0] : hostHeader; +// } +// } + +// // 拼接最终 ws 地址 +// return `${wsProtocol}://${ip}/api/v1/deploy/ws`; +// } + + import { getRuntimeEnv } from "@/lib/ipconfig"; -import { headers } from "next/headers"; export async function getWsBase() { let ip = await getRuntimeEnv("SUPABASE_URL"); @@ -152,11 +187,14 @@ export async function getWsBase() { window.location && window.location.protocol === "https:" ) { + // ✅ 浏览器环境下 wsProtocol = "wss"; - ip = window.location.hostname; // ✅ 浏览器下安全替换为域名 + ip = window.location.hostname; } else { - // ✅ SSR 场景 + // ✅ 服务端环境,延迟导入 headers + const { headers } = await import("next/headers"); const hdrs = headers(); + const forwardedProto = hdrs.get("x-forwarded-proto"); const hostHeader = hdrs.get("host"); @@ -169,7 +207,6 @@ export async function getWsBase() { } } - // 拼接最终 ws 地址 return `${wsProtocol}://${ip}/api/v1/deploy/ws`; } diff --git a/apps/blogai/lib/http/get-base-url.ts b/apps/blogai/lib/http/get-base-url.ts index 5566095..6c8637f 100644 --- a/apps/blogai/lib/http/get-base-url.ts +++ b/apps/blogai/lib/http/get-base-url.ts @@ -44,43 +44,61 @@ import { getRuntimeEnv } from "@/lib/ipconfig"; -import { headers } from 'next/headers'; + + +// export async function getBaseUrl() { +// let ip = await getRuntimeEnv("SUPABASE_URL"); +// if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl"); + +// let protocol = "http"; +// let port = 80; + +// if (typeof window !== "undefined") { +// // ✅ CSR 模式 +// if (window.location.protocol === "https:") { +// protocol = "https"; +// ip = window.location.hostname; +// port = 443; +// } +// } else { +// // ✅ SSR 模式,补充逻辑 +// const hdrs = headers(); +// const forwardedProto = hdrs.get("x-forwarded-proto"); +// const hostHeader = hdrs.get("host"); + +// if (forwardedProto) { +// protocol = forwardedProto; +// } + +// if (hostHeader) { +// ip = hostHeader; +// if (hostHeader.includes(":")) { +// const parts = hostHeader.split(":"); +// ip = parts[0]; +// port = parseInt(parts[1]); +// } else { +// port = (protocol === "https") ? 443 : 80; +// } +// } +// } + +// return `${protocol}://${ip}:${port}`; +// } + export async function getBaseUrl() { let ip = await getRuntimeEnv("SUPABASE_URL"); - if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl"); + if (!ip) throw new Error("SUPABASE_URL 获取失败"); - let protocol = "http"; - let port = 80; - - if (typeof window !== "undefined") { - // ✅ CSR 模式 - if (window.location.protocol === "https:") { - protocol = "https"; - ip = window.location.hostname; - port = 443; - } - } else { - // ✅ SSR 模式,补充逻辑 - const hdrs = headers(); - const forwardedProto = hdrs.get("x-forwarded-proto"); - const hostHeader = hdrs.get("host"); - - if (forwardedProto) { - protocol = forwardedProto; - } - - if (hostHeader) { - ip = hostHeader; - if (hostHeader.includes(":")) { - const parts = hostHeader.split(":"); - ip = parts[0]; - port = parseInt(parts[1]); - } else { - port = (protocol === "https") ? 443 : 80; - } - } + if (typeof window === "undefined") { + // ✅ 只在服务端导入(避免构建失败) + const { getServerBaseUrl } = await import("./getServerBaseUrl"); + return getServerBaseUrl(ip); } - return `${protocol}://${ip}:${port}`; + // ✅ 客户端逻辑 + let protocol = window.location.protocol.replace(":", ""); + let hostname = window.location.hostname; + let port = protocol === "https" ? 443 : 80; + return `${protocol}://${hostname}:${port}`; } diff --git a/apps/blogai/lib/http/getServerBaseUrl.ts b/apps/blogai/lib/http/getServerBaseUrl.ts new file mode 100644 index 0000000..ab85f70 --- /dev/null +++ b/apps/blogai/lib/http/getServerBaseUrl.ts @@ -0,0 +1,11 @@ +// lib/http/getServerBaseUrl.ts +import { headers } from "next/headers"; + +export function getServerBaseUrl(defaultIp: string) { + const h = headers(); + const proto = h.get("x-forwarded-proto") || "http"; + const host = h.get("host") || defaultIp; + const port = proto === "https" ? 443 : 80; + + return `${proto}://${host}:${port}`; +}