-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
92 lines (84 loc) · 2.99 KB
/
Copy pathsw.js
File metadata and controls
92 lines (84 loc) · 2.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const CACHE_NAME = 'zepo-v202';
// Only cache external CDN scripts. Don't pre-cache HTML/manifest — let them be network-first
// so the user always gets the latest version when online.
const ASSETS = [
'https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/dist/umd/supabase.min.js',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=JetBrains+Mono:wght@500&display=swap'
];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE_NAME).then(c => c.addAll(ASSETS).catch(() => {})));
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil((async () => {
// Delete ALL old caches (not just non-matching) to be safe
const keys = await caches.keys();
await Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)));
await self.clients.claim();
})());
});
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET') return;
const url = new URL(e.request.url);
if (url.origin === 'https://vchaxqisbypwwtyjjnjr.supabase.co') return;
if (url.hostname.endsWith('.google.com') || url.hostname.endsWith('.googleapis.com')) return;
if (url.hostname === 'api.coingecko.com') return;
// Network-first for HTML and SW-related files (always get fresh)
const isHTML = e.request.mode === 'navigate' || url.pathname.endsWith('.html') || url.pathname === '/pwa/' || url.pathname === '/pwa/index.html';
const isManifest = url.pathname.endsWith('manifest.json');
if (isHTML || isManifest) {
e.respondWith(
fetch(e.request)
.then(resp => {
const clone = resp.clone();
caches.open(CACHE_NAME).then(c => c.put(e.request, clone));
return resp;
})
.catch(() => caches.match(e.request))
);
return;
}
// Cache-first for everything else (icons, CDN scripts, fonts)
e.respondWith(
caches.match(e.request).then(cached => cached || fetch(e.request).then(resp => {
const clone = resp.clone();
caches.open(CACHE_NAME).then(c => c.put(e.request, clone));
return resp;
}))
);
});
self.addEventListener('push', e => {
let data = {
title: 'Zepo',
body: '¿Registraste todos tus gastos de hoy?',
icon: '/pwa/icons/icon-192.png',
badge: '/pwa/icons/favicon-32.png',
url: '/pwa/',
};
if (e.data) {
try { Object.assign(data, e.data.json()); } catch {}
}
e.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: data.icon,
badge: data.badge,
data: { url: data.url },
vibrate: [200, 100, 200],
tag: data.tag || 'zepo-push',
renotify: true,
})
);
});
self.addEventListener('notificationclick', e => {
e.notification.close();
const url = e.notification.data?.url || '/pwa/';
e.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(list => {
for (const c of list) {
if (c.url.includes('/pwa/') && 'focus' in c) return c.focus();
}
return clients.openWindow(url);
})
);
});