refactor: improve static file and fallback routing in server.ts

This commit is contained in:
2025-06-04 10:50:27 +09:00
parent baf1ad66a2
commit 993217f8d9

View File

@ -26,7 +26,7 @@ const generalPublicDir = path.join(projectRoot, "public");
const app = new Hono();
// APIルート
// APIルート(順序を最適化)
app.get("/api/feeds", async (c) => {
const rows = db
.query("SELECT feed_url FROM processed_feed_items GROUP BY feed_url")
@ -65,7 +65,6 @@ app.post("/api/episodes/:id/regenerate", (c) => {
app.get("/_next/*", async (c) => {
const assetPath = c.req.path.substring("/_next/".length);
const filePath = path.join(frontendBuildDir, "_next", assetPath);
try {
const file = Bun.file(filePath);
if (await file.exists()) {
let contentType = "application/octet-stream";
@ -75,9 +74,6 @@ app.get("/_next/*", async (c) => {
else if (filePath.endsWith(".jpg") || filePath.endsWith(".jpeg")) contentType = "image/jpeg";
return c.body(file, 200, { "Content-Type": contentType });
}
} catch (e) {
console.error(`Error serving Next.js static file ${filePath}:`, e);
}
return c.notFound();
});
@ -85,14 +81,10 @@ app.get("/_next/*", async (c) => {
app.get("/podcast_audio/*", async (c) => {
const audioFileName = c.req.path.substring("/podcast_audio/".length);
const audioFilePath = path.join(podcastAudioDir, audioFileName);
try {
const file = Bun.file(audioFilePath);
if (await file.exists()) {
return c.body(file, 200, { "Content-Type": "audio/mpeg" });
}
} catch (e) {
console.error(`Error serving audio file ${audioFilePath}:`, e);
}
return c.notFound();
});
@ -113,14 +105,10 @@ app.get("/podcast.xml", async (c) => {
// フォールバックとして index.html
app.get("*", async (c) => {
const indexPath = path.join(frontendBuildDir, "server", "pages", "index.html");
try {
const file = Bun.file(indexPath);
if (await file.exists()) {
return c.body(file, 200, { "Content-Type": "text/html; charset=utf-8" });
}
} catch (e) {
console.error("Error serving index.html:", e);
}
return c.notFound();
});