DEV Community

Cover image for Don't struggle with extending the service worker in Create React App anymore. Upgrade to version 4!
Michał Gacka for 3dayweek.dev

Posted on • Edited on • Originally published at 3dayweek.dev

2 1

Don't struggle with extending the service worker in Create React App anymore. Upgrade to version 4!

No need to struggle with how to customize and extend the service worker without ejecting Create React App anymore. Version 4, released in October 2020, comes with a service worker file available for extension in the src folder.

I wrote a post about the simplest way to extend the default service worker in CRA to help others who were dealing with this task. A strangely easy task requiring a dreadful amount of research and trial and error. But that was before I found out that there is an even simpler way: upgrading to the new CRA version.

The previous post generated some attention so I figured that I'm not the only one, who doesn't realize that one can now access the Service Worker file and edit it directly in the new CRA version.

Simply initialize the project with:
npx create-react-app my-app --template cra-template-pwa

or:
npx create-react-app my-app --template cra-template-pwa-typescript

And the service worker will be available for extension the src folder. Check out the official guide for more information and instructions.

Upgrading is quite straightforward too. I recommend creating the new project on the side, comparing the package.json files to update the old one, copying and merging the files generated in the src folder to the old project, and then fixing compile errors if any occur. It didn't take more than 20 minutes for my relatively large project.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (2)

Collapse
 
m3h0w profile image
Michał Gacka

Like I explained in the article, you need to use the pwa template, not the default CRA.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay