9.2 Creating a PWA with React
# Create React App
If you are using Create React App 4 (released October 2020), creating a PWA with React just go a little easier. One of the biggest changes is that you can use the Workbox InjectManifest plugin for Webpack to automatically inject your Webpack managed assets into the service-worker's precache.
It also provides a starter service-worker.js
module and the registration functions.
To get all of this well thought out, automated goodness, you need to use the --template
flag with CRA.
npx create-react-app my-app --template cra-template-pwa
# Enable the service worker
And then in the project's main index.js
module, look for this comment block ...
// 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: https://cra.link/PWA
serviceWorkerRegistration.unregister()
Notice that the service worker is not enabled by default. Update to opt-in to using the service worker.
serviceWorkerRegistration.register()
# Update the PWA Manifest
A default progressive web app manifest is provided at public/manifest.json
. You will need to customize it with your correct app meta data: name, icons, theme colour, etc. See Google's Web App Manifest Guide for details on all of the options.
{
"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"
}
CRA Official Docs
# Practice Exercises
# Basic setup
- Create a new React project using
create-react-app
and the PWA template. - Update the manifest file to change the
Name
to be your name and theShort name
to be your Algonquin user id. - Enable to service worker
- Create a production build of the project with
yarn build
cd
into thebuild
folder and run a node web server:npx serve
- Open the app in a new Incognito browser window.
- Open the
Network
tab in Chrome DevTools. Disable the cache and simulateoffline
status. Now try to reload the browser - it should still work. - Run the lighthouse report in Chrome DevTools. Inspect the report.
- Open the
Application
tab in Chrome DevTools. Review theManifest
andService Worker
information. - Take a screenshot of the
Manifest
details in DevTools showing theIdentity
section and post that to our class Slack channel.
# Add routing
- Add
react-router
to your project. - Create a simple navigation with three pages.
- Add a CSS file for each page to apply some unique styling for each of the three pages.
- Build and test your application in offline mode.
# Customize the service worker
- Add an image file (jpeg or png) to each of the three pages. Store the images in the
public/img
folder. - Build and test your application in offline mode. Do you see the images?
- Modify
service-worker.js
to add Workbox modules to cache the image assets. - Build and test your application in offline mode. Do you see the images?
# BONUS
- Research how you can use
React.lazy
andReact.Suspense
to trigger code-splitting. - Test to see if it still works in offline mode.