CSS files served as "text/plain" MIME error with Go Templates

Hello,

I am getting an error when executing my index.gohtml template from Go that reads “Refused to apply style from (file path) because its MIME type (‘text/plain’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.” I’m able to render the skeleton of the site but not the styles.

Even if I pass a non-existent link into the href of the .gohtml file, I get the same error. I’ve explicitly made the link type “text/css”. I’m wondering if I need to Set() something in the Go code before executing the .gohtml file? I’ve tried the solution to another forum post which was to .Set(“Content-Type”, “text/css”) but that just spits out my actual HTML code from my file into the browser. Any suggestions?

This is all I’m running in my go function:

func index(w http.ResponseWriter, r *http.Request){
err := tpl.ExecuteTemplate(w, “index.gohtml”, nil)
if err != nil{
panic(err.Error())
}
}

can you attach or point to a minimal working example that reproduces the issue?

Something like this:

package main

import(
	"github.com/gorilla/mux"
	"log"
	"net/http"
	"html/template"
)

var tpl *template.Template

func init(){
	tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
 
func main() {
 
  router := mux.NewRouter()
  
  router.HandleFunc("/", index)

  log.Println("Listening on port 8000...")
  log.Fatal(http.ListenAndServe(":8000", router))
}

func index(w http.ResponseWriter, r *http.Request){
	err := tpl.ExecuteTemplate(w, "index.gohtml", nil)
	if err != nil{
		panic(err.Error())
	}
}

I’m able to serve the normal .gohtml file, just not able to link to a css file. If i don’t serve from go and spin up in a normal web server with node or vscode extension, then it works fine.

Since I do not see any static files being served, what serves css files?

I have it linked in the HTML file. So i serve the HTML file only, and I insert a link to bring in the css file like you would normally on the front end. Is that not the correct way to do it?

So I open a browser and point it to your website, my browser requests the page and your server responds with html, my browser then parses the html and realizes that the page also has resources and requests them too (css, js, images), by links from the response page. Then it uses these resources to build/render the page.

So again, my question is, what if anything returns/serves these (static) resources?

Since you are using gorilla/mux, lookup /static/ on https://www.gorillatoolkit.org/pkg/mux
this should get you going.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.