mirror of
https://github.com/TheAnachronism/docspell.git
synced 2024-11-13 02:31:10 +00:00
f29353adb3
Avoid to have users clear their browser caches
38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
// use a cacheName for cache versioning
|
|
var cacheName = 'v{{ flags.uiVersion }}:static';
|
|
|
|
// during the install phase you usually want to cache static assets
|
|
self.addEventListener('install', function(e) {
|
|
// once the SW is installed, go ahead and fetch the resources to make this work offline
|
|
e.waitUntil(
|
|
caches.open(cacheName).then(function(cache) {
|
|
return cache.addAll([
|
|
{{# cssUrls }}
|
|
'{{.}}',
|
|
{{/ cssUrls }}
|
|
{{# jsUrls }}
|
|
'{{.}}',
|
|
{{/ jsUrls}}
|
|
'{{appExtraJs}}'
|
|
]).then(function() {
|
|
self.skipWaiting();
|
|
});
|
|
})
|
|
);
|
|
});
|
|
|
|
// when the browser fetches a url
|
|
self.addEventListener('fetch', function(event) {
|
|
// either respond with the cached object or go ahead and fetch the actual url
|
|
event.respondWith(
|
|
caches.match(event.request).then(function(response) {
|
|
if (response) {
|
|
// retrieve from cache
|
|
return response;
|
|
}
|
|
// fetch as normal
|
|
return fetch(event.request);
|
|
})
|
|
);
|
|
});
|