-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
75 lines (69 loc) · 2.34 KB
/
Copy pathsw.js
File metadata and controls
75 lines (69 loc) · 2.34 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
const CACHE_NAME = 'otp-generator-cache-v91'; // Version incremented for update
const urlsToCache = [
'app.html',
'style.css',
'script.js',
'pwa-loader.js',
'icons/icon-192.png',
'icons/icon-512.png',
// Caching our custom-named favicon is essential
'faviconq.ico'
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Opened cache and caching all required assets.');
return cache.addAll(urlsToCache);
})
.then(() => {
console.log('All assets successfully cached.');
return self.skipWaiting();
})
.catch(error => {
console.error('Failed to cache one or more assets during install:', error);
})
);
});
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', event => {
const requestUrl = new URL(event.request.url);
// For cross-origin requests, do not intercept.
if (requestUrl.origin !== self.location.origin) {
return;
}
// --- SMART FAVICON HANDLING ---
// If the browser makes its automatic, hardcoded request for '/favicon.ico'...
if (requestUrl.pathname === '/favicon.ico') {
// ...we intercept it and respond with our actual icon file from the cache.
event.respondWith(caches.match('faviconq.ico'));
return; // IMPORTANT: Stop processing further for this specific request.
}
// For all other same-origin requests, use a "Cache, falling back to network" strategy.
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
// If the request is for the root path, serve app.html from cache.
if (requestUrl.pathname === '/' || requestUrl.pathname === '/index.html') {
return caches.match('app.html');
}
// For any other request, return the cached response if it exists,
// otherwise, fetch it from the network.
return cachedResponse || fetch(event.request);
})
);
});