Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
114 lines (105 loc) · 3.88 KB
/
Copy pathsw.js
File metadata and controls
114 lines (105 loc) · 3.88 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ------------------------------------------------------------------
Versioning
------------------------------------------------------------------ */
const CACHE_NAME = 'slunce-v3.0.0'; // ← bump whenever you change this file
const urlsToCache = [
'/', // root for PWA standalone launches
'/index.html',
'/style.css',
// entry + critical modules
'/src/main.js',
'/src/ui/controller.js',
'/src/core/solarCalc.js',
'/src/core/solarMath.js',
'/src/core/timezone.js',
'/src/core/solarExtras.js',
'/src/pwa/install.js',
'/src/pwa/update.js',
// metadata & icons
'/manifest.json',
'/favicon.ico',
'/icons/icon-192x192.png',
'/icons/icon-512x512.png'
];
/* ------------------------------------------------------------------
Install – cache the “app shell”
------------------------------------------------------------------ */
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(urlsToCache))
.then(() => self.skipWaiting()) // become active SW immediately
);
});
/* ------------------------------------------------------------------
Activate – drop stale caches, claim clients
------------------------------------------------------------------ */
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((names) =>
Promise.all(
names.map((name) => name !== CACHE_NAME && caches.delete(name))
)
)
.then(() => self.clients.claim())
);
});
/* ------------------------------------------------------------------
Messaging – allow page to request skipWaiting
------------------------------------------------------------------ */
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
/* ------------------------------------------------------------------
Fetch – cache-first, network fallback, offline HTML fallback
------------------------------------------------------------------ */
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((cached) => cached || fetch(event.request)
.then((response) => {
// only cache successful, same-origin, basic responses
if (
response &&
response.status === 200 &&
response.type === 'basic'
) {
const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, copy));
}
return response;
})
.catch(() => {
// if offline and navigation request: serve fallback page
if (event.request.destination === 'document') {
return caches.match('/index.html');
}
})
)
);
});
/* ------------------------------------------------------------------
Background Sync & Push (optional hooks, no functional changes)
------------------------------------------------------------------ */
self.addEventListener('sync', (event) => {
if (event.tag === 'background-sync') {
// background-sync logic goes here if ever needed
}
});
self.addEventListener('push', (event) => {
if (event.data) {
const options = {
body: event.data.text(),
icon: '/icons/icon-192x192.png',
badge: '/icons/icon-192x192.png',
tag: 'slunce-notification',
renotify: true
};
event.waitUntil(
self.registration.showNotification('Slunce', options)
);
}
});