Skip to content
Snippets Groups Projects
service-worker.js 1.96 KiB
Newer Older
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)

    // apply precaching. In the built version, the precacheManifest will
    // 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);

    //workbox.core.skipWaiting();

    // 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('/index.html')

    workbox.routing.registerRoute(
        new RegExp('.*/config/config.json'),
        new workbox.strategies.StaleWhileRevalidate({
            cacheName: 'config',
        })
    )

    workbox.routing.registerRoute(
        new RegExp('.*/api/.*'),
        new workbox.strategies.NetworkFirst({
            cacheName: 'api',
        })
    )
    workbox.routing.registerRoute(
        /^https:\/\/c\.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,
DESPRES Damien's avatar
DESPRES Damien committed
                    // 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;
    }
})