diff --git a/frontend/src/app.html b/frontend/src/app.html index f2516ae..abea469 100644 --- a/frontend/src/app.html +++ b/frontend/src/app.html @@ -4,6 +4,7 @@ + %sveltekit.head% diff --git a/frontend/src/service-worker/indes.ts b/frontend/src/service-worker/indes.ts new file mode 100644 index 0000000..5a4466b --- /dev/null +++ b/frontend/src/service-worker/indes.ts @@ -0,0 +1,60 @@ +/// + +import { build, files, version } from '$service-worker'; + +const CACHE = `cache-${version}`; + +const ASSETS = [ + ...build, // the app itself + ...files // everything in `static` +]; + +self.addEventListener('install', (event) => { + // Create a new cache and add all files to it + async function addFilesToCache() { + const cache = await caches.open(CACHE); + await cache.addAll(ASSETS); + } + event.waitUntil(addFilesToCache()); +}); + +self.addEventListener('activate', (event) => { + // Remove previous cached data from disk + async function deleteOldCaches() { + for (const key of await caches.keys()) { + if (key !== CACHE) await caches.delete(key); + } + } + event.waitUntil(deleteOldCaches()); +}); + +self.addEventListener('fetch', (event) => { + // ignore POST requests, etc + if (event.request.method !== 'GET') return; + + async function respond() { + const url = new URL(event.request.url); + const cache = await caches.open(CACHE); + + // `build`/`files` can always be served from the cache + if (ASSETS.includes(url.pathname)) { + return cache.match(url.pathname); + } + + // for everything else, try the network first, but + // fall back to the cache if we're offline + try { + const response = await fetch(event.request); + + if (response.status === 200) { + cache.put(event.request, response.clone()); + } + + return response; + } catch { + return cache.match(event.request); + } + } + + event.respondWith(respond()); +}); diff --git a/frontend/static/adventurelog.svg b/frontend/static/adventurelog.svg new file mode 100644 index 0000000..92667f2 --- /dev/null +++ b/frontend/static/adventurelog.svg @@ -0,0 +1,313 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/frontend/static/manifest.json b/frontend/static/manifest.json new file mode 100644 index 0000000..9f08d31 --- /dev/null +++ b/frontend/static/manifest.json @@ -0,0 +1,16 @@ +{ + "short_name": "AdventureLog", + "name": "AdventureLog", + "start_url": "/dashboard", + "icons": [ + { + "src": "adventurelog.svg", + "type": "image/svg+xml", + "sizes": "any" + } + ], + "background_color": "#2a323c", + "display": "standalone", + "scope": "/", + "description": "Self-hostable travel tracker and trip planner." +} \ No newline at end of file