ctucx.git: smartie-pwa

[js] smarthome web-gui

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
'use strict';

let preCache = [
	'./',
	'./main.js',
	'./favicon-512x512.png',
	'./manifest.json',
	'https://fonts.googleapis.com/icon?family=Material+Icons',
	'https://fonts.gstatic.com/s/materialicons/v47/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2',
];

const CACHE = 'cache-v6';

self.addEventListener('install', function (evt) {
	self.skipWaiting();
	evt.waitUntil(caches.open(CACHE).then(function (cache) {
		cache.addAll(preCache);
	}));
});

self.addEventListener('fetch', function (evt) {
	evt.respondWith(fromCache(evt.request).then(function (match) {
		if (match) {
			return match;
		} else {
			return fetch(evt.request);
		}
	}));
});

self.addEventListener('activate', function (event) {
	event.waitUntil(clients.claim());
	event.waitUntil(clients.claim().then(function () {
		return caches.keys().then(function (cacheNames) {
			return Promise.all(cacheNames.filter(c => c !== CACHE).map(c => caches.delete(c)));
		});
	}));
});

function fromCache (request) {
	return caches.open(CACHE).then(function (cache) {
		return cache.match(request);
	});
}