aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-12-14 13:00:19 -0800
committerFuwn <[email protected]>2023-12-14 13:00:19 -0800
commit97744e3798aa930e4d0c9ba1a8b53d72f45e8623 (patch)
tree37e969aa077dc97845ba53700f46282341bb7cdd /src
parentfeat(worker): dev register (diff)
downloaddue.moe-97744e3798aa930e4d0c9ba1a8b53d72f45e8623.tar.xz
due.moe-97744e3798aa930e4d0c9ba1a8b53d72f45e8623.zip
style(worker): simplify statements
Diffstat (limited to 'src')
-rw-r--r--src/service-worker.ts22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/service-worker.ts b/src/service-worker.ts
index 7206ec40..83aabcee 100644
--- a/src/service-worker.ts
+++ b/src/service-worker.ts
@@ -5,7 +5,6 @@ import { dev } from '$app/environment';
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);
@@ -30,9 +29,7 @@ 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);
- }
+ for (const key of keys) if (key !== FILES) await caches.delete(key);
worker.clients.claim();
})
@@ -43,26 +40,28 @@ worker.addEventListener('activate', (event) => {
* 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 fetchAndCache = async (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 =
@@ -70,16 +69,15 @@ worker.addEventListener('fetch', (event) => {
const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname);
const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset;
- if (isHttp && !isDevServerRequest && !skipBecauseUncached) {
+ 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);
+ return (
+ (isStaticAsset && (await caches.match(event.request))) || fetchAndCache(event.request)
+ );
})()
);
- }
});