summaryrefslogtreecommitdiffhomepage
path: root/app/tilecache.sw.js
blob: 15ec8f9493971560e8ded9762ef4b2ccf333c3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
this.addEventListener('install', function (event) {
  console.log('Installing Service Worker');
  event.waitUntil(this.skipWaiting());
});

this.addEventListener('activate', function (event) {
  event.waitUntil(this.clients.claim());
});

this.addEventListener('fetch', function(event) {
  var url = event.request.url;

  if(url.startsWith('https://') && (url.includes('tiles.mapbox.com') || url.includes('api.mapbox.com'))) {
    event.respondWith(
      caches.match(event.request).then(function(resp) {
        return resp || fetch(event.request).then(function(response) {
          var cacheResponse = response.clone();
          caches.open('mapbox').then(function(cache) {
            cache.put(event.request, cacheResponse);
          });
          return response;
        });
      })
    );
  }
});