-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
209 lines (186 loc) · 5.82 KB
/
sw.js
File metadata and controls
209 lines (186 loc) · 5.82 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
const CACHE_NAME = 'easybin-v1.1.0';
const STATIC_CACHE = 'easybin-static-v1.1';
const DYNAMIC_CACHE = 'easybin-dynamic-v1.1';
const IMAGE_CACHE = 'easybin-images-v1';
const STATIC_ASSETS = [
'/',
'/index.html',
'/src/css/styles.css',
'/src/js/app.js',
'/src/js/translations.js',
'/src/js/binStyles.js',
'/src/js/analytics.js',
'/src/js/error-monitor.js',
'/src/js/security.js',
'/src/js/modern-features.js',
'/src/js/bento-integration.js',
'/src/js/ai-provider-adapters.js',
'/src/js/ai-vision-client.js',
'/src/js/api-key-manager.js',
'/src/js/image-compression.js',
'/manifest.json'
];
// Install event - cache static assets
self.addEventListener('install', event => {
console.log('Service Worker installing...');
event.waitUntil(
caches.open(STATIC_CACHE)
.then(cache => {
console.log('Caching static assets');
return cache.addAll(STATIC_ASSETS);
})
.catch(err => console.log('Cache install failed:', err))
);
self.skipWaiting();
});
// 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
.filter(cacheName => cacheName !== STATIC_CACHE && cacheName !== DYNAMIC_CACHE)
.map(cacheName => caches.delete(cacheName))
);
})
);
self.clients.claim();
});
// Fetch event - serve from cache with network fallback
self.addEventListener('fetch', event => {
// Skip non-GET requests
if (event.request.method !== 'GET') return;
// Skip chrome-extension and browser-specific requests
if (event.request.url.startsWith('chrome-extension://') ||
event.request.url.startsWith('moz-extension://')) {
return;
}
event.respondWith(
caches.match(event.request)
.then(response => {
// Return cached version if available
if (response) {
// For static assets, return cache first
if (STATIC_ASSETS.some(asset => event.request.url.includes(asset))) {
return response;
}
// For dynamic content, update cache in background
fetch(event.request)
.then(fetchResponse => {
if (fetchResponse && fetchResponse.status === 200) {
const responseClone = fetchResponse.clone();
caches.open(DYNAMIC_CACHE)
.then(cache => cache.put(event.request, responseClone));
}
})
.catch(() => { }); // Ignore network errors for background updates
return response;
}
// Not in cache, fetch from network
return fetch(event.request)
.then(fetchResponse => {
// Don't cache failed requests
if (!fetchResponse || fetchResponse.status !== 200 || fetchResponse.type !== 'basic') {
return fetchResponse;
}
// Cache successful responses
const responseClone = fetchResponse.clone();
caches.open(DYNAMIC_CACHE)
.then(cache => cache.put(event.request, responseClone));
return fetchResponse;
})
.catch(err => {
console.log('Fetch failed for:', event.request.url, err);
// Return offline page for navigation requests
if (event.request.mode === 'navigate') {
return caches.match('/offline.html');
}
// Return a basic error response for other requests
return new Response('Offline - content not available', {
status: 503,
statusText: 'Service Unavailable'
});
});
})
);
});
// Background sync for failed AI requests
self.addEventListener('sync', event => {
if (event.tag === 'background-sync-ai-request') {
event.waitUntil(
// Retry failed AI requests when connection is restored
handleBackgroundSync()
);
}
});
// Handle background sync
async function handleBackgroundSync() {
try {
// Get pending requests from IndexedDB or localStorage
const pendingRequests = await getPendingRequests();
for (const request of pendingRequests) {
try {
// Retry the AI request
const response = await fetch(request.url, request.options);
if (response.ok) {
// Remove from pending requests
await removePendingRequest(request.id);
}
} catch (error) {
console.log('Background sync retry failed:', error);
}
}
} catch (error) {
console.log('Background sync failed:', error);
}
}
// Push notification support
self.addEventListener('push', event => {
if (event.data) {
const data = event.data.json();
const options = {
body: data.body,
icon: '/icons/icon-192.png',
badge: '/icons/icon-192.png',
vibrate: [100, 50, 100],
data: data.data,
actions: [
{
action: 'view',
title: 'View',
icon: '/icons/icon-192.png'
},
{
action: 'close',
title: 'Close'
}
]
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
}
});
// Handle notification clicks
self.addEventListener('notificationclick', event => {
event.notification.close();
if (event.action === 'view') {
event.waitUntil(
clients.openWindow('/')
);
}
});
// Placeholder functions for IndexedDB operations
async function getPendingRequests() {
// Implementation would use IndexedDB to store/retrieve pending requests
return [];
}
async function removePendingRequest(id) {
// Implementation would remove request from IndexedDB
return true;
}
async function handleBackgroundSync() {
// Implementation for retrying failed AI requests
console.log('Background sync triggered - retrying failed requests');
}