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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With