-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathservice-worker.js
62 lines (55 loc) · 1.46 KB
/
service-worker.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
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
'use strict'
import PackageJson from '../package.json'
const { version } = PackageJson
const rev = '#1'
const DYNAMIC_CACHE = `dynamic-cache-${version}` + rev
const STATIC_CACHE = `static-cache-${version}` + rev
const FILES_TO_CACHE = [
'./',
]
self.addEventListener('install', (evt) => {
evt.waitUntil(
caches.open(STATIC_CACHE).then((cache) => {
return cache.addAll(FILES_TO_CACHE)
}),
)
self.skipWaiting()
})
self.addEventListener('activate', (evt) => {
evt.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== STATIC_CACHE && key !== DYNAMIC_CACHE) {
return caches.delete(key)
}
return Promise.resolve()
}))
}),
)
self.clients.claim()
})
self.addEventListener('fetch', (event) => {
event.respondWith(
fetch(event.request).then(response => {
const clonedResponse = response.clone()
if (event.request.method !== 'GET') {
return response
}
console.debug('Caching', { request: event.request })
return caches.open(DYNAMIC_CACHE).then(cache => {
return cache.put(event.request, clonedResponse)
}).then(() => {
return response
})
}).catch((e) => {
console.debug(e)
console.debug('Hitting cache', { request: event.request })
return caches.match(event.request)
}),
)
})