diff --git a/package.json b/package.json index 149b280a..a1babccf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "0.4.4", + "version": "0.4.5", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/app.html b/src/app.html index e4239698..999789f4 100644 --- a/src/app.html +++ b/src/app.html @@ -3,6 +3,7 @@ + + +// @ts-ignore +import { build, files } from '$service-worker' + +const timestamp = new Date().toLocaleString('en') + +const worker = self as unknown as ServiceWorkerGlobalScope +const FILES = `cache${timestamp}` + +// `build` is an array of all the files generated by the bundler, +// `files` is an array of everything in the `static` directory +const to_cache = build.concat(files) +const staticAssets = new Set(to_cache) + +worker.addEventListener('install', (event) => { + event.waitUntil( + caches + .open(FILES) + .then((cache) => cache.addAll(to_cache)) + .then(() => { + worker.skipWaiting() + }) + ) +}) + +worker.addEventListener('activate', (event) => { + event.waitUntil( + caches.keys().then(async (keys) => { + // delete old caches + for (const key of keys) { + if (key !== FILES) await caches.delete(key) + } + + worker.clients.claim() + }) + ) +}) + +/** + * Fetch the asset from the network and store it in the cache. + * Fall back to the cache if the user is offline. + */ +async function fetchAndCache(request: Request) { + const cache = await caches.open(`offline${timestamp}`) + + try { + const response = await fetch(request) + cache.put(request, response.clone()) + return response + } catch (err) { + const response = await cache.match(request) + if (response) return response + + throw err + } +} + +worker.addEventListener('fetch', (event) => { + if (event.request.method !== 'GET' || event.request.headers.has('range')) + return + + const url = new URL(event.request.url) + + // don't try to handle e.g. data: URIs + const isHttp = url.protocol.startsWith('http') + const isDevServerRequest = + url.hostname === self.location.hostname && url.port !== self.location.port + const isStaticAsset = + url.host === self.location.host && staticAssets.has(url.pathname) + const skipBecauseUncached = + event.request.cache === 'only-if-cached' && !isStaticAsset + + if (isHttp && !isDevServerRequest && !skipBecauseUncached) { + event.respondWith( + (async () => { + // always serve static files and bundler-generated assets from cache. + // if your application has other URLs with data that will never change, + // set this variable to true for them and they will only be fetched once. + const cachedAsset = isStaticAsset && (await caches.match(event.request)) + + return cachedAsset || fetchAndCache(event.request) + })() + ) + } +}) diff --git a/static/logo_512.png b/static/logo_512.png new file mode 100644 index 00000000..aef9b28e Binary files /dev/null and b/static/logo_512.png differ diff --git a/static/manifest.json b/static/manifest.json new file mode 100644 index 00000000..71faa3ab --- /dev/null +++ b/static/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "Photon", + "name": "Photon for Lemmy", + "start_url": "/", + "icons": [ + { + "src": "logo_512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "background_color": "#000000", + "display": "standalone", + "scope": "/", + "theme_color": "#000000", + "shortcuts": [ + { + "name": "Inbox", + "short_name": "Inbox", + "description": "View what's in your inbox.", + "url": "/inbox" + } + ], + "description": "A sleek client for Lemmy." +}