Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Service worker registration fails because the script comes back as HTML

ErrorService Worker registration error: Unsupported MIME type ('text/html')

Short answer

The browser fetched your service worker URL and got a response that is not a JavaScript file. In a single-page app the usual reason is that the server answered the missing file with index.html. Request the script URL with curl, look at the Content-Type, then make the server return the real file with a JavaScript type.

  • Applies to Chrome 130, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the exact URL you pass to register() and read the status line and the Content-Type header. The two lines tell you which cause below applies.

    curl -sI https://example.com/service-worker.js | grep -i -E '^(HTTP|content-type)'
  2. 2Match what you see, then jump to its fix

    1. 1Content-Type: text/html on the script URL, and the body starts with <!doctype html>The file does not exist at that URL, and the SPA fallback sends index.htmlFix
    2. 2HTTP 200 on the script URL, but Content-Type is text/plain, application/octet-stream or another non-JavaScript typeThe file exists but the server labels it with a non-JavaScript typeFix

What does the error look like?

  • Service Worker registration error: Unsupported MIME type ('text/html')
  • The script has an unsupported MIME type (text/html)

Cause 1 of 2

The file does not exist at that URL, and the SPA fallback sends index.html

1Check

A single-page app server usually answers every unknown path with index.html. If the worker file was never copied into the folder you serve (for example it is not in the public or build folder), the browser receives that HTML page instead of a script. Look at the first bytes of the response.

curl -s https://example.com/service-worker.js | head -c 100
ls build/ | grep -i service

2Fix

Put the worker file in the folder that is served as static files, so it exists at the URL you register. Then make sure the static file middleware runs before the catch-all route that returns index.html, so real files win over the fallback.

# Order of rules on the server (any framework or proxy):
# 1. serve real files from the build folder
# 2. only if no file matched, return index.html

3Verify

The script URL now returns a JavaScript type, and registration succeeds.

curl -sI https://example.com/service-worker.js | grep -i content-type
# then reload the page and open DevTools > Application > Service Workers

Confirmed by w3c.github.io

Did this fix it?

Cause 2 of 2

The file exists but the server labels it with a non-JavaScript type

1Check

Registration needs the worker script to be served with a valid JavaScript media type. A proxy rule, a custom header or a static host setting that forces one type for every file breaks it.

curl -sI https://example.com/service-worker.js | grep -i content-type

2Fix

Remove the rule that overrides the type for .js files, or set the type for that file to a JavaScript media type such as text/javascript. How you do it depends on your server or host.

# In your server or proxy config, make the response for this one file carry:
Content-Type: text/javascript

3Verify

Hard-reload the page, unregister the old worker in DevTools, and register again.

curl -sI https://example.com/service-worker.js | grep -i content-type

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Register the worker with the same absolute path you can open in the address bar, and open it there once after every deploy.
  • Add a request to the worker URL to your deploy check and fail the deploy if the Content-Type is not JavaScript.
  • Keep the SPA fallback as the last route so it never answers for files that should exist.

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.