Service worker registration fails because the script comes back as HTML
Service 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.
Fix it fast
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)'2Match what you see, then jump to its fix
- 1
Content-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
HTTP 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
- 1
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 service2Fix
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.html3Verify
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 WorkersConfirmed by w3c.github.io
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-type2Fix
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/javascript3Verify
Hard-reload the page, unregister the old worker in DevTools, and register again.
curl -sI https://example.com/service-worker.js | grep -i content-typeConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix