-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
191 lines (170 loc) · 7.18 KB
/
service-worker.js
File metadata and controls
191 lines (170 loc) · 7.18 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Define a cache name with version
const VERSION = 'v7.0'; // Increment this to force cache update (v1, v2, v3, etc.)
const CACHE_NAME = `NotePlus ${VERSION}`;
// List of files to cache
const FILES_TO_CACHE = [
'/',
'/index.html',
'/public/css/styles.css',
'/public/js/file-info.js',
'/public/js/script.js',
'/public/js/search.js',
'/public/js/themes.js',
'/public/js/fetch.js',
'/public/js/popup.js',
'/public/js/ai.js',
'/manifest.json',
'/public/img/icon.png',
'/public/css/cartograph-regular-italic.woff2',
'https://kit.fontawesome.com/f3220d5256.js' // Consider replacing this with a local copy
];
// External files that may fail (not critical for app to work)
const OPTIONAL_FILES_TO_CACHE = [
'https://kit.fontawesome.com/f3220d5256.js' // Consider replacing this with a local copy
];
// Function to check if updates are available
async function checkForUpdates() {
try {
const cache = await caches.open(CACHE_NAME);
const cachedFiles = new Set();
const requests = await cache.keys();
requests.forEach(req => cachedFiles.add(req.url));
let updateAvailable = false;
// Check each file for updates
for (const file of FILES_TO_CACHE) {
try {
const response = await fetch(file, { cache: 'no-store' });
if (response.ok) {
const cachedResponse = await cache.match(file);
// Compare by checking if content changed
if (cachedResponse) {
const newContent = await response.clone().text();
const oldContent = await cachedResponse.text();
if (newContent !== oldContent) {
console.log(`[Service Worker] Update available for: ${file}`);
updateAvailable = true;
// Update the cache with new version
cache.put(file, response.clone());
//break; // Stop checking further if an update is found
}
}
}
} catch (error) {
console.error(`[Service Worker] Error checking update for ${file}:`, error);
}
}
// Notify all clients about the update status
const allClients = await clients.matchAll();
if (updateAvailable) {
allClients.forEach(client => {
client.postMessage({
type: 'UPDATE_AVAILABLE',
message: 'A new version of Blaze Audio Player is available!'
});
});
} else {
// Explicitly notify that no update was found
allClients.forEach(client => {
client.postMessage({ type: 'NO_UPDATE_FOUND' });
});
}
return updateAvailable;
} catch (error) {
console.error('[Service Worker] Error during update check:', error);
return false;
}
}
// Install event
self.addEventListener('install', (event) => {
console.log(`[Service Worker] Installing version ${VERSION}...`);
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log(`[Service Worker] Caching essential files...`);
// Cache all required files
return cache.addAll(FILES_TO_CACHE)
.then(() => {
console.log(`[Service Worker] ✓ All essential files cached successfully!`);
// Try to cache optional files but don't fail if they error
return Promise.allSettled(
OPTIONAL_FILES_TO_CACHE.map(file => cache.add(file))
).then((results) => {
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
console.log(`[Service Worker] ✓ Optional file cached: ${OPTIONAL_FILES_TO_CACHE[index]}`);
} else {
console.warn(`[Service Worker] ⚠ Failed to cache optional file: ${OPTIONAL_FILES_TO_CACHE[index]}`, result.reason);
}
});
});
});
})
.catch(err => {
console.error(`[Service Worker] ✗ Caching failed: ${err}`);
throw err;
})
);
// Force the service worker to skip the waiting phase
self.skipWaiting();
});
// Activate event
self.addEventListener('activate', (event) => {
console.log(`[Service Worker] Activating version ${VERSION}...`);
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('[Service Worker] Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
).then(() => {
console.log('[Service Worker] ✓ Cache cleanup complete');
// Notify all clients that installation is complete
return clients.matchAll().then((allClients) => {
allClients.forEach(client => {
client.postMessage({
type: 'INSTALLATION_COMPLETE',
version: VERSION,
message: 'Service Worker updated! Please refresh the page to load the latest version.'
});
});
});
});
})
);
// Take control of all pages immediately
self.clients.claim();
});
// Message event - listen for update check requests from the main page
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting(); // Forces the waiting service worker to become active
}
if (event.data && event.data.type === 'CHECK_FOR_UPDATES') {
console.log('[Service Worker] Checking for updates...');
checkForUpdates();
}
});
// To handle communication
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'GET_VERSION') {
event.ports[0].postMessage({ version: VERSION });
}
});
// Fetch event
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// Cache hit - return response from cache
if (response) {
console.log(`[Service Worker] Serving from cache: ${event.request.url}`);
return response;
}
console.log(`[Service Worker] Fetching from network: ${event.request.url}`);
return fetch(event.request);
})
);
});