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

FrontEndLondon: ServiceWorker & the offline web

Tom Ashworth
January 30, 2014

FrontEndLondon: ServiceWorker & the offline web

Native platforms hold something over the web: we do not have a meaningful way to ensure that, no matter what the connection of the user is like, we can always deliver code and content. ServiceWorker aims to fix this, allowing developers to take control of navigations and resource fetching before they go to the network. We'll go through the state of things today, look at the ServiceWorker proposal and try out the polyfill that you can use today.

Tom Ashworth

January 30, 2014
Tweet

More Decks by Tom Ashworth

Other Decks in Technology

Transcript

  1. Offline Tom Ashworth | ServiceWorker & the offline web |

    30/01/14 Connectivity is simply not ubiquitous. Think trains, cars, walking and in the Tube. Developing nations. The web should be a viable platform, but isn’t.
  2. Online is the problem. There is no way to know.

    Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  3. Online is the problem. There is no way to know.

    Think airport, hotel or train wifis – requests appear to work. Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  4. #offlinefirst The network is a potentially-unavailable resource. We talk about

    progressive enhancement in terms of browser features, but it’s time to think of the network as one such optional extra. Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  5. #offlinefirst This means we have to start thinking about solving

    problems like synchronisation. Projects like Hood.ie are doing great things here. Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  6. ServiceWorker Handle resource requests with a programmatically controlled, durable cache.

    Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  7. ServiceWorker Think about applications as a shell + content: tweets

    in TweetDeck; articles on the Guardian; emails in Gmail; posts on your blog. Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  8. Page AppCache Browser HTTP + Cache Tom Ashworth | ServiceWorker

    & the offline web | 30/01/14 No programmatic access is problematic.
  9. Page Browser ServiceWorker HTTP + Cache Tom Ashworth | ServiceWorker

    & the offline web | 30/01/14 ServiceWorker sits between your page and the browser’s network stack.
  10. Page Browser ServiceWorker HTTP + Cache Tom Ashworth | ServiceWorker

    & the offline web | 30/01/14 It can intercept, modify and respond to all network requests from the page.
  11. Page Browser ServiceWorker Caches HTTP + Cache Tom Ashworth |

    ServiceWorker & the offline web | 30/01/14 It all has programmatic access to a set of durable caches.
  12. Page ServiceWorker Browser Caches Page Page Tom Ashworth | ServiceWorker

    & the offline web | 30/01/14 1 to many, like an extension.
  13. navigator.serviceWorker .register(‘/worker.1.js’, { scope: ‘/blog’ }); Tom Ashworth | ServiceWorker

    & the offline web | 30/01/14 Limiting the set of URLs the worker controls.
  14. var cache = new Cache( ‘/assets/shell.1.html', ‘/assets/app.1.css', ‘/assets/app.1.js' ); Tom

    Ashworth | ServiceWorker & the offline web | 30/01/14 Creating & populating a cache.
  15. var cache = new Cache( ‘/assets/shell.1.html', ‘/assets/app.1.css', ‘/assets/app.1.js' ); !

    cache.add(‘/api/user.json’); Tom Ashworth | ServiceWorker & the offline web | 30/01/14 Adding to a cache.
  16. var cache = new Cache( ‘/assets/shell.1.html', ‘/assets/app.1.css', ‘/assets/app.1.js' ); !

    cache.add(‘/api/user.json’); ! this.caches.set(‘app-v1’, cache); Tom Ashworth | ServiceWorker & the offline web | 30/01/14 Persisting a cache to the global cache list. This will make it available across worker restarts.
  17. this.addEventListener(‘fetch’, function (event) { event.respondWith( this.caches.match(event.request.url) ); }); Tom Ashworth

    | ServiceWorker & the offline web | 30/01/14 Intercepting a request and responding from the cache.
  18. this.addEventListener(‘fetch’, function (event) { event.respondWith( this.caches.match(event.request.url) ); }); Tom Ashworth

    | ServiceWorker & the offline web | 30/01/14 Matched based on URL (although this isn’t well defined yet). Promise-based.
  19. this.addEventListener(‘fetch’, function (event) { var match = event.request.url; if (event.purpose

    === ‘navigate’) { match = ‘shell.1.html’; } event.respondWith( this.caches.match(match) ); }); Tom Ashworth | ServiceWorker & the offline web | 30/01/14 Responding differently to page navigations.
  20. this.addEventListener(‘fetch’, function (event) { var match = event.request.url; if (event.purpose

    === ‘navigate’) { match = ‘shell.1.html’; } event.respondWith( this.caches.match(match) ); }); Tom Ashworth | ServiceWorker & the offline web | 30/01/14 We can check the ‘purpose’ of the fetch event to respond differently.
  21. this.addEventListener(‘install’, function (event) {}); this.addEventListener(‘activate’, function (event) {}); Tom Ashworth

    | ServiceWorker & the offline web | 30/01/14 Other kinds of event, for populating caches and performing upgrades.
  22. Not in browsers* * but on the way… Tom Ashworth

    | ServiceWorker & the offline web | 30/01/14
  23. <demo> May the demogods bestow good fortune upon us. Tom

    Ashworth | ServiceWorker & the offline web | 30/01/14
  24. </demo> Past me hopes that went well. Present me is

    probably sweating. Future me is cringing at this slide.
  25. Your turn. Get involved. Try it out. Explore the spec.

    Understand the UX implications. Push it to the limits. Tom Ashworth | ServiceWorker & the offline web | 30/01/14
  26. Your turn. Also, work on the polyfill! It’s on GitHub

    too. github.com/phuu/ServiceWorker-Polyfill Tom Ashworth | ServiceWorker & the offline web | 30/01/14