Bugfix:
- Update to @tabler/icons-svelte-runes for a more 'Svente 5' experience. - Adjust error handling in load functions, plain error thrown for handling in the error handler. - Fix the warning regarding a click handler attached to an <img> on the About page - Re-organise component directory - Remove unused font declarations from global.css Features: - Add service-worker caching of FilterData API response to allow for filtering to work fully offline -
This commit is contained in:
@@ -42,13 +42,53 @@ sw.addEventListener('fetch', (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Static Assets (Cache-First)
|
||||
// Static Assets (Cache-First)
|
||||
if (ASSETS.includes(url.pathname) || url.pathname.startsWith('/_app/')) {
|
||||
event.respondWith(caches.match(request).then((res) => res || fetch(request)));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Offline API Fallback (Network-First)
|
||||
// Cachable API Responses - stale-while-revalidate
|
||||
if (url.pathname === '/api/v3/locationFilter/data') {
|
||||
event.respondWith(
|
||||
caches.open('ob-dynamic-cache').then(async (cache) => {
|
||||
const cachedResponse = await cache.match(request);
|
||||
|
||||
// Ensure fallback response exists
|
||||
const makeOfflineFallback = () => {
|
||||
const errorObject: ApiEnvelope.Envelope = {
|
||||
e: {
|
||||
code: 'NETWORK_DISCONNECTED',
|
||||
msg: 'Cannot connect to the OwlBoard server'
|
||||
},
|
||||
t: Math.floor(Date.now() / 1000)
|
||||
};
|
||||
return new Response(JSON.stringify(errorObject), {
|
||||
status: 503,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
};
|
||||
|
||||
const networkFetch = fetch(request)
|
||||
.then((networkResponse) => {
|
||||
if (networkResponse.ok) {
|
||||
cache.put(request, networkResponse.clone());
|
||||
}
|
||||
return networkResponse;
|
||||
})
|
||||
.catch(() => {
|
||||
// If offline & cache empty, return fallback
|
||||
return makeOfflineFallback();
|
||||
});
|
||||
|
||||
// If cachedResponse is undefined, networkFetch safely resolves to a Response
|
||||
return cachedResponse || networkFetch;
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Offline API Fallback (Network-First)
|
||||
if (url.pathname.startsWith('/api') || url.hostname.includes('api')) {
|
||||
event.respondWith(
|
||||
fetch(request).catch(() => {
|
||||
@@ -68,13 +108,11 @@ sw.addEventListener('fetch', (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Navigation Fallback (The Offline 404 Catch-All)
|
||||
// This catches top-level page navigations and hard refreshes
|
||||
// Nav fallback - return fallback page, if offline and page requested
|
||||
if (request.mode === 'navigate' || request.headers.get('accept')?.includes('text/html')) {
|
||||
event.respondWith(
|
||||
fetch(request).catch(() => {
|
||||
// The network request failed entirely (offline).
|
||||
// Serve the cached root shell so SvelteKit can boot.
|
||||
// Serve svelte fallback page
|
||||
return caches.match('/') as Promise<Response>;
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user