-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
mime: incorrect mime-type for .js files on windows 10 (text/plain instead of application/javascript) #32350
Comments
Reversing the order would be a pretty heavy-handed fix. Local system configuration should have precedence over the builtin defaults. Does this issue reproduce on a clean Windows install? |
This issue started to appear out of the blue on a Windows 10 1903 install (go 1.12.9). |
A terrible work-around:
|
Sorry, it may not be default configuration.
|
Interesting, I wonder what is changing this. Thanks for the pointer.
|
As a result that I swimed in internet for 10 minutes, I figure out this value of the registory key seems to be possibly changed by some text editor or casual confguration. https://timothytocci.com/tag/registry/ (text/plain) https://www.jianshu.com/p/a443991462d7 (application/x-javascript) And it is not registered in default on Windows 10. I think this should not be set from registory. Or should be non-overwritable. |
Change https://golang.org/cl/191917 mentions this issue: |
builtinTypesLower is safe. it is not overwritable. mime.types or registory is NOT safe. This can be added to mimeTypes easily. (ex with installing text editor on Windows). So cl191917 disable overwrite to mimeTypes from mime.types or registory. What about this? |
Currently this issue prevents service worker to load as browsers block it if it's content type is not JavaScript |
Hey, just wanted to mention that I have created a little tool to check if the problem is also caused on your machine and to fix the issue by setting the mentioned registry key to the expected value |
Hi If you are looking for a temporary solution, please import the following registry items:
IMHO this one is caused by system, but not golang itself. Did I miss something? |
There seems to be some evolution.
These values appear in line with Mozilla documentation. |
The requirement of the mime package should be to correctly ascertain the mime type of common files, regardless of Windows registry settings. If a web server can't properly serve up a For |
Same problem persists in Win 10 with go 1.15.3 |
Same problem on Go |
cc @neild for decision, though i believe this is working as intended |
Unfortunately, the docs contain this sentence: "On Windows, MIME types are extracted from the registry." This means that Microsoft can and did break the behavior of One could make the argument that the requirement for
Arguably, the current implementation is incorrect because it depends on an unreliable operating system feature. Adherence to the expectations of millions of developers and to industry standards should be the requirement. The requirement should not be to comply with an unreliable Windows feature. Changing the function to ignore Microsoft's insanity would be a breaking change, but maybe a breaking change is better than a function whose implementation allows it to be broken by Microsoft. Therefore, for crucial file types -- html, css, and js -- perhaps the mime types should be hardcoded. Yes, a breaking change. But a breaking change that fixes and prevents breakage. |
also @mattn has a PR to disallow the override of this type by default with the option to do it if needed via an argument to |
Same problem in Windows 11. Go 1.17.5 fs := http.FileServer(http.Dir("./static"))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
log.Fatal(http.ListenAndServe(":8080", router)) In the static folder are some js/vue files, but all
|
Windows is just being Windows, wasted half an hour figuring out why JS was served as text/plain |
I had the same problem and I borrowed dvaldivia solution. // Execute before the service runs.
func init () {
_ = mime.AddExtensionType(".js", "text/javascript")
} |
Change https://go.dev/cl/406894 mentions this issue: |
This situation is clearly unfortunate but I don't think that changing the mime package is the right approach. In general local system configuration should override defaults, not the other way around. I don't know why Windows is providing a strange configuration, but overriding the local configuration is just going to produce a different set of strange bugs. The problem is not unique to Go; here is the same problem reported for Python: https://bugs.python.org/issue43975 , https://www.taricorp.net/2020/windows-mime-pitfalls/ . This bug report says that it is a problem with Visual Studio: https://developercommunity.visualstudio.com/t/installer-mime-type-for-js-extension-is-set-to-tex/954263 . If we do something here it should be a very focused fix on this specific problem. I suggest https://go.dev/cl/406894. Can somebody who is encountering this problem on their Windows system please see if that patch fixes it? Thanks. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Consider this program:
https://play.golang.org/p/JTVjWc9xMDE
What did you expect to see?
Mime-type for .js files should be
application/javascript
on all platforms.What did you see instead?
On Linux it returns
ct: application/javascript
On my latest Windows 10 it returns
ct: text/plain; charset=utf-8
This also affects
Content-Type
returned byhttp.ServeFile()
for.js
files.This is because in
src\mime\type.go
:On Windows
osInitMime()
reads info for additional extensions from registry and over-writes mime-type for.js
files.Note: it's possible this won't repro on every version on Windows.
Reversing the initialization order should fix it:
The text was updated successfully, but these errors were encountered: