Save 37% off PRO during our Black Friday Sale! »

An Introduction to Service Workers - Code Europe Warsaw

8ec1383b240b5ba15ffb9743fceb3c0e?s=47 Phil Nash
December 07, 2016

An Introduction to Service Workers - Code Europe Warsaw

Service Workers are the biggest thing to hit the browser since XMLHttpRequest. We'll take a look at what the Service Worker can do for your app and more importantly, your users. We'll see the surprisingly small amount of code you need to get started with a Service Worker and finally we'll take a look at Progressive Web Apps and how the Service Worker will take part in a revolution for web applications.

Links:

Offline apps course on Udacity: https://www.udacity.com/course/offline-web-applications--ud899

The demo SMS app: https://github.com/philnash/sms-messages-app

HTML5Rocks intro to service worker: http://www.html5rocks.com/en/tutorials/service-worker/introduction/
Service Worker spec: https://github.com/w3c/ServiceWorker
Is Service Worker Ready Yet? https://jakearchibald.github.io/isserviceworkerready/
And resources: https://jakearchibald.github.io/isserviceworkerready/resources.html
Service Worker Cookbook: http://www.serviceworke.rs/
Addy Osmani - Offline Storage for Progressive Web Apps - https://bit.ly/cache-vs-db

Twilio Blog: Web powered SMS inbox with push notifications: https://www.twilio.com/blog/2016/02/web-powered-sms-inbox-with-service-worker-push-notifications.html
Background sync: https://developers.google.com/web/updates/2015/12/background-sync?hl=en

The Service Worker trailer: https://www.youtube.com/watch?v=QiXLaisCq10

8ec1383b240b5ba15ffb9743fceb3c0e?s=128

Phil Nash

December 07, 2016
Tweet

Transcript

  1. AN INTRODUCTION TO SERVICE WORKERS

  2. Phil Nash @philnash http:/ /philna.sh philnash@twilio.com

  3. None
  4. AN INTRODUCTION TO SERVICE WORKERS

  5. SERVICE WORKERS ARE SCRIPTS THAT INTERCEPT NETWORK REQUESTS

  6. SO WEB DEVELOPERS CAN TREAT THE NETWORK AS AN ENHANCEMENT

  7. AND PROVIDE OFFLINE EXPERIENCES FOR USERS OF WEB APPLICATIONS

  8. None
  9. A SLIGHTLY LONGER INTRODUCTION TO SERVICE WORKERS

  10. XMLHttpRequest

  11. IN THE FUTURE WE WILL WONDER HOW WE COPED WITHOUT

    SERVICE WORKERS
  12. THE BIRTH OF THE SERVICE WORKER

  13. The Application Cache CACHE MANIFEST # Version 7 CACHE: #

    Stylesheets /style/app.css # JavaScripts /js/app.js 01. 02. 03. 04. 05. 06. 07. 08.
  14. “THE APPLICATION CACHE IS A DOUCHEBAG”

  15. The Spec https:/ /w3c.github.io/ServiceWorker/

  16. Browsers 40 27 44 4

  17. The other browsers? preview under consideration

  18. SO WHAT DOES IT REALLY DO?

  19. Install it navigator.serviceWorker.register("/sw.js");

  20. Install it navigator.serviceWorker.register("/sw.js", { scope: "/admin" }); 01. 02. 03.

  21. Install it navigator.serviceWorker.register("/sw.js").then(function(){ console.log("Yay!"); }).catch(function(){ console.log("Boo!"); }); 01. 02. 03.

    04. 05.
  22. It intercepts network requests // sw.js self.addEventListener("fetch", function(event) { //

    do something }); 01. 02. 03. 04.
  23. IT'S A PROXY THAT YOU CONTROL

  24. SECURITY?

  25. HTTPS

  26. LOCALHOST

  27. DEMO

  28. WHY?

  29. WEB VS NATIVE

  30. Native Web Online by default O ine by default Discoverable

    on the web App store installation
  31. Native Web O ine rst O ine by default Discoverable

    on the web App store installation
  32. DON'T CARE FOR OFFLINE?

  33. PERFORMANCE

  34. FALLBACKS

  35. None
  36. THERE'S MORE!

  37. BUT HOW?

  38. AN EXAMPLE APPLICATION

  39. None
  40. PROGRESSIVE ENHANCEMENT

  41. WHAT DO WE NEED?

  42. BASE EXPERIENCE

  43. STATIC ASSETS

  44. OFFLINE PAGES

  45. BACKGROUND SYNC

  46. PUSH NOTIFICATIONS

  47. BASE EXPERIENCE

  48. GOOD NEWS!

  49. STATIC ASSETS

  50. DEMO

  51. THE SERVICE WORKER LIFECYCLE

  52. INSTALLING INSTALLED ACTIVATING ACTIVATED REDUNDANT

  53. CACHE INVALIDATION

  54. DEMO

  55. navigator.serviceWorker.register("/sw.js").then(function(reg) { console.log("Yay!"); reg.addEventListener("updatefound", function(){ var worker = reg.installing reg.installing.addEventListener("statechange",

    function(e) { if (worker.state == "installed") { worker.postMessage({action: "update"}); } }) }); }); 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11.
  56. self.skipWaiting();

  57. var sw = navigator.serviceWorker; sw.addEventListener("controllerchange", function() { window.location.reload(); }); 01.

    02. 03. 04.
  58. TAKE BACK CONTROL

  59. OFFLINE PAGES

  60. DATA VS FILES

  61. “FOR URL ADDRESSABLE RESOURCES, USE THE CACHE API. FOR ALL

    OTHER DATA, USE INDEXEDDB”
  62. http:/ /bit.ly/cache-vs-db

  63. IndexedDB

  64. IDB-KEYVAL LOCALFORAGE IDB DEXIE POUCHDB

  65. IDB-KEYVAL LOCALFORAGE IDB DEXIE POUCHDB

  66. var db = new Dexie("messageStore", { autoOpen: true }); db.version(1).stores({

    messages: "++,createdAt" }); 01. 02. 03. 04. 05. 06.
  67. db.transaction("w", db.messages, function() { db.messages.put({ from: FROM_NUMBER, to: TO_NUMBER, createAt:

    new Date(), body: "Service Workers rock " }).then(function(message){ console.log("Yay!"); }); }); 01. 02. 03. 04. 05. 06. 07. 08. 09. 10.
  68. db.transaction("r", db.messages, function() { db.messages.each(function(item, cursor) { render(item); }); });

    01. 02. 03. 04. 05.
  69. RENDER ON THE SERVER

  70. None
  71. APP SHELL

  72. None
  73. BOOTSTRAP FROM THE PAGE OR THE DATABASE

  74. UNIVERSAL JAVASCRIPT

  75. BACKGROUND SYNC

  76. DEMO

  77. reg.sync.register('sentMessage');

  78. self.addEventListener('sync', function(event) { // do the thing }); 01. 02.

    03.
  79. PUSH NOTIFICATIONS

  80. DEMO

  81. PROGRESSIVE WEB APPS

  82. Progressive Web Apps • Service Worker (+ HTTPS) • manifest.json

    • User engagement
  83. APP INSTALL BANNERS

  84. None
  85. ICON ON THE HOME SCREEN

  86. ENHANCEMENT

  87. AN INTRODUCTION TO SERVICE WORKERS

  88. SERVICE WORKERS ARE SCRIPTS THAT INTERCEPT NETWORK REQUESTS

  89. SO WEB DEVELOPERS CAN TREAT THE NETWORK AS AN ENHANCEMENT

  90. AND PROVIDE OFFLINE EXPERIENCES FOR USERS OF WEB APPLICATIONS

  91. AND PROVIDE OFFLINE EXPERIENCES FOR USERS OF WEB APPLICATIONS

  92. AND PROVIDE BETTER EXPERIENCES FOR USERS OF WEB APPLICATIONS

  93. IN THE FUTURE WE WILL WONDER HOW WE COPED WITHOUT

    SERVICE WORKERS
  94. AND IF YOU'RE NOT YET CONVINCED...

  95. None
  96. THANKS!

  97. Thanks! @philnash http:/ /philna.sh philnash@twilio.com