Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Browser blocks a stylesheet or script: MIME type mismatch with nosniff

ErrorThe stylesheet or script does not load and the console reports a MIME type mismatch with X-Content-Type-Options: nosniff

Short answer

With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type. If your Express app answers the asset URL with an HTML 404 page or a wrong type, the file is blocked. Find what the URL really returns, then fix the static path or route.

  • Applies to Express 4, Express 5, Node.js 22, Chrome, Firefox
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the blocked URL directly and read the status and Content-Type. A stylesheet that returns text/html is usually an error page, not your CSS.

    curl -i http://localhost:3000/styles.css | head -n 12
  2. 2Match what you see, then jump to its fix

    1. 1curl shows 404 or 200 with Content-Type: text/html for the CSS or JS URLThe file is not found, so the server answers with an HTML pageFix
    2. 2Every missing asset returns your app's HTML, with a 200 statusA catch-all route sends index.html for every unknown pathFix
    3. 3curl shows 200 with Content-Type text/plain or application/octet-streamThe file exists but is sent with the wrong Content-TypeFix

What does the error look like?

  • The stylesheet or script does not load and the console reports a MIME type mismatch with X-Content-Type-Options: nosniff
  • The Network tab shows the asset URL answered with Content-Type text/html

Cause 1 of 3

The file is not found, so the server answers with an HTML page

1Check

express.static looks files up relative to the folder you give it, and that folder name is not part of the URL. If the URL and the folder do not line up, the file is not found.

ls public/
grep -n "express.static" server.js

2Fix

Make the URL in your HTML match the mounted folder, and use an absolute folder path so the lookup does not depend on where node was started.

const path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
// public/css/site.css is served at /css/site.css

3Verify

The URL now answers 200 with text/css.

curl -I http://localhost:3000/css/site.css

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 3

A catch-all route sends index.html for every unknown path

1Check

A single-page app server often ends with a handler that returns index.html. When express.static does not find a file it passes the request on, and that handler answers instead.

grep -n "index.html" server.js

2Fix

Register express.static before the fallback handler, and in the fallback only answer paths that are not asset requests. serve-static also has a fallthrough option; set it to false on a mount that maps to one folder to get an error instead of a silent pass.

app.use('/assets', express.static(path.join(__dirname, 'public/assets'), { fallthrough: false }));

3Verify

A missing asset now gives an error, not HTML, and an existing asset still loads.

curl -I http://localhost:3000/assets/missing.css
curl -I http://localhost:3000/assets/site.css

Confirmed by github.com

Did this fix it?

Cause 3 of 3

The file exists but is sent with the wrong Content-Type

1Check

nosniff stops the browser from guessing. For a style request the browser blocks anything other than text/css, and for a script anything that is not a JavaScript type.

curl -sI http://localhost:3000/css/site.css | grep -i content-type

2Fix

Serve the file through express.static, or set the type yourself in a custom route. Do not remove the nosniff header; the fix is the correct type.

app.get('/css/site.css', (req, res) => {
  res.type('text/css');
  res.sendFile(path.join(__dirname, 'public/css/site.css'));
});

3Verify

The header now reads text/css, and the page styles load.

curl -sI http://localhost:3000/css/site.css | grep -i content-type

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Always mount static folders with an absolute path built from __dirname.
  • Put API routes and express.static before any catch-all that returns index.html.
  • After each deploy, curl one CSS and one JS URL and check status and Content-Type.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.