Repository navigation
Expand file tree
/
Copy pathservice-worker.js
More file actions
112 lines (103 loc) · 3.34 KB
/
Copy pathservice-worker.js
File metadata and controls
112 lines (103 loc) · 3.34 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
// COMU Hub PWA — Service Worker
// Estratégia: Network First com fallback para cache
// Isso garante que os dados sempre estejam atualizados quando online,
// mas funciona offline com a última versão cacheada.
const CACHE_NAME = 'comu-hub-v1';
const DASHBOARD_FILES = [
'/',
'/comu-hub.html',
'/comu-dashboard-executivo.html',
'/comu-dashboard-tap.html',
'/comu-dashboard-cap.html',
'/comu-dashboard-cap-gestao.html',
'/comu-dashboard-gestao-ams.html',
'/comu-dashboard-comercial.html',
'/comu-dashboard-funil.html',
'/comu-dashboard-funnel.html',
'/comu-dashboard-automacao.html',
'/comu-dashboard-amostras.html',
'/comu-dashboard-feed.html',
'/manifest.json',
'/icons/icon-192.svg',
'/icons/icon-512.svg',
'/icons/icon-maskable-512.svg'
];
const EXTERNAL_ASSETS = [
'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap',
'https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.js'
];
// Install: pre-cache all dashboard files
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[SW] Pre-caching dashboard files');
return cache.addAll([...DASHBOARD_FILES, ...EXTERNAL_ASSETS]);
}).catch(err => {
console.warn('[SW] Pre-cache partial failure (non-critical):', err);
})
);
self.skipWaiting();
});
// Activate: clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
);
})
);
self.clients.claim();
});
// Fetch: Network First strategy for HTML (always fresh data),
// Cache First for static assets (fonts, chart.js, icons)
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Skip non-GET requests
if (event.request.method !== 'GET') return;
// For HTML dashboard files: Network First (para dados sempre atualizados)
if (url.pathname.endsWith('.html') || url.pathname === '/') {
event.respondWith(
fetch(event.request)
.then((response) => {
// Atualiza o cache com a versão mais recente
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
return response;
})
.catch(() => {
// Offline: usa a versão cacheada
return caches.match(event.request);
})
);
return;
}
// For static assets: Cache First (fonts, JS libs, icons)
event.respondWith(
caches.match(event.request).then((cached) => {
if (cached) return cached;
return fetch(event.request).then((response) => {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
return response;
});
})
);
});
// Listen for messages to force update
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') {
self.skipWaiting();
}
if (event.data === 'REFRESH_CACHE') {
caches.open(CACHE_NAME).then((cache) => {
DASHBOARD_FILES.forEach((file) => {
fetch(file).then((response) => {
if (response.ok) cache.put(file, response);
}).catch(() => {});
});
});
}
});