service worker internals
play

Service Worker Internals ~{@saket_kmr} & {@piyuesh23} Who we - PowerPoint PPT Presentation

Service Worker Internals ~{@saket_kmr} & {@piyuesh23} Who we are? @saki007ster @piyuesh23 Technical Architect, QED42 UI/UX Lead, QED42 Agenda Mobile Web Vs Mobile Apps PWA & Service worker introduction Service worker


  1. Service Worker Internals ~{@saket_kmr} & {@piyuesh23}

  2. Who we are? @saki007ster @piyuesh23 Technical Architect, QED42 UI/UX Lead, QED42

  3. Agenda ● Mobile Web Vs Mobile Apps PWA & Service worker introduction ● Service worker dive-in ● Basic Caching strategies ● ● Tooling & debugging Demo ●

  4. Mobile Web V S Mobile Apps

  5. 67% 33%

  6. ?

  7. Tabbed Experience

  8. Offline Enabled

  9. Notifications Simple Notifications Push Messages

  10. Branding

  11. And Many More...

  12. The Web is Dead ● 08/2010 ● http://www.wired. com/2010/08/ff_webrip/

  13. The Web is Not Dead ● 02/2014 ● http://www.wired. com/insights/2014/02/web- dead/

  14. Progressive Web Apps ● Good Experience on flaky network connections ● Fast Loading ● Obscure branding via splash screens ● Installable ● Push notifications ● 1-click away from accessing content

  15. Introduction to Service Workers

  16. Features Programmable cache Offline Web Geo-fencing 2G Functional in Background case of slow processing connections EDGE Push Notifications

  17. https-only

  18. Pillars of SW

  19. Working With Service Workers

  20. No service worker Installing Error Activate Idle Terminated Fetch / Message

  21. Step 1: Registering a service worker Check for Browser Support if ('serviceWorker' in Navigator) { navigator.serviceWorker.register('/sw.js', { scope: './' }).then(function(reg) { Scope for serviceWorker to // registration worked operate upon. console.log('Registration succeeded. Scope is ' + reg.scope); Default scope is set to the location of sw.js file. }).catch(function(err) { // Registration failed. console.log('Registration failed with ' + error); Navigator.serviceWorker. }); controller specified the } worker controlling a page.

  22. self.addEventListener('install', function(event) { Install: pre-cache & app- console.log('[install] Kicking off server worker registration.'); initialization event.waitUntil( // pre-fetch app-shell & perform initialization steps ); App-shell: minimal HTML, }); CSS, and JavaScript powering a user interface self.addEventListener('activate', function(event) { // Anything needed to be done before activation. e.g., cleanup old Activate: Mostly useful in cache. case of SW updates. }); Cleanup of old cache.

  23. self.addEventListener('fetch', function(event) { // Intercept requests going to Network. // Mostly useful to return data from cache or cache // network response. });

  24. Basic Cache-ing Strategies

  25. Most useful for single page websites or ● places where data/app are separate. Cache content offline while installing ● service worker. Request gets served from Service ● worker cache(when offline). Request served from internet(when ● online) - caching response.

  26. { var cacheName = 'DCON NOLA'; var staticResources = [ App-Shell '/script.js', '/style.css' ]; self.addEventListener('install', function(event) { event.waitUntil( Wait before activation until cache.open(cacheName).then(function(cache) { app-shell is cached. return cache.addAll(staticResources); }); ); })

  27. self.addEventListener('fetch', function(event) { navigator checks for the event.respondWith(serverFromCache(event.request)); network. returns true or }); false. function serveFromCache(request) { respondWith() receives caches.match(request).then(function(response)) { only a valid response return response; object. }).catch(function(err) { return caches.match('/offline.html'); }) }

  28. function serverFromNetwork(event) { Fetch response from network & update cache caches.open(cacheName).then(function(cache) { return fetch(event.request.url).then(function (response) { cache.put(event.request.url, response.clone()); Response object is a readable return response; stream object & can be }).catch(function(err) { consumed only once(either to return cache.match(event.request.url).then(function update cache or respond back). (response) { return response; }); If something goes wrong with the network fetch, respond back }); with a stale copy from cache. }) }

  29. Got another custom scenario/Handling cache busting...

  30. Break down Css & Js assets var jsBucket = 'JS'; into different cache buckets. var cssBucket = 'CSS'; var cssResources = [ '/slides.css', '/styles.css' ]; var jsResources = [ '/slides.js', '/scripts.js' ];

  31. self.addEventListener('install', function(event) { Avoid overhead for sw.js check console.log('[install] Kicking off server worker registration.'); on each page using http cache- event.waitUntil( control headers. caches.open(cssBucket).then(function(cache) { cache.addAll(cssResources); }); caches.open(jsBucket).then(function(cache) { cache.addAll(jsResources); }); ); });

  32. Activate fired after installing the self.addEventListener('activate', function(event) { service worker and all the var cacheWhiteList = [jsBucket, cssBucket]; current versions of sw are closed. event.waitUntil(function(caches) { return Promise.all( caches.map(function(cache) { if (cacheWhileList.indexOf(cache) == -1) { return caches.delete(cache); Use event.replace() inside the } install event for the installing }) service worker to take effect instantly. ); }) })

  33. Browser Support 40+ 27+ 44+ Need to support a browser that isn’t ready, Don’t worry we got you covered

  34. Tooling & debugging

  35. chrome://serviceworker-internals/

  36. Local development without ssl-certificates ./Google\ Chrome --user-data-dir=/tmp --ignore- certificate-errors --unsafely-treat-insecure-origin-as- secure=http://dcp.16

  37. Network requests

  38. Chrome devtools

  39. sw-toolbox: https://github.com/GoogleChrome/sw-toolbox - navigator.serviceWorker.register('my-service-worker.js'); - global.toolbox.options.debug = true // The route for any requests from the googleapis origin toolbox.router.get('/(.*)', global.toolbox. cacheFirst, { origin: /\.googleapis\.com$/ });

  40. Common use-cases Caching requests for authenticated users: fetch(url, {credentials: true}).then(function (response){ // Request will be sent to the server with cookies. }) Caching cross-origin resources: fetch(url, {mode: ‘no-cors’}).then(function (response){ // The response will be opaque i.e., we would never know if // the request was successful. })

  41. DEMO

  42. Use-case: - Drupal Camp Pune website - Allow users to access the website offline - Provide an app-like experience - Allow users to add sessions to their schedule even when they are offline. - Redirect pages that are not cached to an offline fallback page.

  43. Cache-strategies: Drupal Enhancements: Offline first ● - Custom route for returning lis to aggregated assets: /css- Network first ● js-aggregated.json Slow connection: ● Again offline first approach would fit in ○ - Custom route for adding/removing schedule flag: /schedule/{entity_id}/{action}/{uid} here. Offline page: ● - Rest endpoints for session & sponsor nodes: /sessions. Redirect users to an offline page ○ json & /sponsors.json showing some info about the event, - hook_page_attachments_alter() to handle loading of asking them to go online for viewing service worker js & manifest.json(for PWA) files. details

  44. Resources ● ● ●

  45. So How Was It? - Tell Us What You Think Thanks!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend