Les Services Workers

L'extinction des dinosaures

LES SERVICES WORKERS

navigator.serviceWorker.register('./service-worker.js')

INSTALLATION

if (navigator.serviceWorker) {
    navigator.serviceWorker.register('./service-worker.js')
}

INSTALLATION

self.addEventListener('fetch', function(event) {
  event.respondWith(fetch(event.request));
});

Fetch

NETWORK Only

1

4

3

2

NETWORK Only

1

2

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open('myCache')
      .then(cache => cache.addAll([
        './index.html'
      ]))
  );
});

Cache Building

self.addEventListener('fetch', function(event) {
  event.respondWith(
    fetch(event.request).catch(function() {
      return caches.match(event.request);
    })
  );
});

FETCH

NETWORK FALLING BACK CACHE

1

3

4

2

LIE-Wifi

NETWORK FALLING BACK CACHE

1

2

?

Offline FIRST

1

2

3

4

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(response) {
      return response || fetch(event.request);
    })
  );
});

FETCH

https://jakearchibald.com/2014/offline-cookbook/

Pour aller plus loin

Pour aller plus loin

  • Notification push

  • BACKGROUND SYNC

Cache Only

1

2

3

Les services workers et l'extinction des dinosaures

By miniplop

Les services workers et l'extinction des dinosaures

  • 1,170