honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
383 stars 136 forks source link

@hono/esbuild-transpiler responds content-type video/mp2t #253

Open Code-Hex opened 9 months ago

Code-Hex commented 9 months ago

Hi 👋

I tried using @hono/esbuild-transpiler. I'm using Cloudflare Workers and confirmed it with wrangler dev. When returning a .ts file from the server, it comes back with a content-type of video/mp2t.

const app = new Hono()

app.get(
  '/static/:scriptName{.+.tsx?}',
  esbuildTranspiler({ wasmModule: wasm, contentType: 'text/javascript' }),
);
app.get('/static/*', serveStatic({ root: './' }));
$ curl http://localhost:8787/static/script.ts -i
HTTP/1.1 200 OK
Content-Length: 381
Content-Type: video/mp2t

import Alpine from "https://esm.sh/alpinejs@3.13.2";
window.Alpine = Alpine;
document.addEventListener("alpine:init", () => {
  Alpine.directive(
    "destroy",
    (_el, { expression }, { evaluateLater, cleanup }) => {
      const clean = evaluateLater(expression);
      cleanup(() => clean());
    }
  );
  Alpine.store("auth", {
    currentUser: null
  });
});
Alpine.start();
yusukebe commented 9 months ago

Hi @Code-Hex!

This issue arises because serve-static identifies .ts files as video/mp2t:

https://github.com/honojs/hono/blob/b1581430754d8ec6a340a781beaaefb985a4057a/src/utils/mime.ts#L71

I believe it's better to maintain this specification in serve-static. Alternatively, we can have the @hono/esbuild-transpiler set the correct content type for .ts files.