mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-20 15:06:15 -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
|
@ -27,23 +27,38 @@ class ServerConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
_getConfig() {
|
_getConfig() {
|
||||||
|
console.log("Loading config...")
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', 'config', true);
|
|
||||||
xhr.addEventListener("load", () => {
|
xhr.addEventListener("load", () => {
|
||||||
if (xhr.status === 200) {
|
if (xhr.status === 200) {
|
||||||
// Config received
|
// Config received
|
||||||
let config = JSON.parse(xhr.responseText);
|
let config = JSON.parse(xhr.responseText);
|
||||||
|
console.log("Config loaded:", config)
|
||||||
this._config = config;
|
this._config = config;
|
||||||
Events.fire('config', config);
|
Events.fire('config', config);
|
||||||
resolve()
|
resolve()
|
||||||
} else if (xhr.status !== 200) {
|
} else if (xhr.status < 200 || xhr.status >= 300) {
|
||||||
// Handle errors
|
retry(xhr);
|
||||||
console.error('Error:', xhr.status, xhr.statusText);
|
|
||||||
reject();
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
xhr.addEventListener("error", _ => {
|
||||||
|
retry(xhr);
|
||||||
});
|
});
|
||||||
xhr.send();
|
|
||||||
|
function retry(request) {
|
||||||
|
setTimeout(function () {
|
||||||
|
openAndSend(request)
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
function openAndSend() {
|
||||||
|
xhr.open('GET', 'config');
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
openAndSend(xhr);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const cacheVersion = 'v1.9.4';
|
const cacheVersion = 'v1.9.4';
|
||||||
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
|
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
|
||||||
const forceFetch = false; // FOR DEVELOPMENT: Set to true to always update assets instead of using cached versions
|
const forceFetch = false; // FOR DEVELOPMENT: Set to true to always update assets instead of using cached versions
|
||||||
const urlsToCache = [
|
const relativePathsToCache = [
|
||||||
'./',
|
'./',
|
||||||
'index.html',
|
'index.html',
|
||||||
'manifest.json',
|
'manifest.json',
|
||||||
|
@ -36,28 +36,44 @@ const urlsToCache = [
|
||||||
'lang/ru.json',
|
'lang/ru.json',
|
||||||
'lang/zh-CN.json'
|
'lang/zh-CN.json'
|
||||||
];
|
];
|
||||||
|
const relativePathsNotToCache = [
|
||||||
|
'config'
|
||||||
|
]
|
||||||
|
|
||||||
self.addEventListener('install', function(event) {
|
self.addEventListener('install', function(event) {
|
||||||
// Perform install steps
|
// Perform install steps
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(cacheTitle)
|
caches.open(cacheTitle)
|
||||||
.then(function(cache) {
|
.then(function(cache) {
|
||||||
return cache.addAll(urlsToCache).then(_ => {
|
return cache
|
||||||
console.log('All files cached.');
|
.addAll(relativePathsToCache)
|
||||||
});
|
.then(_ => {
|
||||||
|
console.log('All files cached.');
|
||||||
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// fetch the resource from the network
|
// fetch the resource from the network
|
||||||
const fromNetwork = (request, timeout) =>
|
const fromNetwork = (request, timeout) =>
|
||||||
new Promise((fulfill, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
const timeoutId = setTimeout(reject, timeout);
|
const timeoutId = setTimeout(reject, timeout);
|
||||||
fetch(request).then(response => {
|
fetch(request)
|
||||||
clearTimeout(timeoutId);
|
.then(response => {
|
||||||
fulfill(response);
|
clearTimeout(timeoutId);
|
||||||
update(request).then(_ => console.log("Cache successfully updated for", request.url));
|
resolve(response);
|
||||||
}, reject);
|
|
||||||
|
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
|
// fetch the resource from the browser cache
|
||||||
|
@ -68,17 +84,32 @@ const fromCache = request =>
|
||||||
cache.match(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
|
// 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
|
caches
|
||||||
.open(cacheTitle)
|
.open(cacheTitle)
|
||||||
.then(cache =>
|
.then(cache =>
|
||||||
fetch(request)
|
fetch(request, {cache: "no-store"})
|
||||||
.then(async response => {
|
.then(response => {
|
||||||
await cache.put(request, 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
|
// 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.
|
// 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