import type { Hono } from "hono"; import { serveStatic } from "hono/deno"; /** * 托管 React Router SPA 构建产物(build/client):存在的文件直接返回, * 其余非 /api 的 GET 回退 index.html 交给前端路由。/api 下的未知路径仍走 JSON 404。 * 注意 hono/deno 的 serveStatic 要求 path 相对 root,写绝对路径会 404。 */ // biome-ignore lint/suspicious/noExplicitAny: 适配任意 Env 的 Hono 实例 export function mountWebRoot(app: Hono, root: string) { const files = serveStatic({ root }); const index = serveStatic({ root, path: "index.html" }); const isApi = (path: string) => path === "/api" || path.startsWith("/api/"); app.use("*", (c, next) => (isApi(c.req.path) ? next() : files(c, next))); app.get("*", (c, next) => (isApi(c.req.path) ? next() : index(c, next))); }