Why does my create-react-app have no service workers?

Viewed 2418

I used the simplest and the dumbest command

npx create-react-app mia-app
cd mia-app
npm start

But after created the project everytime there are no serviceWorker. Can someone help me?

enter image description here enter image description here

However, I've tried with npm create-create-app with this res.

enter image description here

3 Answers

serviceWorker.js is no longer included in the base template, as of create-react-app version 4.0.something.

Trawling through the create-react-app GitHub issues, two main workarounds keep popping up:

  1. Create an app using the following template:
npx create-react-app my-app --template cra-template-pwa

or for Typescript:

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

(where my-app is replaced by the name of your app)

  1. If you already have an existing project, create a dummy app and copy over the relevant code. Don't forget to copy over the initialisation code from index.js/index.tsx if you do this too.

I had forgotten to change the call to unregister to register in serviceWorkerRegistration.ts. However, even after I did that, I didn't see a service worker show up. After looking at the register function, I realized that the service worker won't run on non-prod build so you'll have to address this issue How can i enable service worker in dev mode in create-react-app? if you want to see it with npm start. This is an unresolved issue as dev mode does not serve up service-worker.js

Related