mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-23 16:26:17 -04:00
prevent config from being cached but preserve offline capability
This commit is contained in:
parent
9a2227c30f
commit
778d49e84b
2 changed files with 67 additions and 21 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue