const CACHE_NAME = 'Spothole';
const CACHE_URLS = [
    'static/apidocs/openapi.yml',
    'static/audio/ping.mp3',
    'static/css/style.css',
    'static/js/add-spot.js',
    'static/js/alerts.js',
    'static/js/bands.js',
    'static/js/common.js',
    'static/js/geo.js',
    'static/js/map.js',
    'static/js/spots.js',
    'static/js/spotsbandsandmap.js',
    'static/js/status.js',
    'static/js/ui-ham.js',
    'static/js/utils.js',
    'static/img/logo.png',
    'static/img/favicon.ico',
    'static/img/icon-32.png',
    'static/img/icon-192.png',
    'static/img/icon-512.png',
    'static/vendor/img/markers_default@2x.png',
    'static/vendor/img/markers_shadow.png',
    'static/vendor/img/markers_default.png',
    'static/vendor/img/markers_shadow@2x.png',
    'static/vendor/css/bootstrap-5.3.8.min.css',
    'static/vendor/css/images/marker-icon.png',
    'static/vendor/css/images/layers-2x.png',
    'static/vendor/css/images/marker-shadow.png',
    'static/vendor/css/images/layers.png',
    'static/vendor/css/images/marker-icon-2x.png',
    'static/vendor/css/leaflet-extra-markers-1.2.2.min.css',
    'static/vendor/css/leaflet-1.9.4.min.css',
    'static/vendor/css/fontawesome-6.7.2.min.css',
    'static/vendor/css/solid-6.7.2.min.css',
    'static/vendor/js/jquery-3.7.1.min.js',
    'static/vendor/js/chart-4.4.9.umd.min.js',
    'static/vendor/js/oms-leaflet-0.2.7.min.js',
    'static/vendor/js/leaflet-workedallbritainireland.js',
    'static/vendor/js/leaflet-providers-2.0.0.js',
    'static/vendor/js/text-image-0.7.0.js',
    'static/vendor/js/leaflet-vectorgrid-1.3.0.js',
    'static/vendor/js/leaflet-ituzones.js',
    'static/vendor/js/tinycolor2-1.6.0.min.js',
    'static/vendor/js/bootstrap-5.3.8.bundle.min.js',
    'static/vendor/js/moment-2.29.4.min.js',
    'static/vendor/js/leaflet-cqzones.js',
    'static/vendor/js/leaflet-extra-markers-1.2.2.min.js',
    'static/vendor/js/leaflet-terminator-1.1.0.min.js',
    'static/vendor/js/leaflet-1.9.4.min.js',
    'static/vendor/js/leaflet-geodesic-2.7.2.umd.min.js',
    'static/vendor/js/leaflet-maidenhead.js',
    'static/vendor/webfonts/fa-solid-900.ttf',
    'static/vendor/webfonts/fa-solid-900.woff2'
];

self.addEventListener('fetch', (event) => {
    // Is this an asset we can cache?
    const url = new URL(event.request.url);
    const isCacheableRequest = CACHE_URLS.includes(url.pathname);

    if (isCacheableRequest) {
        // Open the cache
        event.respondWith(caches.open(CACHE_NAME).then((cache) => {
            // Go to the network first, cacheing the response
            return fetch(event.request.url).then((fetchedResponse) => {
                cache.put(event.request, fetchedResponse.clone());

                return fetchedResponse;
            }).catch(() => {
                // If the network is unavailable, get from cache.
                return cache.match(event.request.url);
            });
        }));
    } else {
        // Not a cacheable request, must be a call to the API, so no cache involved just go to the network
    }
});
