-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceWorker.js
129 lines (122 loc) · 4.23 KB
/
serviceWorker.js
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
129
const CACHE_NAME = `cache`;
const urlsToCache = [
'/',
'/static/css/theme.css',
'/static/css/style.css',
'/static/icons/favicon.png',
'/static/icons/icon.png',
'/serviceWorker.js',
'/dist/index.bundle.js',
'/dist/privacy_policy.bundle.js',
'/dist/runtime.bundle.js',
'/dist/361.bundle.js',
'/dist/index.html',
'/dist/baptism_booklet.html',
'/dist/privacy_policy.html',
];
// Install event: cache resources
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
console.log('Opened cache');
return Promise.all(
urlsToCache.map(url => {
return fetch(url).then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
}
console.log(`Caching ${url}`);
return cache.put(url, response.clone());
}).catch(error => {
console.error(`Error caching ${url}:`, error);
});
})
);
}).then(() => {
return self.skipWaiting();
}).catch(error => {
console.error('Failed to open cache:', error);
})
);
});
// Fetch event: serve from cache, then network
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
if (event.request.method === 'GET') {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
console.log('Serving from cache:', event.request.url);
return cachedResponse;
}
return fetch(event.request).then(fetchResponse => {
return caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, fetchResponse.clone());
return fetchResponse;
});
}).catch(error => {
console.error('Fetch failed:', error);
// Return a fallback page or message
return new Response("You are offline, and this resource is not cached.");
});
})
);
}
});
// Activate event: clear old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys => {
return Promise.all(
keys.map(key => {
if (key !== CACHE_NAME) {
console.log('Deleting old cache:', key);
return caches.delete(key);
}
})
);
}).then(() => {
console.log(CACHE_NAME + ' is now ready to handle fetches!');
return self.clients.claim();
})
);
});
// Message event: update cache when version changes
self.addEventListener('message', event => {
if (event.data.action === 'newUpdateCache') {
updateCache(true);
}
if (event.data.action === 'updateCache') {
updateCache(false);
}
});
function updateCache(giveResponse) {
caches.keys().then(function (names) {
return Promise.all(names.map(name => caches.delete(name)));
}).then(function () {
return caches.open(CACHE_NAME).then(cache => {
return Promise.all(
urlsToCache.map(url => {
return fetch(url, {
cache: 'no-store'
}).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return cache.put(url, response);
}).catch(error => {
console.error('Failed to fetch and cache:', url, error);
});
})
);
});
}).then(() => {
if (giveResponse) {
self.clients.matchAll().then(clients => {
clients.forEach(client => client.postMessage({
action: 'cacheUpdated'
}));
});
}
});
}