Giovanni Giampaolo
Giovanni Giampaolo

Reputation: 463

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

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

Upvotes: 3

Views: 3420

Answers (3)

user17783468
user17783468

Reputation: 21

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

Upvotes: 0

James Whiteley
James Whiteley

Reputation: 3474

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.

Upvotes: 8

Giovanni Giampaolo
Giovanni Giampaolo

Reputation: 463

For who have the same problem as suggested by James Whiteley you can use the following command

npx create-react-app my-app --template <app-name>

For more info check this github issue https://github.com/facebook/create-react-app/issues/10104

Upvotes: -1

Related Questions