Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create-react-app service worker not functioning

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?

like image 211
Adam Avatar asked May 14 '19 04:05

Adam


People also ask

Why my react app is not running?

If you really need create-react-app , you might need to reinstall Node and reconfigure your dependencies to ensure you have a fresh start with Node, npm, npx, and the like. This is a good resource for updating and reinstalling Node.

Can I work with react offline?

To make our app work offline, we need to get the web assets to work offline. Create React App already has the required boilerplate for making an app offline using service workers. Open src/index. js and change the line that says serviceWorker.


1 Answers

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.

like image 61
Lanil Marasinghe Avatar answered Sep 18 '22 09:09

Lanil Marasinghe