DEV Community

Cover image for How to make your React App a Progressive Web App (PWA)

How to make your React App a Progressive Web App (PWA)

Nabil Alamin
some random dude
Originally published at ・6 min read

Intro 💨

PWAs are great for your apps because;

  • Short loading times and overall better performance in poor network conditions for your users.

  • No need for extra development time to make product a native app(iOS and Android).

  • Have native features like push notifications without the hassle of development.

  • Overall better user engagement

So it's a pretty handy thing to have in any react app.


If your are just starting your project the best thing would be to run the create-react-app command with the pwa template:

npx create-react-app my-app --template cra-template-pwa
Enter fullscreen mode Exit fullscreen mode

It would save you the time. If you have already progressed into your project follow along ⏬⏬⏬

Details ✍

- First we create a service worker

Saving it as worker.js in our public folder: public/worker.js .
A service worker is a script that your browser runs in the background which in this case pre-caches key assets making your PWA to load faster, improving the interaction between your app and users.

var CACHE_NAME = "app name"

var urlsToCache = [

// Install a service worker
self.addEventListener('install', event => {
  // Perform install steps
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);

// Cache and return requests
self.addEventListener('fetch', event => {
      .then(function(response) {
        // Cache hit - return response
        if (response) {
          return response;
        return fetch(event.request);

// Update a service worker
self.addEventListener('activate', event => {
  var cacheWhitelist = ['your-app-name'];
    caches.keys().then(cacheNames => {
      return Promise.all( => {
          if (cacheWhitelist.indexOf(cacheName) === -1) {
            return caches.delete(cacheName);

Enter fullscreen mode Exit fullscreen mode

- Update your HTML file in your public folder: public/index.html

This confirms if the users browser supports service workers. The following script should be added to the body of the index:

        if ('serviceWorker' in navigator) {
          window.addEventListener('load', function() {
            navigator.serviceWorker.register('worker.js').then(function(registration) {
              console.log('Worker registration successful', registration.scope);
            }, function(err) {
              console.log('Worker registration failed', err);
            }).catch(function(err) {
        } else {
          console.log('Service Worker is not supported by browser.');

Enter fullscreen mode Exit fullscreen mode

- Start the Service Worker

Firstly two files need to be added to the root of the project ;

  • service-worker.js :
/* eslint-disable no-restricted-globals */

// This service worker can be customized!
// See
// for the list of available Workbox modules, or add any other
// code you'd like.
// You can also remove this file if you'd prefer not to use a
// service worker, and the Workbox build step will be skipped.

import { clientsClaim } from "workbox-core";
import { ExpirationPlugin } from "workbox-expiration";
import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
import { registerRoute } from "workbox-routing";
import { StaleWhileRevalidate } from "workbox-strategies";


// Precache all of the assets generated by your build process.
// Their URLs are injected into the manifest variable below.
// This variable must be present somewhere in your service worker file,
// even if you decide not to use precaching. See

// Set up App Shell-style routing, so that all navigation requests
// are fulfilled with your index.html shell. Learn more at
const fileExtensionRegexp = new RegExp("/[^/?]+\\.[^/]+$");
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== "navigate") {
      return false;
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith("/_")) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    } // Return true to signal that we want to use the handler.

    return true;
  createHandlerBoundToURL(process.env.PUBLIC_URL + "/index.html")

// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
  // Add in any other file extensions or routing criteria as needed.
  ({ url }) =>
    url.origin === self.location.origin && url.pathname.endsWith(".png"), // Customize this strategy as needed, e.g., by changing to CacheFirst.
  new StaleWhileRevalidate({
    cacheName: "images",
    plugins: [
      // Ensure that once this runtime cache reaches a maximum size the
      // least-recently used images are removed.
      new ExpirationPlugin({ maxEntries: 50 }),

// This allows the web app to trigger skipWaiting via
// registration.waiting.postMessage({type: 'SKIP_WAITING'})
self.addEventListener("message", (event) => {
  if ( && === "SKIP_WAITING") {

Enter fullscreen mode Exit fullscreen mode
  • serviceWorkerRegistration.js :
// This optional code is used to register a service worker.
// register() is not called by default.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read

const isLocalhost = Boolean(
    window.location.hostname === "localhost" ||
      // [::1] is the IPv6 localhost address.
      window.location.hostname === "[::1]" ||
      // are considered localhost for IPv4.

  export function register(config) {
    if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) {
      // The URL constructor is available in all browsers that support SW.
      const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
      if (publicUrl.origin !== window.location.origin) {
        // Our service worker won't work if PUBLIC_URL is on a different origin
        // from what our page is served on. This might happen if a CDN is used to
        // serve assets; see

      window.addEventListener("load", () => {
        const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

        if (isLocalhost) {
          // This is running on localhost. Let's check if a service worker still exists or not.
          checkValidServiceWorker(swUrl, config);

          // Add some additional logging to localhost, pointing developers to the
          // service worker/PWA documentation.
          navigator.serviceWorker.ready.then(() => {
              "This web app is being served cache-first by a service " +
                "worker. To learn more, visit"
        } else {
          // Is not localhost. Just register service worker
          registerValidSW(swUrl, config);

  function registerValidSW(swUrl, config) {
      .then((registration) => {
        registration.onupdatefound = () => {
          const installingWorker = registration.installing;
          if (installingWorker == null) {
          installingWorker.onstatechange = () => {
            if (installingWorker.state === "installed") {
              if (navigator.serviceWorker.controller) {
                // At this point, the updated precached content has been fetched,
                // but the previous service worker will still serve the older
                // content until all client tabs are closed.
                  "New content is available and will be used when all " +
                    "tabs for this page are closed. See"

                // Execute callback
                if (config && config.onUpdate) {
              } else {
                // At this point, everything has been precached.
                // It's the perfect time to display a
                // "Content is cached for offline use." message.
                console.log("Content is cached for offline use.");

                // Execute callback
                if (config && config.onSuccess) {
      .catch((error) => {
        console.error("Error during service worker registration:", error);

  function checkValidServiceWorker(swUrl, config) {
    // Check if the service worker can be found. If it can't reload the page.
    fetch(swUrl, {
      headers: { "Service-Worker": "script" },
      .then((response) => {
        // Ensure service worker exists, and that we really are getting a JS file.
        const contentType = response.headers.get("content-type");
        if (
          response.status === 404 ||
          (contentType != null && contentType.indexOf("javascript") === -1)
        ) {
          // No service worker found. Probably a different app. Reload the page.
          navigator.serviceWorker.ready.then((registration) => {
            registration.unregister().then(() => {
        } else {
          // Service worker found. Proceed as normal.
          registerValidSW(swUrl, config);
      .catch(() => {
          "No internet connection found. App is running in offline mode."

  export function unregister() {
    if ("serviceWorker" in navigator) {
        .then((registration) => {
        .catch((error) => {

Enter fullscreen mode Exit fullscreen mode

- Edit index.js

Go to to your app's index.js file in the src folder: src/index.js . Register the serviceworker as shown below:

import React from 'react';
import ReactDOM from 'react-dom';
// import './index.css';
import './styles/main.scss'
import App from './App';
import * as serviceWorkerRegistration from "./serviceWorkerRegistration";

    <App />

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers:

Enter fullscreen mode Exit fullscreen mode

NB: If your restart the npm your console would log if the worker registration was successful or not

- manifest.json

You should have it in your project's public folder by default but in the case you don't, create it and add the following:

  "short_name": "React App",
  "name": "Create React App Sample",
  "icons": [
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
      "src": "logo192.png",
      "type": "image/png",
      "sizes": "192x192"
      "src": "logo512.png",
      "type": "image/png",
      "sizes": "512x512"
  "start_url": ".",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"

Enter fullscreen mode Exit fullscreen mode

NB: The manifest allows chrome browsers to show the add to homescreen prompt.

Congratulations 👏👏

Reaching this point you have successfully made your React App a PWA, great job 🔥

end giph

Discussion (0)