|
| 1 | +# Progressive Web App |
| 2 | + |
| 3 | +PWA technologies allow us to push web apps even closer to a native-like experience. The most powerful (and challenging to implement) part |
| 4 | +of this group of APIs is a _Service Worker_ |
| 5 | + |
| 6 | +Service workers are a topic in and of themselves, and if you want to learn more about them |
| 7 | + |
| 8 | +- see my PWA course |
| 9 | +- kyle simpson has a workshop too (https://frontendmasters.com/workshops/service-worker-pwa/) |
| 10 | + |
| 11 | +Thankfully, Ember's opinionated ecosystem allows us to share a common package for this |
| 12 | + |
| 13 | +```sh |
| 14 | +ember install \ |
| 15 | + ember-service-worker \ # core addon |
| 16 | + ember-service-worker-asset-cache \ # caching static assets |
| 17 | + ember-service-worker-cache-fallback \ # fallback cache for API calls |
| 18 | + ember-service-worker-index # special handling of index.html |
| 19 | +``` |
| 20 | + |
| 21 | +There's _almost_ no configuration required. The one thing we have to do is inform the fallback cache of the URLs we want it to "save" data for as they pass through. This way if we lose connectivity, we'll fall back to the cached payloads. |
| 22 | + |
| 23 | +Open [`ember-cli-build.js`](../ember-cli-build.js) and make this change. |
| 24 | + |
| 25 | +```diff |
| 26 | + let app = new EmberApp(defaults, { |
| 27 | ++ 'esw-cache-fallback': { |
| 28 | ++ patterns: ['/api/(.+)'] |
| 29 | ++ } |
| 30 | + }); |
| 31 | +``` |
| 32 | + |
| 33 | +Now if you go to the service worker section of your devtools, you should see something like this. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +<hr> |
| 38 | +<p> |
| 39 | + <blockquote> |
| 40 | + <h3> |
| 41 | + 💡 Mike's Tip: Service Worker Devtools |
| 42 | + </h3> |
| 43 | + <a href="https://github.com/mike-north"> |
| 44 | + <img src="https://github.com/mike-north.png" height=64 align="left" style="margin-right: 10px" /> |
| 45 | + </a> |
| 46 | + <p> |
| 47 | + When developing an app that uses a Service Worker, remember to consider your developer tools, or you may be in for a frustrating experience. |
| 48 | + <p><b>You'll nearly always want "Update on reload" checked, so that a new worker is installed on each refresh.</b> Leaving this un-checked will interfere with your browser picking up on changes as you save files. </p> |
| 49 | + </p> |
| 50 | + </blockquote> |
| 51 | +</p> |
| 52 | +<hr> |
| 53 | + |
| 54 | +Now, to test out our ability to go offline, |
| 55 | + |
| 56 | +- Uncheck all boxes |
| 57 | +- Reload |
| 58 | +- Check the "offline" box |
| 59 | + |
| 60 | +The app will load! You can even turn off your wifi and the app will still work! |
| 61 | + |
| 62 | +Note that as soon as you try to see anything you haven't previously seen (i.e., a channel you haven't clicked on), you'll get an error. You're caching API payloads _as they fly by_. Deliberately caching particular URLs is a very easy change to make, but beyond the scope of this course. |
0 commit comments