DEV Community

Cheryl M
Cheryl M

Posted on • Originally published at cherylm.hashnode.dev on

Convert an Existing React App to a PWA (Typescript)

I wanted to convert my existing react app to a PWA because I wanted to publish it on google play store without having to rewrite it with something like react native. To convert an existing react app to a PWA, we need

  • A service worker
  • A manifest file

Setup the service worker

copy service-worker.ts and serviceWorkerRegistration.ts from https://github.com/cra-template/pwa/tree/main/packages/cra-template-pwa-typescript/template/src to the /src folder of the existing react app.

or create a new react app and copy the files over to /srcnpx create-react-app my-app --template cra-template-pwa-typescript oryarn create react-app my-app --template cra-template-pwa-typescript

If you see this error,

image.png

add, as string to this line,

const publicUrl = new URL(process.env.PUBLIC_URL as string, window.location.href);

Enter fullscreen mode Exit fullscreen mode

in index.tsx, import the service worker and

import * as serviceWorkerRegistration from './serviceWorkerRegistration';

Enter fullscreen mode Exit fullscreen mode

add this after render

serviceWorkerRegistration.register();

Enter fullscreen mode Exit fullscreen mode

The Manifest file

There should be a manifest.json in `/public/ , the template can be found here

Edit the name and icons if required.

Service worker does NOT work in development

To start a production environment locally,

`
yarn global add serve
yarn build
serve -s build

`

Check that the App is a valid PWA

This can be done by using the chrome Lighthouse plugin

Go to the Lighthouse tab in the DevTools, and generate the report

image.png

An install icon shows on chrome url bar for a valid PWA

image.png

image.png

The application tab also shows some useful information

image.png

Learn more

How to make PWAs installable

Learn PWA

Making a Progressive Web App

Top comments (0)