question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to register a ServiceWorker -- serving CRA through Node / Express

See original GitHub issue

Is this a bug report?

Yes

Can you also reproduce the problem with npm 4.x?

Yes

Which terms did you search for in User Guide?

Error during service worker registration: DOMException: Failed to register a ServiceWorker: The script has an unsupported MIME type (‘text/html’).

register service worker

service worker

Environment

  1. node -v: v8.1.2
  2. npm -v: 4.6.1
  3. yarn --version (if you use Yarn): 1.0.2
  4. npm ls react-scripts (if you haven’t ejected): 1.0.13

Then, specify:

  1. Operating system: MacOS Sierra
  2. Browser and version (if relevant): Google Chrome

Steps to Reproduce

(Write your steps here:)

  1. go here: https://swtest.quantfive.org/
  2. Inspect element
  3. View error

Expected Behavior

Register a service worker

Actual Behavior

The script has an unsupported MIME type ('text/html').

/service-worker.js Failed to load resource: net::ERR_INSECURE_RESPONSE

Error during service worker registration: DOMException: Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html').

screen shot 2017-09-19 at 1 06 41 am

Reproducible Demo

Go here to this website: https://swtest.quantfive.org/

Repo Here: https://github.com/lightninglu10/react-sw-test

I’m serving create-react-app through express. I’m forwarding all requests to Index.html and I’m deploying this via Docker & AWS Elastic Beanstalk. Not sure what the error is but I’d like to have a service worker running.

It’s not a front end issue as I’m running the bare bones CRA. I think it may have something to do with serving through express.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
lightninglu10commented, Sep 19, 2017

Alright, thanks guys, this worked!

for reference, I added

app.get('/service-worker.js', (req, res) => {
    res.sendFile(path.resolve(__dirname, '..', 'build', 'service-worker.js'));
});

In my express server.

0reactions
barbalexcommented, Apr 2, 2018

This issue has plagued me for a while on several apps. When building a spa usually you implement a catch-all route like this (hapijs example):

server.route({
    method: 'GET',
    path: '/{path*}',
    handler: {
      file: 'index.html',
    },
  })

Which means that index.js takes over the service-worker.js route.

So I can imagine that this happens to more people than just me. And the resulting error does not help to find the real reason. Or so it seems to me.

It might help a lot of people to mention the need to explicitely route service-worker.js in the readme, maybe here: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#offline-first-considerations

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error in browser - Failed to register a ServiceWorker ...
The CRA service worker is disabled by default (hence the serviceWorker.unregister() line). By removing the unregister line, ...
Read more >
Using Service Workers with create-react-app - Bits and Pieces
To register the service worker, navigate to the src/index. js file, and look for the following line: serviceWorker.
Read more >
Using Service Workers - Web APIs | MDN
After activation, the service worker will now control pages, but only those that were opened after the register() is successful. In other words, ......
Read more >
PWA with Create React App and service workers
The starter src/service-worker.js file that you added using the cra-template-pwa or ...
Read more >
[Solved]-When I try to register a service worker file in react app ...
Coding example for the question When I try to register a service worker file in react app it will give me a reference...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found