summaryrefslogtreecommitdiffstats
path: root/sw.js
blob: 6c211fe90e0ce1d3ec37e3d8506dbfaf07135160 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Change version to cause cache refresh, sw.js SE NE SME CACHAT aka ne ga dat v assets!
const static_cache_name = "site-static-v0.0.2";
const offlineUrl = "/offline.html";
const assets = [
	"/manifest.json",	// proxy	|
	"/",			// proxy	| proxyjan je tudi sw.js
	"/index.html",		// proxy	|
	"/slike/icons-512.png",	// proxy	| redfox.js je poklican iz
	"/slike/icons-192.png",	// proxy	| vsake proxyjane html strani
	"/manifest.webmanifest",// proxy	| in vsebuje custom skripte,
	"/desktop2.css",	// proxy	| kot na primer sw inštalacijo.
	"/phone2.css",		// proxy	|
	"/pin.svg",		// proxy	|
	"/redfox.js",		// moje		|
	"/slike/home100.svg",	// proxy	|
	"/slike/prPage.svg",	// proxy	|
	"/slike/prSubPage.svg",	// proxy	|
	"/slike/neSubPage.svg",	// proxy	|
	"/slike/nePage.svg",	// proxy	|
	"/slike/keyboard.svg",	// proxy	|
	"/slike/go.svg",	// proxy	|
	"/favicon.png",		// proxy	|
	"/offline.html"		// moje		|
];

self.addEventListener("install", (evt) => {
    evt.waitUntil(
        caches.open(static_cache_name).then((cache) => {
            cache.addAll(assets);
        })
    );
});

// Delete old caches
self.addEventListener("activate", evt => {
    evt.waitUntil(
        caches.keys().then((keys) => {
            return Promise.all(keys
                .filter(key => key !== static_cache_name)
                .map(key => caches.delete(key))
            );
        })
    );
});

self.addEventListener("fetch", (event) => {
	if (event.request.mode === 'navigate' || // tole ni povsod podprto, zato
		( (event.request.method === 'GET' ||	// če je GET
		event.request.method === 'POST')	// ali POST zahteva
		&& event.request.headers.get('accept').includes('text/html'))) {
		
		event.respondWith(fetch(event.request.url).catch(error => {
			return caches.match(offlineUrl);
		}));
	} else { // zahteva je za nek resource (css/js/img), ne za stran
		event.respondWith(caches.match(event.request).then((cache_res) => {
			return cache_res || fetch(event.request);
		}))
	}
});