Basics

What is a MIME type — and why the browser refused to run your script

Illustration: a parcel with two labels — one on the box, a different one on the paperwork

Here's the puzzle everyone gets stuck on. The file is called app.js. It's in place, it opens by direct link, the contents are correct. But the browser says:

Refused to execute script … because its MIME type ('text/html') is not executable

And it won't budge. Even though the extension is .js, honest.

The thing is, the browser doesn't care about the file extension. It looks somewhere else entirely.

The extension is for you, the MIME type is for the browser

Extensions like .js, .png, .pdf are an agreement between you and your operating system. Over the network, files travel without names or extensions: a stream of bytes arrives. So the response carries a header to tell the receiver what it is:

Content-Type: text/javascript

That value is the MIME type — a short format label shaped as "category/subtype." The category gives the family: text, image, video, application. The subtype narrows it down: text/html, image/png, application/json.

The name is inherited from email: MIME was invented in the nineties so messages could carry more than plain Latin text. From there the mechanism moved to the web and stayed.

The practical takeaway: the filename is the writing on the box, the MIME type is the shipping manifest. The browser trusts the manifest. Renaming photo.png to photo.jpg changes nothing; changing the server's header changes everything.

Where "Refused to execute script" comes from

Now the answer. That error almost always means: the server sent an HTML page instead of a script.

The classic scenario. You've deployed a single-page app. Those are configured with a rule: "if the requested path doesn't exist, serve index.html" — otherwise refreshing the page on an inner route gives a 404.

Then this happens: the page asks for /assets/app.js, and that file isn't on the server — maybe the build references paths from the root while the site sits in a subfolder. The rule fires honestly and serves index.html as text/html. The browser gets a 200 — not an error! — sees "this is HTML" on the manifest, and refuses to execute it as a script.

So the first move: open the failing file's URL directly in a new tab. If you see the HTML of your own page, the diagnosis is confirmed — there's simply no file at that path.

Why the browser stopped guessing

Fair question: the browser can see JavaScript inside. Why not just figure it out?

It used to — that was MIME sniffing. And it became a security hole. Picture a site where users upload avatars. An attacker uploads a file that looks like an image on the outside but contains a script inside. The server serves it as image/png, the browser sniffs it, decides "ah, a script" — and runs someone else's code on your domain.

To stop that, a header was introduced:

X-Content-Type-Options: nosniff

It means "don't guess, trust the manifest." Today it's on almost everywhere by default, and browsers are strict about scripts and styles even without it. Hence that blunt "is not executable": the browser doesn't argue with the type, it just declines.

Where you'll meet MIME types besides errors

Not only in breakage — it shows up in three everyday places.

  • When you send data yourself. Sending a request with a body, you must set Content-Type: application/json, or the server will parse the body wrong or not at all.
  • When you accept files. The accept attribute on a file input and your server-side validation both speak MIME types. Important: the type the browser sends comes from the client and is forgeable — on the server, check the format by content.
  • When something downloads instead of displaying. More on that below.

Who sets this type in the first place

Worth knowing where the header comes from — then you know where to fix it.

For files on disk, the web server does it automatically. It keeps a lookup table of "extension → type": sees .css, writes text/css. That's exactly why you don't think about this 95% of the time. But the table has an edge: if the extension is rare or new and the server config is old, there's no match — and the file ships as "unknown stuff," i.e. application/octet-stream. That's how fonts and manifest files break on ageing configs.

For files you put into cloud storage, you set the type yourself at upload time. Don't set it, and storage will dutifully mark it "unknown stuff" — after which your image downloads instead of displaying. This is the most common reason user-uploaded files behave oddly.

For responses you build in code, the type is yours too. Most frameworks handle it when you return JSON, but if you're assembling a response by hand, the header is your job.

Why does a file download instead of opening?

Almost always it's application/octet-stream — the universal "just some bytes, I don't know what this is." The browser can't display anything, so it offers to save. Files served from storage without an explicit type often behave this way.

The fix is on the sending side: set the real type when uploading the file to storage. The reverse is useful too — if you deliberately want a file downloaded rather than displayed, there's a separate header for that: Content-Disposition: attachment.

The related symptom where an image doesn't show at all is covered in why your images aren't loading.

How do I check a file's MIME type?

Open developer tools, the Network tab, click the request — the response headers show Content-Type. That's the single source of truth: not what the file is named, but what the server declared it to be.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →