Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using serve returns incorrect mime types #1030

Closed
icedtoast opened this issue Mar 22, 2021 · 5 comments
Closed

Using serve returns incorrect mime types #1030

icedtoast opened this issue Mar 22, 2021 · 5 comments

Comments

@icedtoast
Copy link

When using the serve feature, esbuild returns the javascript files as "text/plain".
This causes an warning in Firefox:

The script from “http://localhost:8000/example.js” was loaded even though its MIME type (“text/plain”) is not a valid JavaScript MIME type.

More crucially if you are loading the entrypoint as WebWorker it will fail to load, with this error:

Loading Worker from “http://localhost:8000/exampleWorker.js” was blocked because of a disallowed MIME type (“text/plain”)

Is it possible to:
a) Change the serve feature to return the correct mime types (application/javascript for .js files)?
b) Allow customization of the mime types returned?

Also thanks for esbuild, it is amazing how much faster it makes javascript development.

@evanw
Copy link
Owner

evanw commented Mar 22, 2021

That's weird. I get Content-Type: application/javascript and no warnings in Firefox when I try it locally. Can you provide detailed instructions for how to reproduce this?

@icedtoast
Copy link
Author

Yes I thought it was strange too.

I am using Windows 10, if that helps

Steps to reproduce:

  1. Create a javascript file with this content: console.log('hello');
  2. Beside the javascript file add a folder called www
  3. Open cmd.exe
  4. cd to the folder containing your javascript file
  5. Run npx esbuild example.js --servedir=www --outdir=www --bundle
  6. Open localhost:8000 in firefox
  7. Click the javascript file
  8. Open Developer Tools, choose the network tab
  9. Reload the page

Expected:
The javascript file is returned with a content type of application/javascript

Actual:
The javascript file is returned with a content type of text/plain.

You can also recreate with an html that refers to the javascript in the www folder, but I omitted that for brevity.

Screenshot of my developer tools:
image

@evanw
Copy link
Owner

evanw commented Mar 23, 2021

How strange. Go's mime.TypeByExtension function seems to return different values when called on the string ".js". Still investigating.

Edit: Apparently this is a bug in Go: golang/go#32350. Dang. I'll just hard-code a list of mime types into esbuild then.

@evanw evanw closed this as completed in c8eb58f Mar 23, 2021
@jridgewell
Copy link
Contributor

I'll just hard-code a list of mime types into esbuild then.

When you're doing this, can we update the mime-type to be text/javascript? Part of the standardization push for the .mjs extension was to return to text/javascript as the preferred mime type, and MDN has already updated to reflect this. It's also a "willful violation" as part of the HTML spec.

@jridgewell
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants