|
| 1 | +project_path: /web/_project.yaml |
| 2 | +book_path: /web/ilt/pwa/_book.yaml |
| 3 | + |
| 4 | +{# wf_auto_generated #} |
| 5 | +{# wf_updated_on: 2018-09-05 #} |
| 6 | +{# wf_published_on: 2016-01-01 #} |
| 7 | + |
| 8 | + |
| 9 | +# Challenge: Convert a news app to a PWA {: .page-title } |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +<div id="overview"></div> |
| 15 | + |
| 16 | + |
| 17 | +## Overview |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +In this challenge, you'll use the skills you've learned during the training to build a complete Progressive Web App (PWA). |
| 23 | + |
| 24 | +#### What you will do |
| 25 | + |
| 26 | +* Test PWA development with Lighthouse |
| 27 | +* Write a service worker using Workbox and `workbox-build` |
| 28 | +* Precache static assets for instant loading |
| 29 | +* Dynamically cache 3rd-party API data for an offline fallback |
| 30 | +* Enable Add to homescreen functionality |
| 31 | + |
| 32 | +#### What you should know |
| 33 | + |
| 34 | +* Intermediate JavaScript, including familiarity with ES2015 [Promises](/web/fundamentals/primers/promises) |
| 35 | +* Familiarity with service workers and Workbox (see [Lab: Workbox](/web/ilt/pwa/lab-workbox)) |
| 36 | +* Some familiarity with [Gulp](https://gulpjs.com/) and [Node.js](https://nodejs.org/en/) is recommended (see: [Lab: Gulp Setup](/web/ilt/pwa/lab-gulp-setup)) |
| 37 | + |
| 38 | +#### What you will need |
| 39 | + |
| 40 | +* Computer with terminal/shell access |
| 41 | +* Connection to the internet |
| 42 | +* A [browser that supports service workers](https://jakearchibald.github.io/isserviceworkerready/) |
| 43 | +* Node.js installed on your computer |
| 44 | +* A text editor |
| 45 | + |
| 46 | +The app you'll be building is PWA News Reader, a simple client-side app that displays [Hacker News](https://news.ycombinator.com/) questions. |
| 47 | + |
| 48 | +Unlike the other labs, this is a self-guided exercise - only the setup is explained, the rest is up to you! |
| 49 | + |
| 50 | +<div id="get-set-up"></div> |
| 51 | + |
| 52 | + |
| 53 | +## 1. Get set up |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +If you have not downloaded the [repository](https://github.com/google-developer-training/pwa-training-labs) and installed [the LTS version of Node.js](https://nodejs.org/en/), follow the instructions in [Setting up the labs](setting_up_the_labs.md). |
| 59 | + |
| 60 | +### Install project dependencies and explore the app |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +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. |
| 65 | + |
| 66 | +The `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: |
| 67 | + |
| 68 | + cd news-reader/project |
| 69 | + npm install |
| 70 | + |
| 71 | +After the installation is complete, run the following command to build and serve the app: |
| 72 | + |
| 73 | + npm run serve |
| 74 | + |
| 75 | +The `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: |
| 76 | + |
| 77 | +* The app's images and its root files (`index.html` and `sw.js`) are copied directly to a `dist/` folder |
| 78 | +* The CSS files in `styles/` and the JS files in `scripts/` are processed (minified, compiled for browser support, etc.) and inlined directly into `index.html` |
| 79 | +* A development server is started in the `dist/` directory |
| 80 | +* The `watch` method is configured to rebuild the app and reload the browser as files are updated |
| 81 | + |
| 82 | +You can terminate the development server and `watch` process at any time by running `ctrl + c` from the command line. |
| 83 | + |
| 84 | +Open your browser and navigate to `localhost:8081` to examine the app. |
| 85 | + |
| 86 | +Note: [Unregister](tools-for-pwa-developers#unregister) any service workers and [clear all service worker caches](tools-for-pwa-developers#clearcache) for localhost so that they do not interfere with the lab. |
| 87 | + |
| 88 | +Solution code can be found in the `solution/` folder. |
| 89 | + |
| 90 | +<div id="specification"></div> |
| 91 | + |
| 92 | + |
| 93 | +## 2. Specification |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +Now you're ready to start. Here is your challenge: |
| 99 | + |
| 100 | +### 2.1 Service worker |
| 101 | + |
| 102 | +#### Features |
| 103 | + |
| 104 | +Use the Service Worker API to achieve the following: |
| 105 | + |
| 106 | +* Precache the app shell files (`index.html` and all images in the `images/` directory) |
| 107 | +* Dynamically cache calls to the Hacker News API (`https://hacker-news.firebaseio.com/v0/*`) and serve them with a network-first strategy |
| 108 | +* Dynamically cache calls to the polyfill CDN (`https://cdn.polyfill.io/*`) with a stale-while-revalidate strategy |
| 109 | +* Configure the service worker to call [`skipWaiting`](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting) and [`Clients.claim`](https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim) so that updated service workers will immediately activate and claim all clients |
| 110 | + |
| 111 | +#### Constraints |
| 112 | + |
| 113 | +* The service worker should only be registered if the browser supports service workers, and should register after the page has loaded. |
| 114 | +* Use [workbox-build](/web/tools/workbox/guides/precache-files/workbox-build#using_with_gulp) to inject the precache manifest automatically as part of the gulp build process. Write a gulp task as described in the [documentation](/web/tools/workbox/guides/precache-files/workbox-build#using_with_gulp) and add the task to those called in the `dist` task. The source service worker is `src/sw.js` and the destination service worker is `dist/sw.js`. |
| 115 | +* Use [Workbox routing](/web/tools/workbox/modules/workbox-routing) and [strategies](/web/tools/workbox/reference-docs/latest/workbox.strategies) to implement all of the dynamic caching. |
| 116 | + |
| 117 | +#### Optional challenges |
| 118 | + |
| 119 | +* 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. |
| 120 | +* Configure the Hacker News API route with a network timeout of 5 seconds. |
| 121 | +* Name the polyfill cache "polyfills". |
| 122 | + |
| 123 | +#### Hints |
| 124 | + |
| 125 | +* See the [Workbox precaching guide](/web/tools/workbox/guides/precache-files/workbox-build) for documentation on precaching files and injecting a precache manifest with gulp. |
| 126 | +* Check out the [Workbox routing guide](/web/tools/workbox/guides/route-requests#handling_a_route_with_a_workbox_strategy) and [common recipes](/web/tools/workbox/guides/common-recipes) for documentation on runtime caching strategies and routes. |
| 127 | +* See the [workbox module documentation](/web/tools/workbox/modules/workbox-sw#skip_waiting_and_clients_claim) for configuring `skipWaiting` and `Clients.claim`. |
| 128 | + |
| 129 | +#### Testing |
| 130 | + |
| 131 | +If implemented correctly: |
| 132 | + |
| 133 | +* You should see the service worker [registered in developer tools](tools-for-pwa-developers#accesssw) |
| 134 | +* The home page should load offline after the first visit and you should see `index.html` and `images/` [in the cache](tools-for-pwa-developers#cache) |
| 135 | +* Previously fetched data from the Hacker News API should be available on subsequent loads and should be visible [in the cache](tools-for-pwa-developers#cache) |
| 136 | +* Updated service workers should immediately activate on each new page load |
| 137 | + |
| 138 | +You can also run an [audit with Lighthouse](/web/tools/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). |
| 139 | + |
| 140 | +See the solution code in the `solution/` directory if you get stuck. |
| 141 | + |
| 142 | +### 2.2 Optional: Add to Homescreen |
| 143 | + |
| 144 | +Note: This task is optional, and [browser support for the manifest file](https://caniuse.com/#feat=web-app-manifest) and [`beforeinstallprompt`](https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent) is not as broad as that for service workers. |
| 145 | + |
| 146 | +#### Features |
| 147 | + |
| 148 | +* Add a manifest file to enable Add to Homescreen functionality |
| 149 | +* The manifest should be configured to open the `index.html` page in `standalone` mode |
| 150 | +* Theme and background colors are `#607d8b`, the name is "PWA News Reader" and the short name is "News Reader" |
| 151 | +* `meta` tags should be supplied for browsers that don't support the `manifest.json` file |
| 152 | +* Bonus: Ensure that the Add to Home Screen prompt is activated from within a user gesture, which is [required](/web/updates/2018/06/a2hs-updates) for Chrome 68+ |
| 153 | + |
| 154 | +#### Hints |
| 155 | + |
| 156 | +* Note that the touch icons are already available in `images/touch/`. |
| 157 | +* Use a manifest generator like [this one](https://app-manifest.firebaseapp.com/) or [this one](https://tomitm.github.io/appmanifest/), which also generates `<meta>` tags for browsers that don't support the manifest file. |
| 158 | + |
| 159 | +#### Testing |
| 160 | + |
| 161 | +* In Chrome DevTools, the manifest properties can be examined under __Application__ > __Manifest__, where you can also [test Add to Homescreen](/web/fundamentals/app-install-banners/#test) |
| 162 | +* Using Chrome, the [Lighthouse](/web/tools/lighthouse/) PWA audit will also confirm that the manifest is configured correctly |
| 163 | + |
| 164 | +<div id="congratulations"></div> |
| 165 | + |
| 166 | + |
| 167 | +## Congratulations! |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +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! |
| 173 | + |
| 174 | + |
0 commit comments