-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
128 lines (122 loc) · 3.71 KB
/
Copy pathsw.js
File metadata and controls
128 lines (122 loc) · 3.71 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
// Level Up Service Worker v7.0 - Network-first for updates
const CACHE_NAME = 'levelup-v7';
const ASSETS_TO_CACHE = [
'/',
'/index.html',
'/scenarios.js',
'/translations.js',
'/scenario-translations-es.js',
'/levelup-theme.js',
'/manifest.json',
'/icon-192.png',
'/icon-512.png',
'/apple-touch-icon.png',
'/level-up.mp3',
'/intro.mp3',
'/arthur.jpg',
'/laurie.jpg',
'/aurora.jpg',
'/heghine.jpg',
'/roger.jpg',
'/Lily_Bot.jpg',
'/og-game.png',
'/ap.svg',
'/google.svg',
'/yahoo.svg',
'/bloomberg.svg'
];
// Install - cache assets and take over immediately
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(ASSETS_TO_CACHE))
.then(() => self.skipWaiting())
.catch(err => console.log('Cache failed:', err))
);
});
// Activate - delete ALL old caches and claim clients immediately
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim())
);
});
// Fetch - NETWORK FIRST for HTML, cache-first for assets
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
if (!event.request.url.startsWith('http')) return;
const url = new URL(event.request.url);
// Network-first for HTML (always get latest)
if (event.request.mode === 'navigate' || url.pathname.endsWith('.html') || url.pathname === '/') {
event.respondWith(
fetch(event.request)
.then(response => {
// Cache the fresh response
const responseClone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, responseClone));
return response;
})
.catch(() => {
// Offline - serve from cache
return caches.match(event.request) || caches.match('/index.html');
})
);
return;
}
// Network-first for JS too (always get latest game content + translations)
if (url.pathname.endsWith('.js')) {
event.respondWith(
fetch(event.request)
.then(response => {
const responseClone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, responseClone));
return response;
})
.catch(() => {
return caches.match(event.request);
})
);
return;
}
// Cache-first for other assets (images, audio, fonts)
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
if (cachedResponse) return cachedResponse;
return fetch(event.request).then(response => {
if (!response || response.status !== 200) return response;
const responseClone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, responseClone));
return response;
});
})
.catch(() => {
if (event.request.mode === 'navigate') {
return caches.match('/index.html');
}
})
);
});
// Push notifications
self.addEventListener('push', event => {
const options = {
body: event.data ? event.data.text() : 'Your streak is waiting!',
icon: '/icon-192.png',
badge: '/icon-192.png',
vibrate: [100, 50, 100]
};
event.waitUntil(self.registration.showNotification('Level Up 🔥', options));
});
// Notification clicks
self.addEventListener('notificationclick', event => {
event.notification.close();
event.waitUntil(clients.openWindow('/'));
});