fix: properly detect offline mode when using cached data
This commit is contained in:
13
src/App.js
13
src/App.js
@@ -41,28 +41,21 @@ function App() {
|
|||||||
setRefreshing(forceRefresh);
|
setRefreshing(forceRefresh);
|
||||||
|
|
||||||
const result = forceRefresh ? await refreshData() : await fetchData();
|
const result = forceRefresh ? await refreshData() : await fetchData();
|
||||||
setData(result);
|
setData(result.data);
|
||||||
setError(null);
|
setError(null);
|
||||||
setIsOffline(false);
|
setIsOffline(result.fromCache);
|
||||||
|
|
||||||
// Update cache info
|
// Update cache info
|
||||||
const status = getCacheStatus();
|
const status = getCacheStatus();
|
||||||
setCacheInfo(status);
|
setCacheInfo(status);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Check if we got data from cache despite the error
|
|
||||||
const status = getCacheStatus();
|
|
||||||
if (status.available && data.length > 0) {
|
|
||||||
setIsOffline(true);
|
|
||||||
setCacheInfo(status);
|
|
||||||
} else {
|
|
||||||
setError(err.message);
|
setError(err.message);
|
||||||
}
|
|
||||||
console.error(err);
|
console.error(err);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
}
|
}
|
||||||
}, [data.length]);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadData();
|
loadData();
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ export async function fetchData() {
|
|||||||
const cached = loadFromCache();
|
const cached = loadFromCache();
|
||||||
if (cached) {
|
if (cached) {
|
||||||
console.warn('NocoDB not configured, using cached data');
|
console.warn('NocoDB not configured, using cached data');
|
||||||
return cached.data;
|
return { data: cached.data, fromCache: true, cacheTimestamp: cached.timestamp };
|
||||||
}
|
}
|
||||||
throw new Error('NocoDB not configured and no cached data available. Set REACT_APP_NOCODB_URL and REACT_APP_NOCODB_TOKEN in .env.local');
|
throw new Error('NocoDB not configured and no cached data available. Set REACT_APP_NOCODB_URL and REACT_APP_NOCODB_TOKEN in .env.local');
|
||||||
}
|
}
|
||||||
@@ -191,7 +191,7 @@ export async function fetchData() {
|
|||||||
// Save to cache on success
|
// Save to cache on success
|
||||||
saveToCache(data);
|
saveToCache(data);
|
||||||
|
|
||||||
return data;
|
return { data, fromCache: false };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('NocoDB fetch failed:', err.message);
|
console.error('NocoDB fetch failed:', err.message);
|
||||||
|
|
||||||
@@ -199,7 +199,7 @@ export async function fetchData() {
|
|||||||
const cached = loadFromCache();
|
const cached = loadFromCache();
|
||||||
if (cached) {
|
if (cached) {
|
||||||
console.warn(`Using cached data from ${new Date(cached.timestamp).toLocaleString()} (offline mode)`);
|
console.warn(`Using cached data from ${new Date(cached.timestamp).toLocaleString()} (offline mode)`);
|
||||||
return cached.data;
|
return { data: cached.data, fromCache: true, cacheTimestamp: cached.timestamp };
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Database unavailable and no cached data: ${err.message}`);
|
throw new Error(`Database unavailable and no cached data: ${err.message}`);
|
||||||
@@ -214,7 +214,7 @@ export async function refreshData() {
|
|||||||
|
|
||||||
const data = await fetchFromNocoDB();
|
const data = await fetchFromNocoDB();
|
||||||
saveToCache(data);
|
saveToCache(data);
|
||||||
return data;
|
return { data, fromCache: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|||||||
Reference in New Issue
Block a user