-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
238 lines (205 loc) · 7.6 KB
/
sw.js
File metadata and controls
238 lines (205 loc) · 7.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// TaskMaster Pro - Service Worker for Offline Support
const CACHE_NAME = 'taskmaster-pro-v1.0.0';
const STATIC_CACHE = 'taskmaster-static-v1.0.0';
// Files to cache for offline use
const STATIC_FILES = [
'/',
'/index.html',
'/app.html',
'/css/style.css',
'/css/app.css',
'/js/main.js',
'/js/utils.js',
'/js/storage.js',
'/js/taskManager.js',
'/js/projectManager.js',
'/js/ui.js',
'/js/views.js',
'/js/app-main.js',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css',
'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap'
];
// Install event - cache static files
self.addEventListener('install', (event) => {
console.log('Service Worker: Installing...');
event.waitUntil(
caches.open(STATIC_CACHE)
.then((cache) => {
console.log('Service Worker: Caching static files');
return cache.addAll(STATIC_FILES);
})
.then(() => {
console.log('Service Worker: Static files cached successfully');
return self.skipWaiting();
})
.catch((error) => {
console.error('Service Worker: Failed to cache static files', error);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
console.log('Service Worker: Activating...');
event.waitUntil(
caches.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== STATIC_CACHE && cacheName !== CACHE_NAME) {
console.log('Service Worker: Deleting old cache', cacheName);
return caches.delete(cacheName);
}
})
);
})
.then(() => {
console.log('Service Worker: Activated successfully');
return self.clients.claim();
})
);
});
// Fetch event - serve cached files when offline
self.addEventListener('fetch', (event) => {
// Skip non-GET requests
if (event.request.method !== 'GET') {
return;
}
// Skip chrome-extension and other non-http requests
if (!event.request.url.startsWith('http')) {
return;
}
event.respondWith(
caches.match(event.request)
.then((cachedResponse) => {
// Return cached version if available
if (cachedResponse) {
return cachedResponse;
}
// Otherwise, fetch from network
return fetch(event.request)
.then((response) => {
// Don't cache non-successful responses
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response
const responseToCache = response.clone();
// Cache the response for future use
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(() => {
// If network fails and no cache, return offline page
if (event.request.destination === 'document') {
return caches.match('/app.html');
}
});
})
);
});
// Background sync for when connection is restored
self.addEventListener('sync', (event) => {
console.log('Service Worker: Background sync triggered', event.tag);
if (event.tag === 'background-sync') {
event.waitUntil(
// Perform background sync operations
syncData()
);
}
});
// Push notification handling
self.addEventListener('push', (event) => {
console.log('Service Worker: Push notification received', event);
const options = {
body: event.data ? event.data.text() : 'New notification from TaskMaster Pro',
icon: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">✅</text></svg>',
badge: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">✅</text></svg>',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
},
actions: [
{
action: 'explore',
title: 'View Tasks',
icon: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">👀</text></svg>'
},
{
action: 'close',
title: 'Close',
icon: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">❌</text></svg>'
}
]
};
event.waitUntil(
self.registration.showNotification('TaskMaster Pro', options)
);
});
// Notification click handling
self.addEventListener('notificationclick', (event) => {
console.log('Service Worker: Notification clicked', event);
event.notification.close();
if (event.action === 'explore') {
// Open the app
event.waitUntil(
clients.openWindow('/app.html')
);
} else if (event.action === 'close') {
// Just close the notification
return;
} else {
// Default action - open the app
event.waitUntil(
clients.openWindow('/app.html')
);
}
});
// Message handling from main thread
self.addEventListener('message', (event) => {
console.log('Service Worker: Message received', event.data);
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
if (event.data && event.data.type === 'GET_VERSION') {
event.ports[0].postMessage({ version: CACHE_NAME });
}
});
// Helper function for background sync
async function syncData() {
try {
console.log('Service Worker: Syncing data...');
// Here you would typically sync data with a server
// For now, we'll just log that sync is happening
// Send message to main thread about sync completion
const clients = await self.clients.matchAll();
clients.forEach(client => {
client.postMessage({
type: 'SYNC_COMPLETE',
timestamp: Date.now()
});
});
console.log('Service Worker: Data sync completed');
} catch (error) {
console.error('Service Worker: Data sync failed', error);
}
}
// Periodic background sync (if supported)
self.addEventListener('periodicsync', (event) => {
console.log('Service Worker: Periodic sync triggered', event.tag);
if (event.tag === 'content-sync') {
event.waitUntil(syncData());
}
});
// Handle errors
self.addEventListener('error', (event) => {
console.error('Service Worker: Error occurred', event.error);
});
// Handle unhandled promise rejections
self.addEventListener('unhandledrejection', (event) => {
console.error('Service Worker: Unhandled promise rejection', event.reason);
});
console.log('Service Worker: Script loaded successfully');