2020-09-21 12:25:50 +00:00
|
|
|
/*
|
2024-02-02 18:27:50 +00:00
|
|
|
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
|
2020-09-21 12:25:50 +00:00
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
|
|
|
|
*/
|
|
|
|
|
2020-06-07 14:57:19 +00:00
|
|
|
'use strict'
|
|
|
|
|
2022-12-12 16:44:09 +00:00
|
|
|
import PackageJson from '../package.json'
|
2020-08-07 17:27:20 +00:00
|
|
|
|
2022-12-12 16:44:09 +00:00
|
|
|
const { version } = PackageJson
|
2020-06-07 14:57:19 +00:00
|
|
|
const rev = '#1'
|
|
|
|
|
2020-08-07 17:27:20 +00:00
|
|
|
const DYNAMIC_CACHE = `dynamic-cache-${version}` + rev
|
|
|
|
const STATIC_CACHE = `static-cache-${version}` + rev
|
2020-06-07 14:57:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-07-24 20:39:46 +00:00
|
|
|
return Promise.resolve()
|
2020-06-07 14:57:19 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
)
|
|
|
|
self.clients.claim()
|
|
|
|
})
|
|
|
|
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
|
|
event.respondWith(
|
|
|
|
fetch(event.request).then(response => {
|
|
|
|
const clonedResponse = response.clone()
|
2020-06-15 22:40:29 +00:00
|
|
|
if (event.request.method !== 'GET') {
|
|
|
|
return response
|
|
|
|
}
|
2020-06-07 14:57:19 +00:00
|
|
|
console.debug('Caching', { request: event.request })
|
|
|
|
return caches.open(DYNAMIC_CACHE).then(cache => {
|
|
|
|
return cache.put(event.request, clonedResponse)
|
|
|
|
}).then(() => {
|
|
|
|
return response
|
|
|
|
})
|
2020-06-15 22:40:29 +00:00
|
|
|
}).catch((e) => {
|
|
|
|
console.debug(e)
|
2020-06-07 14:57:19 +00:00
|
|
|
console.debug('Hitting cache', { request: event.request })
|
|
|
|
return caches.match(event.request)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|