Springboot - Resource Interpreted as Stylesheet But Transferred with Mime Type Text/Htm

Springboot - Resource interpreted as Stylesheet but transferred with MIME type text/htm

I had the exact same issue while writing some code with Spring Boot + Spring MVC. The CSS files set using a CDN worked fine, while the CSS file set from my static/css folder returned a HTML content.

Example:

<!-- This first worked fine, loading all styles -->
<link th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"
href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet" media="screen" />

<!-- This second one returned the content of an HTML - Content Type text/html -->
<link rel="stylesheet" th:href="@{/css/login/style.css}" href="/css/login/style.css" />

After a while I could see using Chrome Dev Tools that the content returned for my local style.css was the same as one of my HTML pages.

Inspecting the route that pointed to the HTML file with that content, I could realise I was using the wrong property for the @RequestMapping configuration. I had @RequestMapping(name="..."), instead of @RequestMapping(path="...").

Controller with the problem

@RequestMapping(name = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("product", new Product());
return "product/edit";
}

Controller changed

@RequestMapping(path = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
model.addAttribute("product", new Product());

return "product/edit";
}

After changing the property name with path everything started to load correctly.

It was strange how a small mistake like this affected my whole program.

Hope it's somehow useful for someone who faces the same issue.

Resource interpreted as stylesheet but transferred with MIME type text/html (seems not related with web server)

i'd like to start by understanding the problem

Browsers make HTTP requests to servers. The server then makes an HTTP response.

Both requests and responses consist of a bunch of headers and a (sometimes optional) body with some content in it.

If there is a body, then one of the headers is the Content-Type which describes what the body is (is it an HTML document? An image? The contents of a form submission? etc).

When you ask for your stylesheet, your server is telling the browser that it is an HTML document (Content-Type: text/html) instead of a stylesheet (Content-Type: text/css).

I've already checked my myme.type and text/css is already on css.

Then something else about your server is making that stylesheet come with the wrong content type.

Use the Net tab of your browser's developer tools to examine the request and the response.

Resource interpreted as Stylesheet but transferred with MIME type text/javascript

I had the exact same problem, cause by lines in my HTML in the following form:

<link rel="stylesheet" type="text/css" href="css/my_css_resource.css" />

The problem was fixed when I removed rel="stylesheet" from the tag.

The .css file turns to become an HTML (thymeleaf) where it was embedded to

The problem actually was solved by setting the "path" instead of the "name" parameter to controller's @RequestMapping. The answer is here: Springboot - Resource interpreted as Stylesheet but transferred with MIME type text/htm

Interface/enum listing standard mime-type constants

Guava library

We have a Guava class for this: com.google.common.net.MediaType.

It was released with Guava 12 as stated in the source code and in Issue 823. Sources are available, too.



Related Topics



Leave a reply



Submit