Implementation of service worker using ReactJS Application to Build PWA
Tags: webdev, programming, beginners, career
Published: 2022-01-05 20:27:19

## A BoilerPlate Code Implementation of service worker using ReactJS Application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/js8vnp0zgk1y9mny1iyr.png)
 

Service worker cache the web pages along with other resources, in a separate storage area called CacheStorage.
The service worker life cycle
A service worker has a lifecycle that is completely separate from your web page. To install a service worker for your site, you need to register it.
These are the following lifecycle of a service worker.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep9yr4wotyevvhs3193v.png)

Let’s start with adding a service worker file in your react application.
Step 1. I have created my react application using `create-react-app npm package`.
Step 2. `**Create-react-app**` also gives a service worker file. we are not gonna use that default service-worker file. we will create a new service worker file from scratch.
If you have created your react application using your own configuration then you don’t need to do below step.
If you have create your app using create-react-app then you should delete serviceWorker.js file and remove the following code from index.js.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n72d0pc4bgfavuzeceew.png)
 

Step 3. Create a `sw.js` file in your public folder of root directory and add the following javascript code in index.html file of public folder.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/foq8m306towagsufmo0c.png)
 
In the above code we are checking serviceWorker in navigator object ( Which is used for browser detection ) and we are registering service worker with the scope of root directory then after registration we are checking service worker is ready or not.Through the above step service worker will get activated and start running but still application will not work offline because we haven’t written any code for caching and fetching any static content in sw.js file.
but if you will check application tab of app. you can see something like this.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbzvguwe0ol2vzqi5dp9.jpeg)
 


Step 4. Now open your sw.js file and add the following code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgd0g1ytsp00uo9xxz11.jpeg)
 

`Install` event listener opens the caches object and then populates it with the list of resources that we want to cache. One important thing about the addAll operation is that it’s all or nothing. If one of the files is not present or fails to be fetched, the entire addAll operation fails.
we have added a listener during installation event of service worker and we will put the installation event to waiting state to cache our static data( By using **CACHE_NAME** variable you can name caching object in CacheStorage ). Inside the **cache.addAll** argument we are passing the list of files which we need to cache such as css, html, json and bundled files etc. The successfully return of promise of addAll operation we are calling self.skipWaiting() which is used to change service worker state from waiting to active.
Step 5 ( **Activation Event** ): Now we will check that our service worker will get activated or not so add the following code to sw.js.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kvva9nuwfkorzrkj7rz.jpeg)
 
**Activation Event Listener ** will check activating state of service worker.
( you can take control of uncontrolled clients by calling clients.claim() within your service worker once it’s activated More Explanation with Example.)
Step 6 ( **Fetch Event** ): Once a service worker controls a page, it can intercept every request that the page makes and decide what to do with the request. Every request’s response we can match with our storage cache. If response of the request is already available in cache then we can return cached response otherwise we can cache new request’s responses to our cached storage.
I am using navigator.onLine to check we are connected to internet or not so I am caching the new response.
If we are using our application offline then it will take from cache storage.
( navigator.onLine condition is useful to fetch new static changes of our app).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skr0d12fj9f7wusvxjvt.jpeg)
 
Now your application is ready to work offline.
This was the boilerplate code implementation of service worker in a react application.
There is a lot to learn about PWA. Please refer the below links.
Happy Coding !

Resources: https://developers.google.com/web/fundamentals/primers/service-workers
Give me a clap if you found this article helpful.
Follow me on :
Twitter: https://twitter.com/softfolayanyemi
Linkedin: https://www.linkedin.com/in/folayan-yemi
Github: http://github.com/yemmyfolayan