This commit is contained in:
hailin 2025-06-15 13:10:58 +08:00
parent 8d6ed1c5c4
commit 8f408e03a9
2 changed files with 36 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import { useTranslation } from "react-i18next";
import { DetailPageHeader } from '@/components/header' import { DetailPageHeader } from '@/components/header'
import { getRuntimeEnv } from "@/lib/ipconfig"; import { getRuntimeEnv } from "@/lib/ipconfig";
import { getBaseUrl } from "@/lib/http/get-base-url"; // ✅ 不是 export 它,而是 import 用
export const runtime = "nodejs"; export const runtime = "nodejs";
@ -26,25 +27,25 @@ type Props = {
searchParams: { [key: string]: string | string[] | undefined }; searchParams: { [key: string]: string | string[] | undefined };
}; };
// 推荐用法async 获取 // // 推荐用法async 获取
export async function getBaseUrl() { // export async function getBaseUrl() {
let ip = await getRuntimeEnv("SUPABASE_URL"); // let ip = await getRuntimeEnv("SUPABASE_URL");
if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl"); // if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl");
// 判断协议 // // 判断协议
let protocol = "http"; // let protocol = "http";
if ( // if (
typeof window !== "undefined" && // typeof window !== "undefined" &&
window.location && // window.location &&
window.location.protocol === "https:" // window.location.protocol === "https:"
) { // ) {
protocol = "https"; // protocol = "https";
ip = window.location.hostname; // ✅ HTTPS 场景下安全替换 IP 为域名 // ip = window.location.hostname; // ✅ HTTPS 场景下安全替换 IP 为域名
} // }
// 拼接 // // 拼接
return `${protocol}://${ip}`; // return `${protocol}://${ip}`;
} // }
export async function generateMetadata({ params }: Props): Promise<Metadata> { export async function generateMetadata({ params }: Props): Promise<Metadata> {

View File

@ -0,0 +1,18 @@
import { getRuntimeEnv } from "@/lib/ipconfig";
export async function getBaseUrl() {
let ip = await getRuntimeEnv("SUPABASE_URL");
if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl");
let protocol = "http";
if (
typeof window !== "undefined" &&
window.location &&
window.location.protocol === "https:"
) {
protocol = "https";
ip = window.location.hostname;
}
return `${protocol}://${ip}`;
}