-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
71 lines (66 loc) · 1.99 KB
/
sw.js
File metadata and controls
71 lines (66 loc) · 1.99 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
// sw.js - Versão "Bunker" (Totalmente Offline)
const CACHE_NAME = 'participa-df-offline-v3';
// Lista exata dos arquivos que você tem na pasta
const urlsToCache = [
'./',
'./index.html',
'./style.css',
'./app.js',
'./manifest.json',
'./libs/leaflet.css',
'./libs/leaflet.js',
// Novas imagens locais:
'./libs/images/marker-icon.png',
'./libs/images/marker-icon-2x.png',
'./libs/images/marker-shadow.png',
'./libs/images/marker-icon-red.png',
'./libs/images/marker-icon-orange.png'
];
// 1. Instalação: Baixa e guarda tudo
self.addEventListener('install', event => {
self.skipWaiting();
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('📦 [SW] Cacheando arquivos locais...');
return cache.addAll(urlsToCache);
})
);
});
// 2. Ativação: Limpa caches antigos
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
// 3. Interceptação (A Mágica Offline)
self.addEventListener('fetch', event => {
// Se for requisição para a API (Python), tenta rede primeiro, se falhar, deixa passar (o app.js trata o erro)
if (event.request.url.includes('/api/')) {
return;
}
// Para arquivos estáticos (HTML, CSS, JS, Mapas), usa CACHE FIRST
event.respondWith(
caches.match(event.request)
.then(response => {
// Se achou no cache, retorna rápido!
if (response) {
return response;
}
// Se não achou (ex: tiles do mapa), tenta buscar na internet
return fetch(event.request).catch(() => {
// Se falhar e for imagem (tile do mapa), retorna nada ou placeholder
// Isso evita o erro crítico no console
return new Response('', { status: 408, statusText: 'Offline' });
});
})
);
});