-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
63 lines (60 loc) · 2.06 KB
/
sw.js
File metadata and controls
63 lines (60 loc) · 2.06 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
const CACHE_NAME = 'metro-surge-v3';
const URLS_TO_CACHE = [
'./',
'./index.html',
'./manifest.json',
'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css',
'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js',
'https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2'
];
// 1. INSTALL: Cache Core Files immediately
self.addEventListener('install', event => {
self.skipWaiting(); // FORCE ACTIVATION (Don't wait for tabs to close)
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Opened cache');
return cache.addAll(URLS_TO_CACHE);
})
);
});
// 2. ACTIVATE: Delete old caches instantly
self.addEventListener('activate', event => {
event.waitUntil(clients.claim()); // Take control of all pages immediately
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
// 3. FETCH: The "Network First" Strategy
self.addEventListener('fetch', event => {
// If it's the main page (HTML), try Network first, then Cache
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request)
.then(response => {
return caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, response.clone()); // Update cache with new version
return response;
});
})
.catch(() => {
// If offline, return cached version
return caches.match(event.request);
})
);
} else {
// For images/scripts, keep using Cache First (for speed)
event.respondWith(
caches.match(event.request)
.then(response => response || fetch(event.request))
);
}
});