create-react-app: Error: Service worker has an unsupported MIME type ('text/html')
Describe the bug
Using the create-react-app-typescript-pwa template to generate a progressive web app returns the following error when `serviceWorker.register() is called:
index.js:1 Error during service worker registration: DOMException: Failed to register a ServiceWorker for scope ('http://localhost:3000/') with script ('http://localhost:3000/serviceWorker.js'): The script has an unsupported MIME type ('text/html').
- I’m only able to see the error message after I remove the following condition in the
checkValidServiceWorker()
function in./src/serviceWorkerRegistration.ts
:
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
Did you try recovering your dependencies?
Yes
Please paste the output of npm --version
and/or yarn --version
to confirm.
–>
7.6.1
Which terms did you search for in User Guide?
PWA, TypeScript, Workbox, Service worker
Environment
current version of create-react-app: 4.0.3 System: OS: Windows 10 10.0.17763
Steps to reproduce
- Run
npx create-react-app my-app --template cra-template-pwa-typescript
- modify
serviceWorker.unregister()
toserviceWorker.register()
Expected behavior
- Service worker should be registered
Actual behavior
Service worker not registered
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 18
- Comments: 15 (1 by maintainers)
Given this issue was encountered on Windows and seems to match the symptoms we experienced on Windows I’m wondering if this PR would be relevant: https://github.com/facebook/create-react-app/pull/11640