/** * 公开首页与已登录工作台的服务器分流。Cookie存在不是有效会话证明,需向profile端点验证后才重定向。 * 会话无效或服务暂不可达仍显示公开LandingPage,不因为一枚旧Cookie隐藏公开内容。 */ import { cookies } from "next/headers"; import { redirect } from "next/navigation"; import LandingPage from "@/components/landing-page"; /** 异步读取请求Cookie,带15秒时限/禁缓存验证上游;只需成功状态,不消费完整profile JSON。 */ export default async function Home() { const session = (await cookies()).get("foundry_session")?.value; let authenticated = false; if (session) { const origin = process.env.FOUNDRY_API_ORIGIN || "https://api.foundry.entroia.com"; const project = process.env.NEXT_PUBLIC_FOUNDRY_PROJECT || "endoleap"; try { const response = await fetch(`${origin}/projects/${encodeURIComponent(project)}/profile`, { headers: { Authorization: `Bearer ${session}` }, cache: "no-store", redirect: "manual", signal: AbortSignal.timeout(15000), }); // 按响应状态确定是否已认证,再丢弃不需要的body;此处不解析profile字段。 // 先保存 HTTP 成功判定;随后 cancel 若抛错,catch 不会把 authenticated 重置为 false。 authenticated = response.ok; await response.body?.cancel(); } catch { // An unverified session must not hide the public landing page. } } // redirect 放在上方 try/catch 外,避免把框架用于导航的控制流异常当作 profile 请求失败吞掉。 // 仅在确认成功后跳工作台,未认证时沿用同一静态展示组件。 if (authenticated) redirect("/dashboard"); return ; }