-
Notifications
You must be signed in to change notification settings - Fork 0
/
emergency.js
66 lines (58 loc) · 2.07 KB
/
emergency.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
// Redirect from the outdated domain
if (window.location.host === 'cowswap.exchange') {
window.location.href = 'https://swap.cow.fi'
}
if (window.location.host === 'barn.cowswap.exchange') {
window.location.href = 'https://barn.cow.fi'
}
/**
* In case of problems with the service worker cache we can urgently reset the cache.
* Just set resetCacheInCaseOfEmergency to true and release a new version
*/
const emergencyConfigUrl = 'https://raw.githubusercontent.com/cowprotocol/cowswap/configuration/config/emergency.json'
async function deleteAllCaches() {
return caches.keys().then((cacheNames) => {
;(cacheNames || []).map((cacheName) => {
console.log('[Service worker] Delete cache', cacheName)
// Delete old caches
// https://developers.google.com/web/ilt/pwa/caching-files-with-service-worker#removing_outdated_caches
return caches.delete(cacheName)
})
})
}
function unregisterAllWorkers() {
return navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (const registration of registrations) {
registration.unregister()
}
})
}
function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then((registration) => {
registration.unregister()
})
.catch((error) => {
console.error(error.message)
})
}
}
;(function () {
fetch(emergencyConfigUrl + '?cacheReset=' + Date.now())
.then((res) => res.json())
.then(({ resetCacheInCaseOfEmergency }) => {
if (resetCacheInCaseOfEmergency && 'serviceWorker' in navigator) {
console.log('[Service worker] Unregister worker...')
unregister()
console.log('[Service worker] Deleting all caches...')
deleteAllCaches()
.then(() => console.log('[worker] All caches have been deleted'))
.catch(console.error)
console.log('[Service worker] Unregistering all workers...')
unregisterAllWorkers()
.then(() => console.log('[Service worker] All workers have been unregistered'))
.catch(console.error)
}
})
})()