-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsw.js
More file actions
91 lines (85 loc) · 2.57 KB
/
Copy pathsw.js
File metadata and controls
91 lines (85 loc) · 2.57 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
const CACHE_VERSION = 'v12';
const CACHE_NAME = `safe-id-${CACHE_VERSION}`;
const urlsToCache = [
'/',
'/index.html',
'/index-en.html',
'/manifest.json',
'/logo.png',
'/android-chrome-192x192.png',
'/android-chrome-512x512.png',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap'
];
// Force clear old caches during activation
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
// Force fetch new resources
return cache.addAll(urlsToCache.map(url => new Request(url, {cache: 'reload'})));
})
.then(() => {
// Force this service worker to become the active service worker
return self.skipWaiting();
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Always try network first for HTML files
if (event.request.mode === 'navigate' ||
event.request.headers.get('accept').includes('text/html')) {
return fetch(event.request)
.then(response => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(() => response); // Fallback to cache if network fails
}
// Cache first for other resources
if (response) {
return response;
}
return fetch(event.request)
.then(response => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
});
return response;
});
})
);
});
// Handle client side cache clearing
self.addEventListener('message', event => {
if (event.data === 'skipWaiting') {
self.skipWaiting();
}
});