The Ultimate Guide to Converting React Apps to Progressive Web Apps
Tags: pwa, react, tutorial, webdev
Published: 2023-03-11 19:54:35

Do you want to take your React app to the next level and offer your users an app-like experience without the struggle of downloading a native app? Then Progressive Web Apps (PWAs) are the answer! PWAs offer offline functionality, push notifications and more, making them a game-changer in web development. In this article, I'll walk you through the steps on converting your React app to a PWA, allowing you to deliver an immersive, app-like experience to your users.

## Introduction
To get familiar with the terms I will be referring to:

- **Progressive Web Apps (PWAs)** are a new development trend that offers users an app-like experience on their mobile devices without installing a native app. PWAs are essentially websites that run on the mobile browser and can be stored on a device. They create an icon that looks and feels like a mobile application when opened. [See Documentation](https://learn.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/)

- **Service workers** are like special helpers that sit between your browser and the server. They can help your website work better by allowing it to be accessed even if you don't have an internet connection. They can also make your website faster by caching (storing) certain data so that it doesn't have to be reloaded every time you visit the website. [See Documentation](https://developer.chrome.com/docs/workbox/service-worker-overview/)

- **Web App Manifest** is a JSON file that describes your app and provides metadata about it, such as its name, icons, and start URL. The manifest is used by the browser to add your app to the home screen and to provide a more app-like experience. [See Documentation](https://developer.mozilla.org/en-US/docs/Web/Manifest)

Having covered the key terms and concepts, we can now dive straight into the steps required to convert your React app to a Progressive Web App.

## Step 1: Create a new React app
To begin you must create a new React project with cra-template-pwa template, which includes the necessary configurations and files for building a progressive web app. Enter the following command into your terminal.

```
npx create-react-app my-app --template cra-template-pwa
```
## Step 2: Enable service worker functionality
Upon creating a new React app, the service worker files are automatically included. To enable the app to function as a PWA, you need to modify the default service worker registration from 'unregister()' to 'register()' in your index.js file. This change ensures that the service worker is registered as a persistent background process, allowing your app to behave like a PWA even after users close their browser or turn off their device. It should look like this

![Service worker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s104nt94nsy4fo64cw9u.PNG)

## Step 3: Enable your Web app manifest
To customize the appearance of your PWA, you can edit the manifest.json file located in the public directory of your React app. For example, if you want to change the name of your app, you can modify the name property in this file. I will be changing the name of my PWA using this approach.

![manifest.json image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjlldz971zbe2hgxd5po.PNG)

## Step 4: Editing App.js to Customize Your React PWA
You can add any content you want to your App.js file. Here is an example of what my App.js file looks like:

![app.js image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5e9xuddrsmaum9le6opx.PNG)

## Step 5: Build the App
The next step is to build the app. This will create a production-ready version of your app in the build directory. To build the app, run the following command in the terminal:

```
npm run build
//or
yarn build
```

## Step 6: Serve the App
The final step is to serve the production-ready version of your app located in the build directory. This will create a local server and serve your PWA directly from the build folder. First run the following command to install 'serve' package globally.

```
npm install -g serve
//or
yarn global add serve
```
Then run this command to start a server at http://localhost:5000 (or another available port) and serve your PWA from the build directory

```
npx serve -s build
```

![terminal image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ps0g8lrlphjtbiqyxna.PNG)

When you run your app in your browser it should look like this

![react pwa image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wv9zj7u2gzogidqntrl8.PNG)

You can also add a prompt for ios using the 'react-ios-pwa-prompt' library by running the following command:
```
npm i react-ios-pwa-prompt
```
Then, in your index.js file, you should import the PWAPrompt component from the 'react-ios-pwa-prompt' library using the following code:

```
import PWAPrompt from 'react-ios-pwa-prompt';
```
Next, wrap your app using the PWAPrompt component and the React.StrictMode component as shown below:

```
<React.StrictMode>
  <App />
  <PWAPrompt
    promptOnVisit={1}
    timesToShow={3}
    copyClosePrompt="Close"
    permanentlyHideOnDismiss={false}
  />
</React.StrictMode>
```
The promptOnVisit prop determines when the prompt will be displayed, with a value of 1 indicating that it will be displayed on the first visit. The timesToShow prop determines how many times the prompt will be shown. The copyClosePrompt prop sets the text for the close button, and the permanentlyHideOnDismiss prop determines whether the prompt will be permanently hidden if dismissed.

## Conclusion
Congratulations! In this article we walked through the process of converting a React app to a Progressive Web App. We started by understanding the key features and requirements of a PWA, including a service worker and a manifest file and how to implement them. Additionally, we explored advanced PWA features, such as push notifications and home screen icons and title and learned how to incorporate them into a React app. Happy coding!