Files
study-bible-project/public/sw.js
T
nmemmert 96b6e840a3 Split App.jsx into pages + add service worker for offline support
App.jsx reduced from ~6,500 to ~3,900 lines by extracting each route into its
own page component (AdminPage, AuthPage, HomePage, ImportPage, ReaderPage,
SettingsPage, SetupPage, StudyPage). State and handlers are shared via
AppContext (React Context API); AppRouter handles routing at module level.

Also adds a network-first service worker (public/sw.js) that caches the last-
fetched Bible chapter data from bible.helloao.org and bolls.life so the app
stays usable when those APIs are temporarily unreachable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-08-12 08:06:48 -04:00

37 lines
1.1 KiB
JavaScript

const CACHE = 'bible-api-v1';
const CACHEABLE = ['https://bible.helloao.org', 'https://bolls.life'];
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then(keys => Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k))))
.then(() => self.clients.claim())
);
});
// Network-first, fall back to cache for external Bible API requests.
self.addEventListener('fetch', (event) => {
if (!CACHEABLE.some(origin => event.request.url.startsWith(origin))) return;
event.respondWith(
fetch(event.request)
.then(response => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE).then(cache => cache.put(event.request, clone));
}
return response;
})
.catch(() =>
caches.open(CACHE)
.then(cache => cache.match(event.request))
.then(cached => cached ?? new Response('{"error":"offline"}', {
status: 503,
headers: { 'Content-Type': 'application/json' },
}))
)
);
});