Kwabena Muriuki
Kwabena Muriuki

Reputation: 121

Getting MIME type ('text/plain') error in Golang while serving CSS

I am building my first Go web project and I am getting this error on the browser console when I load my page

Refused to apply style from 'http://localhost:8080/static/css/processor-auth.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

I am not sure what I am doing wrong because I have already added this code to load the static files

http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

This is how my main.go file looks like:

 package main

import(
    "net/http"
    "os"
    "html/template"

    "github.com/julienschmidt/httprouter"
)


// Auth struct handler
type auth struct{}

func (auth *auth) ServeHTTP(w http.ResponseWriter, r *http.Request){
    wd,_:= os.Getwd()
    t := template.Must(template.ParseFiles(wd+"/templates/processor/auth.html"))
    err:=t.Execute(w,nil)

    if err !=nil{
        http.Error(w,"Could not execute template",500)
    }

}


func main(){

    router:= httprouter.New()
    // set the static files
    http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

    router.Handler("GET","/auth",&auth{})

    server := http.Server{
        Addr:"127.0.0.1:8080",
        Handler:router,
    }

    server.ListenAndServe()
}

Folder Structure

Edit: Solved the issue

Since I was using httprouter as my multiplexer I could not use

http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

I had to update to the httprouter's ServeFiles function and update the code to router.ServeFiles("/static/*filepath",http.Dir("static"))

Upvotes: 7

Views: 6734

Answers (3)

Nux
Nux

Reputation: 7098

I got this problem on windows and I fixed it by

func FixMimeTypes() {
    err1 := mime.AddExtensionType(".js", "text/javascript")
    if err1 != nil {
        log.Printf("Error in mime js %s", err1.Error())
    }

    err2 := mime.AddExtensionType(".css", "text/css")
    if err2 != nil {
        log.Printf("Error in mime js %s", err2.Error())
    }
}

Credit here

Upvotes: 0

Gregory Ledray
Gregory Ledray

Reputation: 1207

Why does this happen?

This error shows up because Go is auto-detecting the content type of the file. To do the auto-detection it uses a map which points from the file extension (like .js) -> MIME type (like text/plain). To get this map it reads it from the local machine. So if your local machine has an incorrect value in its registry (or your OS's equivalent) for .css files and you're using code which auto-detects the MIME type of the file being served then this can happen.

What is setting the registry incorrectly?

I experience incorrect registry values when I re-install or uninstall Visual Studio.

Windows Fix

You need to edit the registry entries using regedit so that that the "Content Type" registry key points to the correct value. You can find extension keys in 2 places:

HKEY_CLASSES_ROOT contains a list. In my case, I looked up .js in that list and changed its value from text/plain to application/javascript. In the original poster's place, it looks like the error was in .css, so you'd set the HKEY_CLASSES_ROOT\.css key "Content Type" to text/css.

HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES also contains a list. You should update it in the same way so that it matches HKEY_CLASSES_ROOT. In my case, this was already correctly set to application/javascript so I assume it's not the first registry value Go is reading.

References

Go Issue: https://github.com/golang/go/issues/32350

Upvotes: 1

Isaac Weingarten
Isaac Weingarten

Reputation: 1190

I am using a windows machine (windows 10 & windows server 2019) and I had the same issue on javascript files, I went into the registry \HKEY_CLASSES_ROOT\.js > "Content Type" and i changed it from "text/plain" to "application/javascript" and i restart the PC and that fixed it

Upvotes: 5

Related Questions