ff98547dbb
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
24 lines
735 B
TypeScript
24 lines
735 B
TypeScript
import type { Hono } from "hono";
|
|
import type { HttpBindings } from "@hono/node-server";
|
|
import { serveStatic } from "@hono/node-server/serve-static";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
type App = Hono<{ Bindings: HttpBindings }>;
|
|
|
|
export function serveStaticFiles(app: App) {
|
|
const distPath = path.resolve(import.meta.dirname, "../dist/public");
|
|
|
|
app.use("*", serveStatic({ root: distPath }));
|
|
|
|
app.notFound((c) => {
|
|
const accept = c.req.header("accept") ?? "";
|
|
if (!accept.includes("text/html")) {
|
|
return c.json({ error: "Not Found" }, 404);
|
|
}
|
|
const indexPath = path.resolve(distPath, "index.html");
|
|
const content = fs.readFileSync(indexPath, "utf-8");
|
|
return c.html(content);
|
|
});
|
|
}
|