go-pb-jubilee/pb_public/service-worker.js
Martin Donnelly 9b11de7902 init
2024-08-28 00:24:33 +01:00

178 lines
5.4 KiB
JavaScript

// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const CACHE_VERSION = { 'version': '0.0.1049' };
const PRECACHE = `jubileeData-${CACHE_VERSION.version}`;
const RUNTIME = 'runtime';
const PRECACHE_URLS = [
'/',
'/index.html',
'/service-worker.js',
'/site.webmanifest',
'/browserconfig.xml',
'/css/mui.custom.css',
'/js/bundle.js',
'/js/vendor.js',
'/img/favicon-16x16.png',
'/img/favicon-32x32.png',
'/img/android-chrome-192x192.png',
'/img/android-chrome-512x512.png',
'/gfx/default_daily_image.jpg',
'/gfx/stars_00.png',
'/gfx/stars_10.png',
'/gfx/stars_15.png',
'/gfx/stars_20.png',
'/gfx/stars_25.png',
'/gfx/stars_30.png',
'/gfx/stars_35.png',
'/gfx/stars_40.png',
'/gfx/stars_45.png',
'/gfx/stars_50.png',
'/gfx/yssdk_yelp_logo.png',
'/gfx/bg_evening.jpg',
'/gfx/bg_morning.jpg',
'/gfx/clear_d.jpg',
'/gfx/clear_n.jpg',
'/gfx/cloudy_d.jpg',
'/gfx/cloudy_n.jpg',
'/gfx/foggy_d.jpg',
'/gfx/foggy_n.jpg',
'/gfx/rain_d.jpg',
'/gfx/rain_n.jpg',
'/gfx/snow_d.jpg',
'/gfx/snow_n.jpg',
'/gfx/storm_d.jpg',
'/gfx/storm_n.jpg',
'/fonts/fonts.css',
'/fonts/fujicons.css',
'/fonts/fujicons.ttf',
'/fonts/Roboto-italic-400.woff',
'/fonts/Roboto-italic-700.woff',
'/fonts/Roboto-normal-100.woff',
'/fonts/Roboto-normal-300.woff',
'/fonts/Roboto-normal-400.woff',
'/fonts/Roboto-normal-500.woff',
'/fonts/Roboto-normal-700.woff',
'/fonts/Roboto-normal-900.woff',
'/fonts/Roboto_Condensed-normal-300.woff',
'/fonts/Roboto_Condensed-normal-400.woff',
'/fonts/Roboto_Condensed-normal-700.woff'
];
const liveData = ['news', 'agenda'];
self.addEventListener('install', event => {
console.warn('Installing...');
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
console.warn('Activate...');
const currentCaches = [PRECACHE, RUNTIME];
event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});
// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
console.warn('Fetch', event.request.url);
// Skip cross-origin requests, like those for Google Analytics.
/* if (event.request.url.startsWith(self.location.origin)) {
console.log('One of our requests..');
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
console.log('cachedResponse', cachedResponse);
return cachedResponse;
}
return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone()).then(() => {
console.log('fetch cache response', response);
return response;
});
});
});
})
);
}*/
if (event.request.url.startsWith(self.location.origin)) {
console.log('!!', event.request);
/* event.respondWith(
caches.open(RUNTIME).then(function(cache) {
return cache.match(event.request).then(function (response) {
console.log('£', response);
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);*/
event.respondWith(
caches.open(RUNTIME).then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
}
if (event.request.url.startsWith('https://maps.googleapis.com')) {
const url = new URL(event.request.url);
const locCache = `loc-${url.searchParams.get('latlng')}`;
event.respondWith(
caches.match(locCache).then(cachedResponse => {
if (cachedResponse)
return cachedResponse;
return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(locCache, response.clone()).then(() => {
return response;
});
});
});
})
);
}
});