Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting service worker with create-react-app

lately, I have create two react application and in both of them the service worker is not present instead a new file is generated i.e 'reportWebVitals.js'. Here is my folder structure of src after creating new app. File Structure

Also, if we check on running the app, the browser says that 'No matching service worker detected'

like image 887
Sumit Avatar asked Nov 29 '20 12:11

Sumit


People also ask

Is create React app SSR or CSR?

Remember, you can support SSR in your React applications with frameworks like Next. js, while frameworks like Create React App enable you to support CSR.

How do I find out if a service worker is running?

A: From a page on the same origin, go to Developer Tools > Application > Service Workers. You can also use chrome://inspect/#service-workers to find all running service workers.


2 Answers

Thanks @jonrsharpe So, CRA does not give support for service worker directly with default installation command as developer may not be making the application as PWA. So, they decided to keep it optional. Like me, if you anyone else want to install service worker while creating a new app, Prefer:

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

instead of default npx create-react-app my-app. enter image description here Hence, for more yo can refer to https://create-react-app.dev/docs/making-a-progressive-web-app/

like image 66
Sumit Avatar answered Sep 25 '22 10:09

Sumit


If you have already existing project and you want to migrate to the new versions of CRA, you can follow this steps:

1 Create somewhere on your computer a new CRA with the service worker template:

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

2 Copy the service-worker.js from the new created app into your src directory

3 Copy the "workbox-*" dependencies from the package.json into your package.json dependencies

4 (Optional) If you want web-vitals, copy the web-vitals dependency from package.json into your package.json

and don't forget to run again 'yarn install' or 'npm install'

Thats it

like image 23
bukso Avatar answered Sep 24 '22 10:09

bukso