12
votes

I am working on a React progressive web app built from create-react-app, and am attempting to register a service worker.

However, after following the documentation on create-react-app which merely says to change serviceWorker.unregister() to serviceWorker.register(), doesn't register anything.

Even from a fresh create-react-app project this still doesn't work.

The documentation seems very lean for this topic, is there anything else I need to do to register the default service worker generated by create-react-app?

4
Did you read/follow all in here: facebook.github.io/create-react-app/docs/… - Akrion
@adam show your registerServiceWorker.js - Akshay Mulgavkar
@akrion Yes I followed the steps on that page, its just changing unregister() to register() unless I'm missing something? - Adam
@AkshayMulgavkar my register service work file is the exact same that CRA spits out. I have not touched it at all - Adam
I faced the same problem on newly created-react-app. But not sure what happened, I closed and reopened the chrome instance and serviceWorker got registered - rehman_00001

4 Answers

8
votes

step 1 :

npm i workbox-cli

step 2 : create workbox-config.js file in your project folder

step 3 : add

module.exports = {
  globDirectory: './public/',
  globPatterns: ['\*\*/\*.{html,js}'],
  swDest: './public/sw.js',
  clientsClaim: true,
  skipWaiting: true
};

step 4: update your serviceWorker.js file with

const swUrl = `${process.env.PUBLIC_URL}/sw.js`

step 5: update your package.json file

"scripts": {
  // ...    
  "build": " workbox generateSW && react-scripts build",
  // ...
}

Then build and serve.

read more https://medium.com/@arafatahmedtanimcsedu57/progressive-web-apps-with-create-react-app-ca0c955ab798

4
votes

If you bootstrapped your app using create-react-app, then go index.js and change the line serviceWorker.unregister() to serviceWorker.register()

Then build the app using npm run build and run the app from build directory using an HTTP server like serve or http-server.

Open DevTools and got to Application tab, you'll see your service worker running.

1
votes

If you are not seeing your service worker active in the applications tab of dev tools then you can try the following:

  1. Ensure you are viewing the application on either a "localhost" or an HTTPS url, anything else will not work.
  2. Try running the build in production mode (instead of dev) by running this in your console:

npm run prod

Then if you want to run a dev server you can follow that with:

serve -s build

and access your website on the URL specified in the response (likely localhost:5000).

0
votes

I had the same problem while I was tinkering with some service workers simultaneously, and even though I created a new create-react-app, changed unregister to register, it still didn't work. It was first when I restarted chrome, built my app, and served it with serve that it actually started working.

I also removed the if production condition to enable it everywhere.