diff options
| author | Adam Malczewski <[email protected]> | 2026-06-21 20:54:44 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-21 20:54:44 +0900 |
| commit | 72747d341badd182338b80602a09f083d0400b14 (patch) | |
| tree | 69d8d29742934055a092958e54201183de6655b7 | |
| parent | 6adf0f99431ea3ff430a73082d8296779abe9760 (diff) | |
| download | dispatch-72747d341badd182338b80602a09f083d0400b14.tar.gz dispatch-72747d341badd182338b80602a09f083d0400b14.zip | |
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.).
| -rw-r--r-- | packages/transport-http/src/app.ts | 21 |
1 files changed, 20 insertions, 1 deletions
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<string, string> = { + ".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`); |
