summaryrefslogtreecommitdiffstats
path: root/sw.js
blob: 8f8d7a1b408a0cca21985992dd5b8065cdaa7790 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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.6";
const offlineUrl = "/offline.html";
const assets = [
	"/manifest.json",	// proxy	|
	"/900/404/",		// moje		|
	"/900/404/index.html",	// moje		|
	"/900/404",		// moje		|
	"/",			// 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(caches.match(event.request)
			.then((cache_res) => {
			if (cache_res) {
				return cache_res;
			} else {

///// start of fetch thingies /////

		return fetch(event.request.url)
			.then(response => {
				response.redirected = false;
				console.log(response);
				if (response.redirected) {
return new Response("<meta http-equiv=refresh content=0;/?404 />", {
		status: 200,
		statusText: "OK",
		headers: response.headers
});
				}
				return response;
			})
			.catch(error => {
				return caches.match(offlineUrl);
			});

///// end of fetch thingys /////

			}
		}));
	} 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);
		}));
	}
});