From 4426cf6d2edeee0b12d05c5964fa16b542e0612e Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Sep 2023 20:59:08 -0700 Subject: feat: pwa --- src/app.html | 1 + src/service-worker.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++ static/favicon-128x128.png | Bin 0 -> 26590 bytes static/favicon-144x144.png | Bin 0 -> 30783 bytes static/favicon-16x16.png | Bin 0 -> 6878 bytes static/favicon-192x192.png | Bin 0 -> 45372 bytes static/favicon-36x36.png | Bin 0 -> 8790 bytes static/favicon-48x48.png | Bin 0 -> 10453 bytes static/favicon-512x512.png | Bin 0 -> 221516 bytes static/favicon-72x72.png | Bin 0 -> 14464 bytes static/favicon-original.png | Bin 0 -> 202862 bytes static/manifest.json | 50 +++++++++++++++++++++++++++ 12 files changed, 131 insertions(+) create mode 100644 src/service-worker.ts create mode 100644 static/favicon-128x128.png create mode 100644 static/favicon-144x144.png create mode 100644 static/favicon-16x16.png create mode 100644 static/favicon-192x192.png create mode 100644 static/favicon-36x36.png create mode 100644 static/favicon-48x48.png create mode 100644 static/favicon-512x512.png create mode 100644 static/favicon-72x72.png create mode 100644 static/favicon-original.png create mode 100644 static/manifest.json diff --git a/src/app.html b/src/app.html index fb37d49a..627beba8 100644 --- a/src/app.html +++ b/src/app.html @@ -22,6 +22,7 @@ + %sveltekit.head% diff --git a/src/service-worker.ts b/src/service-worker.ts new file mode 100644 index 00000000..4c15bb6a --- /dev/null +++ b/src/service-worker.ts @@ -0,0 +1,80 @@ +/// + +import { build, files, version } from '$service-worker'; + +const worker = self as unknown as ServiceWorkerGlobalScope; +const FILES = `cache${version}`; + +// `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${version}`); + + 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/favicon-128x128.png b/static/favicon-128x128.png new file mode 100644 index 00000000..4519906d Binary files /dev/null and b/static/favicon-128x128.png differ diff --git a/static/favicon-144x144.png b/static/favicon-144x144.png new file mode 100644 index 00000000..8477e816 Binary files /dev/null and b/static/favicon-144x144.png differ diff --git a/static/favicon-16x16.png b/static/favicon-16x16.png new file mode 100644 index 00000000..38db0382 Binary files /dev/null and b/static/favicon-16x16.png differ diff --git a/static/favicon-192x192.png b/static/favicon-192x192.png new file mode 100644 index 00000000..48751ae1 Binary files /dev/null and b/static/favicon-192x192.png differ diff --git a/static/favicon-36x36.png b/static/favicon-36x36.png new file mode 100644 index 00000000..2c109b9b Binary files /dev/null and b/static/favicon-36x36.png differ diff --git a/static/favicon-48x48.png b/static/favicon-48x48.png new file mode 100644 index 00000000..c8a22689 Binary files /dev/null and b/static/favicon-48x48.png differ diff --git a/static/favicon-512x512.png b/static/favicon-512x512.png new file mode 100644 index 00000000..8a594882 Binary files /dev/null and b/static/favicon-512x512.png differ diff --git a/static/favicon-72x72.png b/static/favicon-72x72.png new file mode 100644 index 00000000..9cf8e666 Binary files /dev/null and b/static/favicon-72x72.png differ diff --git a/static/favicon-original.png b/static/favicon-original.png new file mode 100644 index 00000000..f82d3c10 Binary files /dev/null and b/static/favicon-original.png differ diff --git a/static/manifest.json b/static/manifest.json new file mode 100644 index 00000000..c06e227c --- /dev/null +++ b/static/manifest.json @@ -0,0 +1,50 @@ +{ + "short_name": "期限", + "name": "期限", + "start_url": "/", + "icons": [ + { + "src": "favicon-512x512.png", + "type": "image/png", + "sizes": "512x512" + }, + { + "src": "favicon-192x192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "favicon-144x144.png", + "type": "image/png", + "sizes": "144x144" + }, + { + "src": "favicon-128x128.png", + "type": "image/png", + "sizes": "128x128" + }, + { + "src": "favicon-72x72.png", + "type": "image/png", + "sizes": "72x72" + }, + { + "src": "favicon-48x48.png", + "type": "image/png", + "sizes": "48x48" + }, + { + "src": "favicon-36x36.png", + "type": "image/png", + "sizes": "36x36" + }, + { + "src": "favicon-16x16.png", + "type": "image/png", + "sizes": "16x16" + } + ], + "display": "standalone", + "scope": "/", + "description": "Instantly view which anime & manga on your lists have new episodes/chapters!" +} -- cgit v1.2.3