-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
92 lines (82 loc) · 2.53 KB
/
Copy pathsw.js
File metadata and controls
92 lines (82 loc) · 2.53 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
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
90
91
92
// Service Worker for offline support and PWA features
const CACHE_NAME = 'klimaticket-v2';
const APP_SHELL_FILES = [
'./',
'./index.html',
'./manifest.json',
'./sw.js',
'./css/styles.css',
'./partials/app.html',
'./js/config.js',
'./js/i18n.js',
'./js/ui.js',
'./js/data.js',
'./js/app.js',
'./js/partials.js',
'./js/features/stats.js',
'./js/features/heatmap.js',
'./js/features/achievements.js',
'./js/features/export.js'
];
// Install event - cache files
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(APP_SHELL_FILES).catch(() => {
// If caching fails, just continue
return Promise.resolve();
});
})
);
self.skipWaiting();
});
// Activate event - clean old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
self.clients.claim();
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', (event) => {
const { request } = event;
// Only handle same-origin GET requests.
if (request.method !== 'GET' || !request.url.startsWith(self.location.origin)) {
return;
}
// Respect explicit no-store requests from the app.
if (request.cache === 'no-store') {
event.respondWith(fetch(request));
return;
}
event.respondWith((async () => {
try {
const networkResponse = await fetch(request);
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
const cache = await caches.open(CACHE_NAME);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch (_err) {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
return cachedResponse;
}
if (request.mode === 'navigate') {
const offlineShell = await caches.match('./index.html');
if (offlineShell) {
return offlineShell;
}
}
throw _err;
}
})());
});