Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serviceWorker missing from ReactApp

Hi All I realized just recently when CRA(npx create-react-app myapp) , serviceWorker is missing from src folder and instead I noticed reportWebVitals().Is there some update from React that got ride of serviceWorker and replaced it with reportWebVitals ? Or there it is something I am not doing correctly? pls lmk. thank you

like image 488
halleK7 Avatar asked Nov 15 '20 21:11

halleK7


People also ask

Where is serviceWorker in React?

A service worker is generated by default when we use the create-react-app to create a new React project. We will find the serviceWorker. js file in the src folder of the project.

What is serviceWorker JS in React?

A service worker is a script that the client (your browser) runs in the background separate from the web page. Operating in the background separate from the web page enables you to use features that don't actually require a web page or user interaction to trigger — like a push notification or background sync.

What is a service worker in react?

In React, service workers are automatically added when you create your application through the create-react-app command, through SWPrecacheWebpackPlugin. Service workers ensure that the network is not a bottleneck to serve new requests.

Did create-react-app change the way service workers manage service workers?

But definitely seems like create-react-app has changed the way that manage service worker. I found in the create-react-app GitHub this a workaround: Show activity on this post. Source. The production build has all the tools necessary to generate a first-class Progressive Web App, but the offline/cache-first behavior is opt-in only.

Why is my CRA file missing from the service worker?

The file is missing. This causes a problem with apps which are upgrading from prior CRA versions, as they continue to reference the cached service worker (so the web app continues to cache old assets). Just run the commands above.

How do I register the service worker in the application?

At this stage, your service worker is not registered, so you will have to first register it before utilizing it in your application. To register the service worker, navigate to the src/index.js file, and look for the following line: Change it to the following line.


Video Answer


3 Answers

Yes, this is an update after CRAv4 and React 17. Create-React-App’s (CRA) boilerplate now comes set up to record those measurements out of the box. If you want to see the values right now, checkout your index.js and pass console.log into the reportWebVitals function.

enter image description here

In an update, they switched from Service worker to the Workbox InjectManifest plugin and moved the PWA templates into their own repository. For more, you can refer to this release doc https://github.com/facebook/create-react-app/releases/tag/v4.0.0

like image 109
VIshal Jain Avatar answered Oct 29 '22 03:10

VIshal Jain


I have the same problem here! I don’t know if I did something wrong. But definitely seems like create-react-app has changed the way that manage service worker.

I found in the create-react-app GitHub this a workaround:

https://github.com/facebook/create-react-app/issues/10032

like image 34
Adrián Guillen Avatar answered Oct 29 '22 05:10

Adrián Guillen


A bit late, but if you want to get the service worker opt-in, try

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

With typescript,

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

Source.

The production build has all the tools necessary to generate a first-class Progressive Web App, but the offline/cache-first behavior is opt-in only.

Starting with Create React App 4, you can add a src/service-worker.js file to your project to use the built-in support for Workbox's InjectManifest plugin, which will compile your service worker and inject into it a list of URLs to precache.

If you start a new project using one of the PWA custom templates, you'll get a src/service-worker.js file that serves as a good starting point for an offline-first service worker.

like image 37
Nithin Sai Avatar answered Oct 29 '22 05:10

Nithin Sai