prevent config from being cached but preserve offline capability

This commit is contained in:
schlagmichdoch 2023-11-09 00:25:40 +01:00
parent 9a2227c30f
commit 778d49e84b
2 changed files with 67 additions and 21 deletions

View file

@ -1,7 +1,7 @@
const cacheVersion = 'v1.9.4';
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
const forceFetch = false; // FOR DEVELOPMENT: Set to true to always update assets instead of using cached versions
const urlsToCache = [
const relativePathsToCache = [
'./',
'index.html',
'manifest.json',
@ -36,28 +36,44 @@ const urlsToCache = [
'lang/ru.json',
'lang/zh-CN.json'
];
const relativePathsNotToCache = [
'config'
]
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(cacheTitle)
.then(function(cache) {
return cache.addAll(urlsToCache).then(_ => {
console.log('All files cached.');
});
return cache
.addAll(relativePathsToCache)
.then(_ => {
console.log('All files cached.');
});
})
);
});
// fetch the resource from the network
const fromNetwork = (request, timeout) =>
new Promise((fulfill, reject) => {
new Promise((resolve, reject) => {
const timeoutId = setTimeout(reject, timeout);
fetch(request).then(response => {
clearTimeout(timeoutId);
fulfill(response);
update(request).then(_ => console.log("Cache successfully updated for", request.url));
}, reject);
fetch(request)
.then(response => {
clearTimeout(timeoutId);
resolve(response);
if (doNotCacheRequest(request)) return;
update(request)
.then(() => console.log("Cache successfully updated for", request.url))
.catch(reason => console.log("Cache could not be updated for", request.url, "Reason:", reason));
})
.catch(error => {
// Handle any errors that occurred during the fetch
console.error(`Could not fetch ${request.url}. Are you online?`);
reject(error);
});
});
// fetch the resource from the browser cache
@ -68,17 +84,32 @@ const fromCache = request =>
cache.match(request)
);
const rootUrl = location.href.substring(0, location.href.length - "service-worker.js".length);
const rootUrlLength = rootUrl.length;
const doNotCacheRequest = request => {
const requestRelativePath = request.url.substring(rootUrlLength);
return relativePathsNotToCache.indexOf(requestRelativePath) !== -1
};
// cache the current page to make it available for offline
const update = request =>
const update = request => new Promise((resolve, reject) => {
if (doNotCacheRequest(request)) {
reject("Url is specifically prevented from being cached in the serviceworker.");
return;
}
caches
.open(cacheTitle)
.then(cache =>
fetch(request)
.then(async response => {
await cache.put(request, response);
fetch(request, {cache: "no-store"})
.then(response => {
cache
.put(request, response)
.then(() => resolve());
})
.catch(_ => console.log(`Cache could not be updated. ${request.url}`))
.catch(reason => reject(reason))
);
});
// general strategy when making a request (eg if online try to fetch it
// from cache, if something fails fetch from network. Update cache everytime files are fetched.