-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
108 lines (96 loc) · 3.06 KB
/
Copy pathsw.js
File metadata and controls
108 lines (96 loc) · 3.06 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
// Service Worker for Task Timer App
const VERSION = '1.0.1';
const CACHE_NAME = 'task-timer-cache-v' + VERSION;
const OFFLINE_PAGE = 'offline.html';
// Use relative paths to work in any subdirectory
const urlsToCache = [
'./',
'./index.html',
'./styles.css',
'./script.js',
'./sounds/win.mp3',
'./offline.html',
'./manifest.json'
];
// Install event - cache assets
self.addEventListener('install', event => {
console.log(`Task Timer Service Worker v${VERSION} installing...`);
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log(`Cache opened: ${CACHE_NAME}`);
return cache.addAll(urlsToCache).then(() => {
console.log('All resources cached successfully');
return self.skipWaiting(); // Activate immediately
}).catch(error => {
console.error('Failed to cache all resources:', error);
// Continue even if some resources fail to cache
return self.skipWaiting();
});
})
);
});
// Fetch event - serve from cache, fall back to network
self.addEventListener('fetch', event => {
// Skip non-HTTP/HTTPS requests (like chrome-extension:// URLs)
if (!event.request.url.startsWith('http')) {
return;
}
event.respondWith(
caches.match(event.request)
.then(response => {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request).then(
response => {
// Check if we received a valid response
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response
const responseToCache = response.clone();
// Cache the response, but handle potential errors
caches.open(CACHE_NAME)
.then(cache => {
try {
cache.put(event.request, responseToCache);
} catch (error) {
console.warn('Failed to cache response for:', event.request.url, error);
}
});
return response;
}
).catch(error => {
console.error('Fetch failed:', error);
// Return the offline page for navigation requests
if (event.request.mode === 'navigate') {
return caches.match(OFFLINE_PAGE);
}
});
})
);
});
// Activate event - clean up old caches and take control immediately
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
// Clean up old caches
const cacheCleanup = 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);
}
})
);
});
// Ensure the service worker takes control immediately
event.waitUntil(
Promise.all([
cacheCleanup,
self.clients.claim() // Take control of all clients
])
);
});