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