Service worker isn’t working? Here’s a small detail you might’ve missed. (create-react-app)

Andre Tai
2 min readSep 4, 2020

--

I was learning how to build PWAs by following a couple of tutorials on Youtube but I was hit with a road block at the very last step. I couldn’t get my service worker to run even when I’ve done everything correctly (the issues ranged from service worker being redundant to it being deleted as soon as the page loads).

You might want to try the index.js file in the src folder. At the very last line (if you haven’t modified anything), you will see that serviceWorker is unregistered by default. Simply change the unregister() method to register() so that the application will not delete the service worker when it loads.

Change the serviceWorker.unregister() method to register() method in src/index.js.

I made the mistake of assuming everything is done in the public folder when building a PWA (like the Youtube tutorials suggest) when in fact, ‘create-react-app’ already has a preset for that. So if you’ve decide to not build the service worker from scratch and instead use the one in the src folder, this may be a non-issue.

It’s really simple and to be honest dumb on my part. However, I couldn’t find any answer similar to this and I feel like someone like myself will be happy to know this. Haha!

--

--

Andre Tai

I build and write stuff. Find me on Twitter and Instagram @andretaiwx