In this challenge, you'll use the skills you've learned during the training to build a complete Progressive Web App (PWA).
What you will do
- Test PWA development with Lighthouse
- Write a service worker using Workbox and
- Precache static assets for instant loading
- Dynamically cache 3rd-party API data for an offline fallback
- Enable Add to homescreen functionality
What you should know
- Familiarity with service workers and Workbox (see Lab: Workbox)
- Some familiarity with Gulp and Node.js is recommended (see: Lab: Gulp Setup)
What you will need
- Computer with terminal/shell access
- Connection to the internet
- A browser that supports service workers
- Node.js installed on your computer
- A text editor
The app you'll be building is PWA News Reader, a simple client-side app that displays Hacker News questions.
Unlike the other labs, this is a self-guided exercise - only the setup is explained, the rest is up to you!
1. Get set up
Install project dependencies and explore the app
After you have downloaded the repo, open the
news-reader/project/ folder in your preferred text editor. The
project/ folder is where you will be working.
Take a moment to look through the code and get familiar with the app's structure. The
src/ folder contains the application's source files.
package.json file specifies the app's development dependencies. You don't need to be familiar with these specific packages, but they're required to build the app. In the command line, navigate to the
project/ directory and install these packages:
cd news-reader/project npm install
After the installation is complete, run the following command to build and serve the app:
npm run serve
serve command is aliased in
package.json to run the gulp
dev task. Open
gulpfile.js and explore the contents. You don't need to understand all the tasks. The overall build flow (defined by the
dev task) is as follows:
- The app's images and its root files (
sw.js) are copied directly to a
- The CSS files in
styles/and the JS files in
scripts/are processed (minified, compiled for browser support, etc.) and inlined directly into
- A development server is started in the
watchmethod is configured to rebuild the app and reload the browser as files are updated
You can terminate the development server and
watch process at any time by running
ctrl + c from the command line.
Open your browser and navigate to
localhost:8081 to examine the app.
Solution code can be found in the
Now you're ready to start. Here is your challenge:
2.1 Service worker
Use the Service Worker API to achieve the following:
- Precache the app shell files (
index.htmland all images in the
- Dynamically cache calls to the Hacker News API (
https://hacker-news.firebaseio.com/v0/*) and serve them with a network-first strategy
- Dynamically cache calls to the polyfill CDN (
https://cdn.polyfill.io/*) with a stale-while-revalidate strategy
- Configure the service worker to call
Clients.claimso that updated service workers will immediately activate and claim all clients
- The service worker should only be registered if the browser supports service workers, and should register after the page has loaded.
- Use workbox-build to inject the precache manifest automatically as part of the gulp build process. Write a gulp task as described in the documentation and add the task to those called in the
disttask. The source service worker is
src/sw.jsand the destination service worker is
- Use Workbox routing and strategies to implement all of the dynamic caching.
- Name the Hacker News API cache "stories" and configure it to accept a maximum of 300 entries. Set a cache lifetime for the entries to 5 minutes.
- Configure the Hacker News API route with a network timeout of 5 seconds.
- Name the polyfill cache "polyfills".
- See the Workbox precaching guide for documentation on precaching files and injecting a precache manifest with gulp.
- Check out the Workbox routing guide and common recipes for documentation on runtime caching strategies and routes.
- See the workbox module documentation for configuring
If implemented correctly:
- You should see the service worker registered in developer tools
- The home page should load offline after the first visit and you should see
images/in the cache
- Previously fetched data from the Hacker News API should be available on subsequent loads and should be visible in the cache
- Updated service workers should immediately activate on each new page load
You can also run an audit with Lighthouse using Chrome. The final app should pass all PWA tests except those concerned with HTTPS (the Add to Homescreen tests are completed in the next section).
See the solution code in the
solution/ directory if you get stuck.
2.2 Optional: Add to Homescreen
- Add a manifest file to enable Add to Homescreen functionality
- The manifest should be configured to open the
- Theme and background colors are
#607d8b, the name is "PWA News Reader" and the short name is "News Reader"
metatags should be supplied for browsers that don't support the
- Bonus: Ensure that the Add to Home Screen prompt is activated from within a user gesture, which is required for Chrome 68+
- Note that the touch icons are already available in
- Use a manifest generator like this one or this one, which also generates
<meta>tags for browsers that don't support the manifest file.
- In Chrome DevTools, the manifest properties can be examined under Application > Manifest, where you can also test Add to Homescreen
- Using Chrome, the Lighthouse PWA audit will also confirm that the manifest is configured correctly
You have made the news reader app into a PWA by ensuring that content is available offline and enabling users to add the app to their device's home screen!