Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Progressive Web Apps mit Angular 2, Meetup at @thelondonjs

Progressive Web Apps mit Angular 2, Meetup at @thelondonjs

Manfred Steyer

November 11, 2016
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. About me … • Manfred Steyer • SOFTWAREarchitekt.at • Trainer

    & Consultant • Focus: Angular • Google Developer Expert (GDE) Page  2
  2. vs.

  3. Implementing PWAs Modern Browser Technologies •Service Worker •Web App Manifest

    •IndexedDb & Co. Patterns •Caching Patterns •App Shell Pattern •Universal JS
  4. What are Service Worker? • Background Tasks • Are installed

    by an Web App • Can run without Web App • Can decide to go idle or to re-activate themself • HTTPS only (exception: localhost) • No XHR, but fetch
  5. Service Worker and Offline • Can intercept requests • Can

    decide how to respond to requests • Same origin policy (SOP) • Caching Pattern • Cache only • Network only • Try cache first, then network • Try network first, then cache • etc.
  6. Service Worker Toolbox //sw-with-toolbox.js importScripts('/node_modules/sw-toolbox/sw-toolbox.js'); toolbox.precache(CACHE_FILES); toolbox.router.get('/(.*)', toolbox.networkOnly, {origin: 'http://www.angular.at'});

    toolbox.router.default = toolbox.cacheFirst; <script src="node_modules/sw-toolbox/companion.js" data-service-worker="sw-with-toolbox.js"></script> Array can be generated
  7. Fallback for Safari & Co. • AppCache • Less features

    • Not much choice for Caching Patterns • Cache only • Network only
  8. Storing Data while offline • LocalStroage • WebDb (deprecated but

    here) • IndexedDb • Good idea: Use an abstraction, like PouchDB • Challange: Quotas!
  9. Scenarios • Not for: Going offline for a long period

    of time • Use it to bridge lacking or bad data connections
  10. PouchDb const db = new PouchDB("bookingDb"); db.get('bookings').then(entity => { //

    do something with entity }); var entity = { _id: "bookings", bookings: bookings } db.put(entity).then(_ => { console.debug('stored!'); });
  11. App Shell • Shell is cached (Service Worker) • Uses

    cached data (e. g. PouchDb) • Can be displayed immediately • Updates cached view with online-data when loadad App Shell Content
  12. "Install" to Home Screen { "name": "Flight PWA-Demo", "short_name": "Flight-PWA",

    "icons": [{ "src": "images/touch/icon-128x128.png", "sizes": "128x128", "type": "image/png" }, […] ], "start_url": "/index.html?homescreen=1", "display": "standalone", […] }
  13. Referencing the Manifest <!-- Web Application Manifest --> <link rel="manifest"

    href="manifest.json"> <!-- Add to homescreen for Safari on iOS --> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-title" content="Web Starter Kit"> <link rel="apple-touch-icon" href="images/touch/apple-touch-icon.png">
  14. Background Sync • App can request Background Sync • Service

    Worker triggers Sync Event when it is appropriate (Network, Battery …) • Also plans for periodic Background Sync
  15. Push Notifications Browser Push-Service Web API 4. Notification 2. URL

    + Id 3. Notification for Browser(s) 1. Register
  16. Mobile Toolkit • Scaffolding Angular 2 PWA with Angular CLI

    • Helps to get started • Generates Web App Manifest • Generates App Shell • Angular Universal • Service Worker for Caching • AppCache as Fallback for Safari & Co.
  17. Getting Started > npm install -g angular-cli > ng new

    hello-mobile --mobile Infos and Guids: mobile.angular.io
  18. Summary Best of Web and Native Progressive Enhancements No installation

    Easy deployment Cross Plattform No App Store Offline Browser DBs Background Sync Push Angular Mobile Toolkit M&M's solve nearly all problems ;-)