TL;DR
- To start building a Progressive Web App (PWA), you need four essential files:
index.html
, a logo (preferably in SVG format),manifest.json
, andserviceworker.js
.index.html
links tomanifest.json
and registers the service worker.- Use pwa-asset-generator to automatically create icons, splash screens, and update metadata in
index.html
andmanifest.json
.- Configure the
service-worker.js
file to enable caching with Workbox.- Host your PWA with
npx serve
, then test it using Google DevTools for service worker and Lighthouse audits.
Requirements
You can start building a PWA with just four essential files: index.html
, a logo file (preferably in SVG format), manifest.json
, and serviceworker.js
.
Generating Icons and Splash Screen
The first step is to generate multiple-sized icons and a splash screen using pwa-asset-generator. It automatically creates icon and splash screen images, favicons, and mstile images. Pwa-asset-generator offers a CLI tool to convert our input logo file into a bunch of icons. The usage is as follows:
npx pwa-asset-generator [source-file] [output-folder]
You can also add flags to the command. For example, use -i
to automatically add splash screen and icon meta tags to the index file, or use -m
to update the manifest file with the generated icons automatically.
npx pwa-asset-generator logo.svg -i ./index.html -m ./manifest.json
After running the CLI command with -i
and -m
, the index.html
and manifest.json
will be updated and filled with icon and splash screen metadata.
Configure ServiceWorker
The next step is to edit our service-worker.js
file to enable caching features for our app. We are going to import Workbox from a CDN and apply caching to the images in our app. This means that when our PWA loads an image, it will first check the cache. If the image is not in the cache, it will fetch it from the network.
Serve
Finally, you can use npx serve
to host your PWA web app on localhost:3000
. And now, you can open Google DevTools to test if you have managed the manifest file and started the service worker successfully.
Also, you can open the Lighthouse tool to test if your PWA audits have passed.
References