Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline cache #2508

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Minor improvements
  • Loading branch information
corrideat committed Jan 15, 2025
commit 14f1fce12eb3d2002f088cacb2bfdc25b7ed441d
15 changes: 10 additions & 5 deletions frontend/controller/serviceworkers/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ if (
caches.open(CURRENT_CACHES.assets).then((cache) => {
return cache
.match(request)
.then((response) => {
if (response) {
.then((cachedResponse) => {
if (cachedResponse) {
// If we're offline, return the cached response, if it exists
if (navigator.onLine === false) {
return response
return cachedResponse
}
}

Expand All @@ -108,13 +109,17 @@ if (
await cache.put(request, response.clone()).catch(e => {
console.error('Error adding request to cache')
})
} else {
await cache.delete(request)
}

return response
}).catch(e => {
if (response) {
if (cachedResponse) {
console.warn('Error while fetching', request.url, e)
return response
// If there was a network error fetching, return the cached
// response, if it exists
return cachedResponse
}

throw e
Expand Down