Path: blob/main/static/src/gs/public/breaklock/service-worker.js
1333 views
var APP_NAME = 'breaklock',1APP_VERSION = 12,2CACHE_NAME = APP_NAME + '_' + APP_VERSION;3var filesToCache = [4'./',5'./?utm_source=homescreen',6'./app.css',7'./app.js',8'./assets/intro.svg',9'./assets/fonts/robotomono-light-webfont.woff2',10'./assets/fonts/robotomono-light-webfont.woff',11'./assets/fonts/robotomono-light-webfont.ttf'12];1314// Service worker from Google Documentation1516self.addEventListener('install', function(event) {17// Perform install steps18event.waitUntil(19caches.open(CACHE_NAME)20.then(function(cache) {21return cache.addAll(filesToCache);22})23);24});2526self.addEventListener('activate', function(event) {27event.waitUntil(28caches.keys().then(function(cacheNames) {29return Promise.all(30cacheNames.map(function(cacheName) {31if (cacheName.indexOf(APP_NAME) === 0 && CACHE_NAME !== cacheName) {32return caches.delete(cacheName);33}34})35);36})37);38});3940self.addEventListener('fetch', function(event) {41event.respondWith(42caches.match(event.request)43.then(function(response) {44// Cache hit - return response45if (response) {46return response;47}4849// IMPORTANT: Clone the request. A request is a stream and50// can only be consumed once. Since we are consuming this51// once by cache and once by the browser for fetch, we need52// to clone the response.53var fetchRequest = event.request.clone();5455return fetch(fetchRequest).then(56function(response) {57// Check if we received a valid response58if(!response || response.status !== 200 || response.type !== 'basic') {59return response;60}6162// IMPORTANT: Clone the response. A response is a stream63// and because we want the browser to consume the response64// as well as the cache consuming the response, we need65// to clone it so we have two streams.66var responseToCache = response.clone();6768caches.open(CACHE_NAME)69.then(function(cache) {70cache.put(event.request, responseToCache);71});7273return response;74}75);76})77);78});798081