forked from Renu-code123/ExpenseFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
182 lines (162 loc) · 5.45 KB
/
sw.js
File metadata and controls
182 lines (162 loc) · 5.45 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
const CACHE_NAME = 'expenseflow-v1.0.0';
const urlsToCache = [
'/expensetracker.html',
'/expensetracker.css',
'/trackerscript.js',
'/manifest.json',
'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css'
];
// Install event - cache resources
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
.then(() => {
console.log('All resources cached successfully');
return self.skipWaiting(); // Activate new service worker immediately
})
.catch((error) => {
console.error('Failed to cache resources:', error);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => {
console.log('Service worker activated');
return self.clients.claim(); // Take control of all clients immediately
})
);
});
// Fetch event - serve from cache when offline
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// Return cached version or fetch from network
if (response) {
console.log('Serving from cache:', event.request.url);
return response;
}
// Clone the request because it's a stream that can only be consumed once
const fetchRequest = event.request.clone();
return fetch(fetchRequest).then((response) => {
// Check if we received a valid response
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response because it's a stream that can only be consumed once
const responseToCache = response.clone();
// Cache the fetched resource for future use
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
}).catch((error) => {
console.error('Fetch failed:', error);
// Return a custom offline page or response for HTML requests
if (event.request.destination === 'document') {
return caches.match('/expensetracker.html');
}
// For other resources, you might want to return a default response
throw error;
});
})
);
});
// Background sync for offline data synchronization
self.addEventListener('sync', (event) => {
if (event.tag === 'background-sync') {
console.log('Background sync triggered');
event.waitUntil(
// You could implement background data sync here
// For example, sync offline transactions when back online
syncOfflineTransactions()
);
}
});
// Push notifications (if needed in the future)
self.addEventListener('push', (event) => {
const options = {
body: event.data ? event.data.text() : 'New notification from ExpenseFlow',
icon: '/icons/icon-192x192.png',
badge: '/icons/badge-72x72.png',
vibrate: [200, 100, 200],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
},
actions: [
{
action: 'explore',
title: 'Open ExpenseFlow',
icon: '/icons/checkmark.png'
},
{
action: 'close',
title: 'Close notification',
icon: '/icons/xmark.png'
}
]
};
event.waitUntil(
self.registration.showNotification('ExpenseFlow', options)
);
});
// Notification click handler
self.addEventListener('notificationclick', (event) => {
event.notification.close();
if (event.action === 'explore') {
// Open the app
event.waitUntil(
clients.openWindow('/expensetracker.html')
);
} else if (event.action === 'close') {
// Just close the notification
return;
} else {
// Default action - open the app
event.waitUntil(
clients.openWindow('/expensetracker.html')
);
}
});
// Helper function for background sync
async function syncOfflineTransactions() {
try {
// This is where you would implement logic to sync offline data
// For example, get pending transactions from IndexedDB and send to server
console.log('Syncing offline transactions...');
// Example implementation:
// const pendingTransactions = await getPendingTransactionsFromDB();
// for (const transaction of pendingTransactions) {
// await syncTransaction(transaction);
// }
return Promise.resolve();
} catch (error) {
console.error('Error syncing offline transactions:', error);
throw error;
}
}
// Message handler for communication with main thread
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
console.log('Service Worker loaded successfully');