From 72747d341badd182338b80602a09f083d0400b14 Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Sun, 21 Jun 2026 20:54:44 +0900 Subject: fix(transport-http): set Content-Type on static file responses Bun.file() returns an empty MIME type for .js files, causing the browser to reject module scripts with strict MIME checking. Added an explicit MIME type map for common static file extensions (.js, .css, .html, .svg, .woff2, etc.). --- packages/transport-http/src/app.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/transport-http/src/app.ts b/packages/transport-http/src/app.ts index 9e4fc8a..a9dcbe5 100644 --- a/packages/transport-http/src/app.ts +++ b/packages/transport-http/src/app.ts @@ -678,12 +678,31 @@ export function createApp(opts: CreateServerOptions): Hono { // ─── Static frontend serving (catch-all, API routes take precedence) ────── if (opts.webDir !== undefined) { const webDir = opts.webDir; + const MIME: Record = { + ".js": "text/javascript; charset=utf-8", + ".mjs": "text/javascript; charset=utf-8", + ".css": "text/css; charset=utf-8", + ".html": "text/html; charset=utf-8", + ".json": "application/json; charset=utf-8", + ".svg": "image/svg+xml", + ".png": "image/png", + ".jpg": "image/jpeg", + ".ico": "image/x-icon", + ".woff": "font/woff", + ".woff2": "font/woff2", + ".txt": "text/plain; charset=utf-8", + ".wasm": "application/wasm", + }; app.get("*", async (c) => { const urlPath = new URL(c.req.url).pathname; const filePath = `${webDir}${urlPath}`; const file = Bun.file(filePath); if (await file.exists()) { - return new Response(file); + const ext = filePath.slice(filePath.lastIndexOf(".")); + const contentType = MIME[ext] ?? "application/octet-stream"; + return new Response(file, { + headers: { "Content-Type": contentType }, + }); } // SPA fallback: serve index.html for client-side routing const indexFile = Bun.file(`${webDir}/index.html`); -- cgit v1.2.3