fallback to cache if networking fails

This commit is contained in:
Jill 2023-08-20 04:02:43 +03:00
parent 8df30ebc65
commit 5fd335bbf5
Signed by: oat
GPG Key ID: 33489AA58A955108
1 changed files with 8 additions and 2 deletions

View File

@ -58,8 +58,14 @@ const CACHE_INVALID_PERIOD = 60 * 1000;
export async function getPosts() {
const timeSinceCache = Date.now() - postCache.refreshed;
if (timeSinceCache > CACHE_INVALID_PERIOD) {
postCache.posts = await getPostsUncached();
postCache.refreshed = Date.now();
try {
postCache.posts = await getPostsUncached();
postCache.refreshed = Date.now();
} catch(err) {
console.error('error while loading posts:');
console.error(err);
return postCache.posts;
}
}
return postCache.posts;