-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
286 lines (251 loc) · 8.91 KB
/
Copy pathservice-worker.js
File metadata and controls
286 lines (251 loc) · 8.91 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// DigitalForge Pro - Service Worker
// Version 3.1.0
const CACHE_NAME = 'digitalforge-pro-v3.2.0';
const CACHE_VERSION = '3.2.0';
// Archivos principales que siempre deben estar en caché
const CORE_CACHE = [
'/',
'/index.html',
'/assets/styles/main.css',
'/js/core/app.js',
'/js/core/config.js',
'/js/core/utils.js',
'/js/features/calculators.js',
'/js/features/tools.js',
'/js/features/circuit-designer.js',
'/js/ai/generator.js',
'/js/ai/assistant.js',
'/js/cloud/puter-service.js',
'/pages/formulas-list.html',
'/pages/hardware.html',
'/pages/advanced.html'
];
// Recursos externos con estrategia de respaldo
const EXTERNAL_CACHE = [
'https://cdn.tailwindcss.com',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css',
'https://js.puter.com/v2/'
];
const urlsToCache = [...CORE_CACHE, ...EXTERNAL_CACHE];
// Evento de instalación - cachear recursos
self.addEventListener('install', event => {
console.log('🔧 Service Worker: Installing v' + CACHE_VERSION);
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('📦 Service Worker: Caching core files');
// Cachear archivos principales primero (crítico)
return cache.addAll(CORE_CACHE)
.then(() => {
console.log('📦 Service Worker: Caching external resources');
// Cachear recursos externos (no crítico, puede fallar)
return Promise.allSettled(
EXTERNAL_CACHE.map(url =>
cache.add(url).catch(err => {
console.warn('⚠️ Failed to cache:', url, err);
return null;
})
)
);
});
})
.then(() => {
console.log('✅ Service Worker: Installation complete');
return self.skipWaiting();
})
.catch(error => {
console.error('❌ Service Worker: Installation failed', error);
throw error;
})
);
});
// Evento de activación - limpiar cachés antiguos
self.addEventListener('activate', event => {
console.log('🚀 Service Worker: Activating...');
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
console.log('🗑️ Service Worker: Deleting old cache', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => {
console.log('✅ Service Worker: Activation complete');
return self.clients.claim();
})
);
});
// Evento de fetch - Network first for HTML, Cache first for assets
self.addEventListener('fetch', event => {
// Saltar peticiones que no sean GET
if (event.request.method !== 'GET') {
return;
}
// Saltar chrome-extension and other non-http requests
if (!event.request.url.startsWith('http')) {
return;
}
const url = new URL(event.request.url);
// Estrategia network-first for HTML documents
if (event.request.destination === 'document') {
event.respondWith(
fetch(event.request)
.then(response => {
// Cachear la nueva versión
const responseToCache = response.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(() => {
// Respaldo al caché if network fails
return caches.match(event.request)
.then(response => response || caches.match('/index.html'));
})
);
return;
}
// Estrategia cache-first for assets (CSS, JS, images, fonts)
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
console.log('📋 Cache hit:', url.pathname);
// Actualizar caché en segundo plano
fetch(event.request).then(networkResponse => {
if (networkResponse && networkResponse.status === 200) {
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, networkResponse);
});
}
}).catch(() => {});
return response;
}
// No está en caché, obtener de la red
console.log('🌐 Network fetch:', url.pathname);
return fetch(event.request).then(response => {
// Cachear respuestas válidas
if (response && response.status === 200) {
const responseToCache = response.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache);
});
}
return response;
});
})
.catch(error => {
console.error('❌ Fetch failed:', error);
// Retornar respaldo offline for navigation
if (event.request.destination === 'document') {
return caches.match('/index.html');
}
throw error;
})
);
});
// Sincronización en segundo plano for saving data when online
self.addEventListener('sync', event => {
console.log('🔄 Service Worker: Background sync', event.tag);
if (event.tag === 'save-hdl-code') {
event.waitUntil(
// Handle background sync for saving HDL code
handleBackgroundSync()
);
}
});
// Notificaciones push (para uso futuro)
self.addEventListener('push', event => {
console.log('📢 Service Worker: Push notification received');
const options = {
body: event.data ? event.data.text() : 'Nueva actualización disponible',
icon: 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"%3E%3Ctext y=".9em" font-size="90"%3E🔬%3C/text%3E%3C/svg%3E',
badge: 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"%3E%3Ctext y=".9em" font-size="90"%3E🔬%3C/text%3E%3C/svg%3E',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
},
actions: [
{
action: 'explore',
title: 'Abrir App',
icon: 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"%3E%3Ctext y=".9em" font-size="90"%3E🚀%3C/text%3E%3C/svg%3E'
},
{
action: 'close',
title: 'Cerrar',
icon: 'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"%3E%3Ctext y=".9em" font-size="90"%3E❌%3C/text%3E%3C/svg%3E'
}
]
};
event.waitUntil(
self.registration.showNotification('Digital Logic Studio Pro', options)
);
});
// Manejador de click en notificación
self.addEventListener('notificationclick', event => {
console.log('🔔 Service Worker: Notification clicked');
event.notification.close();
if (event.action === 'explore') {
event.waitUntil(
clients.openWindow('/')
);
}
});
// Manejador de mensajes para comunicación con el hilo principal
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 });
}
});
// Función auxiliar for background sync
async function handleBackgroundSync() {
try {
// Obtener datos pendientes from IndexedDB or localStorage
const pendingData = await getPendingData();
if (pendingData && pendingData.length > 0) {
// Procesar cada elemento pendiente
for (const item of pendingData) {
await processPendingItem(item);
}
// Limpiar datos pendientes after successful sync
await clearPendingData();
console.log('✅ Service Worker: Background sync completed');
}
} catch (error) {
console.error('❌ Service Worker: Background sync failed', error);
throw error;
}
}
// Función auxiliars para gestión de datos
async function getPendingData() {
// La implementación dependería on your storage strategy
return [];
}
async function processPendingItem(item) {
// Procesar elemento de sincronización individual
console.log('Processing sync item:', item);
}
async function clearPendingData() {
// Limpiar datos de sincronización procesados
console.log('Clearing pending sync data');
}
// Manejador de errores
self.addEventListener('error', event => {
console.error('❌ Service Worker: Error occurred', event.error);
});
// Manejador de rechazos no manejados
self.addEventListener('unhandledrejection', event => {
console.error('❌ Service Worker: Unhandled promise rejection', event.reason);
event.preventDefault();
});
console.log('🚀 DigitalForge Pro Service Worker v' + CACHE_VERSION + ' loaded');