How to force mime type on js files with apache?

1.3k views Asked by At

I host my react app on uberspace. They use apache and have X-Content-Type-Options: nosniff enabled. Which I like. However when I host my react app I get the following error in firefox console: The resource from “url...” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff). I figured out that the mimetype of the js is for some reason text/html. Saw that in network tab in the firefox developer tools. I added AddType text/javascript js to my .htaccess, but it doesn't help. I think the problem is, that the js is served as html mimetype and I don't know how to force it to be mimetype js.

Any ideas? Thank you

2

There are 2 answers

0
Jax Cooper On

I might be late but I solved my similar problem.

Check if the file path is correct or that the file is there.

I used SvelteKit static build and I only uplouaded the index.html file and not the _assets which contain files with new randomly generated filenames.

Reference: Resource blocked due to MIME type mismatch (X-Content-Type-Options: nosniff) dav's answer

0
aaronk6 On

Make sure you’re actually requesting the correct URL to the JavaScript file.

All reasonably configured web servers should return Content-Type: application/javascript for .js files by default. There shoudn’t be the need to override the standard configuration using an .htaccess file.

I had the same issue, but when I looked closely, I noticed a 404 error that occurred right before the script error:

Refused to execute script

So the browser is (rightfully) refusing to parse the 404 error document as a script.