feat: serve all precached data offline via DataCache fallback
- DataCache.fetchWithFallback: network-first with IDB write-through under
the same url: keys the preload writes; falls back to precached IDB copy
when offline — closes the preload(IDB) vs read(SW cache) gap that made
the Bible reader (and every other view) fail offline despite precaching
- Convert all 33 raw /data fetches across bible/service/menaion/prayer/
calendar/settings views; in-memory caches and notFound maps preserved
- fetchCommandText: switch to lowercase services/commands/ (already in
preload manifest); tolerate both TEXT/text node casings per migrate-data
- Remove stale duplicate static/data/en/services/Command/ (15 files)
- fetchLives: fix misc fallback to real lives/misc/{N}.json bundles
This commit is contained in:
1 parent
623c56a96f
commit
a005e386a6
22 files changed
+118
-198
No files matched your search
@@ -204,6 +204,30 @@ export class DataCache {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Network-first fetch with IndexedDB fallback.
|
||||
*
|
||||
* Online: fetches, parses and writes through to IDB under the same
|
||||
* `url:` keys the offline preload uses. Offline: serves the precached
|
||||
* IDB copy, or null when the file was never preloaded.
|
||||
*/
|
||||
static async fetchWithFallback<T = unknown>(url: string, type: 'json' | 'text' = 'json'): Promise<T | null> {
|
||||
try {
|
||||
const resp = await fetch(url);
|
||||
const ct = resp.headers.get('content-type');
|
||||
if (resp.ok && !(ct && ct.includes('text/html'))) {
|
||||
const data: unknown = type === 'text' ? await resp.text() : await resp.json();
|
||||
await DataCache.set(`url:${url}`, data);
|
||||
return data as T;
|
||||
}
|
||||
} catch { /* offline or invalid response — fall through to IDB */ }
|
||||
try {
|
||||
return ((await DataCache.get(`url:${url}`)) as T) ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache-first fetch: tries IndexedDB first, falls back to network.
|
||||
* If the data is fetched from network, it's cached for future use.
|
||||
|
||||
Reference in new issue
Block a user