minor tweaks

This commit is contained in:
Min Idzelis 2025-05-16 01:22:36 +00:00
parent 00bcf659c3
commit 92befd87a7
2 changed files with 13 additions and 15 deletions

View File

@ -1,9 +1,6 @@
import { build, files, version } from '$service-worker';
const pendingLoads = new Map<string, AbortController>();
const useCache = true;
// Create a unique cache name for this deployment
const CACHE = `cache-${version}`;
export const APP_RESOURCES = [
@ -11,6 +8,9 @@ export const APP_RESOURCES = [
...files, // everything in `static`
];
export const isURL = (request: URL | RequestInfo): request is URL => (request as URL).href !== undefined;
export const isRequest = (request: RequestInfo): request is Request => (request as Request).url !== undefined;
export async function deleteOldCaches() {
for (const key of await caches.keys()) {
if (key !== CACHE) {
@ -24,8 +24,15 @@ export async function addFilesToCache() {
await cache.addAll(APP_RESOURCES);
}
export const isURL = (request: URL | RequestInfo): request is URL => (request as URL).href !== undefined;
export const isRequest = (request: RequestInfo): request is Request => (request as Request).url !== undefined;
const pendingLoads = new Map<string, AbortController>();
export async function cancelLoad(urlString: string) {
const pending = pendingLoads.get(urlString);
if (pending) {
pending.abort();
pendingLoads.delete(urlString);
}
}
export async function getCachedOrFetch(request: URL | Request | string, cancelable: boolean = false) {
const cached = await checkCache(request);
@ -42,7 +49,6 @@ export async function getCachedOrFetch(request: URL | Request | string, cancelab
return await fetchWithCancellation(request, cached.cache);
} catch {
console.log('getCachedOrFetch error', request);
return new Response(undefined, {
status: 499,
statusText: 'Request canceled: Instructions unclear, accidentally interrupted myself',
@ -50,14 +56,6 @@ export async function getCachedOrFetch(request: URL | Request | string, cancelab
}
}
export async function cancelLoad(urlString: string) {
const pending = pendingLoads.get(urlString);
if (pending) {
pending.abort();
pendingLoads.delete(urlString);
}
}
async function fetchWithCancellation(request: URL | Request | string, cache: Cache) {
const cacheKey = getCacheKey(request);
const cancelToken = new AbortController();

View File

@ -4,7 +4,7 @@
/// <reference lib="webworker" />
import { installBroadcastChannelListener } from './broadcast-channel';
import { addFilesToCache, deleteOldCaches } from './cache';
import { handleFetchEvent } from './fetchEvent';
import { handleFetchEvent } from './fetch-event';
const sw = globalThis as unknown as ServiceWorkerGlobalScope;