Commit 6aae39c5 authored by Nicolas Frandeboeuf's avatar Nicolas Frandeboeuf
Browse files

Alert user that api response comes from cache

parent b1fa6121
Pipeline #315605 passed with stages
in 5 minutes and 42 seconds
......@@ -681,6 +681,14 @@ export function init() {
// The UI must be computed at the end.
state.ui = Ui.initialState(get.boolSetting(state, 'demo-mode'));
if (world.isFromCache) {
window.alert(
'Your internet connection seems down. The results you see were loaded from ' +
'the last successful request. Please be aware that any further ' +
'modification might fail until the network is up again.'
);
}
return new Promise(accept => {
accept(state);
});
......
......@@ -11,15 +11,32 @@ function networkFirst(request) {
.fetch(request)
.then(response => {
if (response) {
const responseCache = response.clone();
global.caches.open(UPTODATE_CACHE_NAME).then(cache => {
cache.put(request, responseCache);
});
const newResponse = response.clone();
let cacheResponse = res => {
global.caches.open(UPTODATE_CACHE_NAME).then(cache => {
cache.put(request, res);
});
};
if (newResponse.ok && newResponse.headers.get('Content-Type').includes('/json')) {
newResponse.json().then(body => {
cacheResponse(
new Response(JSON.stringify({ isFromCache: true, ...body }), {
status: newResponse.status,
statusText: newResponse.statusText,
headers: newResponse.headers
})
);
});
} else {
cacheResponse(newResponse);
}
}
return response;
})
.catch(() => {
.catch((err) => {
console.error(err);
return global.caches.open(UPTODATE_CACHE_NAME).then(cache => {
return cache.match(request).then(response => {
return response;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment