-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
57 lines (54 loc) · 1.78 KB
/
Copy pathsw.js
File metadata and controls
57 lines (54 loc) · 1.78 KB
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
// Stjernekikkert service worker (PERF-3) — offline-støtte.
// Cache-first for app-skall; nett-fallback for alt annet (f.eks. vær-API senere).
var CACHE = 'stjernekikkert-v25';
var ASSETS = [
'index.html',
'objekter.html',
'anbefalt.html',
'setup.html',
'kollimering.html',
'sikt.html',
'shared.js',
'shared.css',
'manifest.json',
'icon.svg'
];
self.addEventListener('install', function(e){
e.waitUntil(
caches.open(CACHE).then(function(c){ return c.addAll(ASSETS); }).then(function(){ return self.skipWaiting(); })
);
});
self.addEventListener('activate', function(e){
e.waitUntil(
caches.keys().then(function(keys){
return Promise.all(keys.map(function(k){ if (k !== CACHE) return caches.delete(k); }));
}).then(function(){ return self.clients.claim(); })
);
});
self.addEventListener('fetch', function(e){
var req = e.request;
if (req.method !== 'GET') return;
// Open-Meteo (vær): alltid live, aldri cache — la nettleseren håndtere det rett.
if (new URL(req.url).hostname.indexOf('open-meteo.com') >= 0) return;
// Google Fonts og andre cross-origin: nett, men cache for offline gjenbruk
if (new URL(req.url).origin !== self.location.origin){
e.respondWith(
fetch(req).then(function(res){
var copy = res.clone();
caches.open(CACHE).then(function(c){ c.put(req, copy); });
return res;
}).catch(function(){ return caches.match(req); })
);
return;
}
// Egne filer: cache først, så nett
e.respondWith(
caches.match(req).then(function(hit){
return hit || fetch(req).then(function(res){
var copy = res.clone();
caches.open(CACHE).then(function(c){ c.put(req, copy); });
return res;
});
}).catch(function(){ return caches.match('index.html'); })
);
});