-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
158 lines (146 loc) · 4.53 KB
/
sw.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"use strict";
import { VERSION } from "./config.js";
const appAssets = [
"config.js",
"index.html",
"info.html",
"help.html",
"app.js",
"core/lib.js",
"core/storage.js",
"settings.json",
"css/pwa.css",
"css/style.css",
"icons/back.svg",
"icons/change.svg",
"icons/email.svg",
"icons/generate.svg",
"icons/right-generate.svg",
"icons/granite.png",
"icons/help.svg",
"icons/info.svg",
"icons/install.svg",
"icons/lock.svg",
"icons/logo.svg",
"icons/reload.svg",
"icons/reset.svg",
"icons/save.svg",
"icons/share.svg",
"privacy/index.html",
];
self.addEventListener("install", (installEvent) => {
const msg = { install: true };
const installChannel = new BroadcastChannel("installChannel");
installEvent.waitUntil(
Promise.all([
// Broadcast the installation message
installChannel.postMessage(msg),
// Cache all app assets
caches.open(`static-${VERSION}`)
.then(cache => cache.addAll(appAssets))
.catch(error => {
console.error('Failed to cache assets:', error);
throw error; // Re-throw to indicate installation failure
}),
self.skipWaiting()
])
.then(() => {
console.log('Installation completed successfully');
})
.catch(error => {
console.error('Installation failed:', error);
// Optionally, you could do additional error handling here
})
.finally(() => {
installChannel.close(); // Close the channel after use
})
);
});
self.addEventListener("activate", (event) => {
const cleaned = caches.keys().then((keys) => {
return Promise.all(
keys.map((key) => {
if (key !== `static-${VERSION}` && key.startsWith("static-")) {
return caches.delete(key); // Delete old cache
}
})
);
});
event.waitUntil(
Promise.all([
cleaned, // Clean up old caches
self.clients.claim() // Take control of all clients immediately
])
);
});
async function staticCache(req) {
try {
// Try fetching the resource from the network
const networkRes = await fetch(req);
// If the response is valid and not a partial response (status !== 206), cache it
if (networkRes.ok && networkRes.status !== 206) {
const cache = await caches.open(`static-${VERSION}`);
await cache.put(req, networkRes.clone()); // Cache the cloned response
}
// Return the network response (cached or not)
return networkRes;
} catch (error) {
console.warn('Fetch request failed, falling back to cache:', error);
// If the fetch fails (e.g., offline), try retrieving the resource from the cache
const cache = await caches.open(`static-${VERSION}`);
const cachedRes = await cache.match(req);
if (cachedRes) {
return cachedRes; // Return the cached response if available
}
// If no cached response is found, throw an error or handle it gracefully
return new Response('Offline and resource not found in cache', {
status: 503,
statusText: 'Service Unavailable',
});
}
}
self.addEventListener("fetch", (e) => {
// Only handle requests within the same origin as your app
if (e.request.url.startsWith(location.origin)) {
e.respondWith(staticCache(e.request));
}
});
self.addEventListener("load", (e) => {
const debug = false;
if (debug) console.log("sw: load event detected: e= ", e);
});
// service-worker.js
// Listen to the request
self.addEventListener("message", (event) => {
const debug = false;
if (debug) console.log("sw: message: event= ", event);
if (event.data && event.data.type === "GET_VERSION") {
// Select who we want to respond
if (debug) console.log("sw: message: event.data= ", event.data);
self.clients
.matchAll({
includeUncontrolled: true,
type: "window",
})
.then((clients) => {
if (clients && clients.length) {
// Send a response - the clients
// array is ordered by last focused
const msg = { type: "VERSION", VERSION: VERSION };
if (debug) console.log("sw: message: postMessage: msg= ", msg);
clients[0].postMessage(msg);
}
});
}
});
let storedPassword = null;
self.addEventListener("message", (event) => {
event.waitUntil((async () => {
if (event.data && event.data.type === "store-password") {
storedPassword = event.data.password;
const tag = event.data.password;
} else if (event.data && event.data.type === "retrieve-password") {
event.source.postMessage({type: "password", password: storedPassword});
}
})());
});