Skip to content
Snippets Groups Projects
service-worker.js 2.5 KiB
Newer Older
Timothee P's avatar
Timothee P committed
/* global workbox */ //* allow undefined variable for 'workbox' in this file (because global variable) to avoid eslint error
DESPRES Damien's avatar
DESPRES Damien committed
// custom service-worker.js
if (workbox) {
  // adjust log level for displaying workbox logs
  //workbox.core.setLogLevel(workbox.core.LOG_LEVELS.debug)
DESPRES Damien's avatar
DESPRES Damien committed

  // apply precaching. In the built version, the precacheManifest will
Florent Lavelle's avatar
Florent Lavelle committed
  // be imported using importScripts (as is workbox itself) and we can
  // precache this. This is all we need for precaching
  workbox.precaching.precacheAndRoute(self.__precacheManifest);
DESPRES Damien's avatar
DESPRES Damien committed

  //workbox.core.skipWaiting();
DESPRES Damien's avatar
DESPRES Damien committed

Timothee P's avatar
Timothee P committed
  // Make sure to return a specific response for all navigation requests.
  // Since we have a SPA here, this should be index.html always.
  // https://stackoverflow.com/questions/49963982/vue-router-history-mode-with-pwa-in-offline-mode
  workbox.routing.registerNavigationRoute('/geocontrib/index.html', {
    blacklist: [/\/api/,/\/admin/,/\/media/,/\/cas/],
  });
DESPRES Damien's avatar
DESPRES Damien committed

  workbox.routing.registerRoute(
    new RegExp('.*/config/config.json'),
    new workbox.strategies.StaleWhileRevalidate({
      cacheName: 'config',
    })
  );
DESPRES Damien's avatar
DESPRES Damien committed

  workbox.routing.registerRoute(
    new RegExp('.*/api/.*'),
    new workbox.strategies.NetworkFirst({
      cacheName: 'api',
      plugins: [
        new workbox.cacheableResponse.Plugin({
          statuses: [0, 200],
        }),
      ],
    })
  );
  workbox.routing.registerRoute(
    /^https:\/\/[a-zA-Z]\.tile\.openstreetmap\.fr/,
    new workbox.strategies.CacheFirst({
      cacheName: 'osm',
      plugins: [
        new workbox.cacheableResponse.Plugin({
          statuses: [0, 200],
        }),
        new workbox.expiration.Plugin({
          maxAgeSeconds: 60 * 60 * 24 * 365,
          // maxEntries: 30, pour limiter le nombre d'entrée dans le cache
        }),
      ],
    })
  );
    new RegExp('.*/service=WMS&request=GetMap/.*'),
      cacheName: 'wms',
      plugins: [
        new workbox.cacheableResponse.Plugin({
          statuses: [0, 200],
        }),
        new workbox.expiration.Plugin({
          maxAgeSeconds: 60 * 60 * 24 * 365,
          // maxEntries: 30, pour limiter le nombre d'entrée dans le cache
        }),
      ],
    })
  );
DESPRES Damien's avatar
DESPRES Damien committed

}

// This code listens for the user's confirmation to update the app.
self.addEventListener('message', (e) => {
  if (!e.data) {
    return;
  }
  //console.log(e.data);
  switch (e.data.type) {
  case 'SKIP_WAITING':
    self.skipWaiting();
    break;
  default:
    // NOOP
    break;
  }
});
DESPRES Damien's avatar
DESPRES Damien committed