How can I set correct http.FileServer mime types?

Viewed 400

Is there any way to overwrite the content-type used in FileServer? I have simple REST server written in golang, and now i want add some "frontend" in Vue.

Html/js files are served from /static/ folder and almost everything works fine...

Almost, except Vue files.

My /static/ is served by:

fs := http.FileServer(http.Dir("./static/"))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))

all *.vue files are served with Content-Type: text/plain; charset=utf-8, and all browsers throws error:

Failed to load module script:
Expected a JavaScript module script but the server responded with a MIME type of "text/plain". 
Strict MIME type checking is enforced for module scripts per HTML spec.

How overwrite this Content-type? Maybe something like Handler.extension_map in python?

I tried:

log.Println(mime.TypeByExtension(".vue"))
mime.AddExtensionType(".vue", "application/javascript")
log.Println(mime.TypeByExtension(".vue"))
// Returns:
// 2022/01/14 11:45:50 
// 2022/01/14 11:45:50 application/javascript

Which works fine itself, but is apparently ignored by FileServer.

Alternatively, how else to solve this problem?

P.S. Yes, i saw: http.FileServer response with wrong mime "Content-Type" But didn't help in my case.

0 Answers
Related