25 Jan 2023

Building a progressive web application (PWA) using React

Progressive web applications (PWAs) are web applications that can be installed on a user's device and provide an experience similar to a native mobile application. PWAs are designed to be fast, reliable, and accessible on any device or network. In this blog post, we will explore how to build a PWA using React, a popular JavaScript library for building user interfaces.

First, let's start by creating a new React project. We will use the create-react-app command-line tool to set up a new project with a basic file structure and some default configurations. Open a terminal and run the following command:

npx create-react-app my-pwa

This will create a new directory called "my-pwa" with the basic file structure for a React application. Inside the "src" folder, you will find the "index.js" file, which is the entry point for your application.

Next, we will add some configuration to make our application a PWA. First, we need to add a web manifest file, which is a JSON file that contains information about the application such as its name, icons, and start URL. Create a new file called "manifest.json" in the "public" folder and add the following code:

{
  "short_name": "My PWA",
  "name": "My Progressive Web App",
  "icons": [
    {
      "src": "icon.png",
      "sizes": "192x192",
      "type": "image/png"
    }
  ],
  "start_url": "./index.html",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"
}

Make sure to change the "short_name", "name", and "icons" fields to match your application.

Next, we need to link to the manifest file in the "index.html" file. Open the "index.html" file in the "public" folder and add the following line in the head section:

<link rel="manifest" href="./manifest.json">

Now that we have added the manifest file, we need to add a service worker to enable offline capabilities and improve the performance of our application. A service worker is a script that runs in the background and can cache assets and handle network requests. To add a service worker, we will use the "workbox-webpack-plugin" library.

First, install the library by running the following command in the terminal:

npm install --save-dev workbox-webpack-plugin

Next, open the "webpack.config.js" file in the "node_modules/react-scripts" folder and add the following code at the top of the file:

const WorkboxWebpackPlugin = require('workbox-webpack-plugin');

Then, add the following code in the "plugins" array:

new WorkboxWebpackPlugin.GenerateSW({
  clientsClaim: true,
  skipWaiting: true
})

This will generate a service worker file and configure it to take control of the clients as soon as it's installed and start controlling them immediately.

Finally, we need to register the service worker in the "index.js" file. Add the following code at the bottom of the file, before the closing "ReactDOM.render" function:

if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('./service-worker.js')
      .then((registration) => {
        console.log('Service worker registered: ', registration);
      })
      .catch((error) => {
        console.log('Service worker registration failed: ', error);
      });
  });
}

This code checks if the browser supports service workers, and if it does, it registers the "service-worker.js" file. This file is generated by the "workbox-webpack-plugin" and contains the service worker code.

With these steps, we have created a basic PWA using React. The application can now be installed on a user's device, and will work offline and provide a fast and reliable experience.

However, this is just a basic PWA, you can continue to improve the performance and usability of your application by adding additional features such as push notifications, background sync, and more.

It's worth noting that PWAs are still evolving technology and it's possible that some of the steps or tools I mentioned might become deprecated or change over time, but this should give you a good starting point for building your own PWAs using React.