refactor: service worker (#21250)

This commit is contained in:
Jason Rasmussen
2025-08-25 12:52:57 -04:00
committed by GitHub
parent d5f3629c49
commit 7531ffcbfb
5 changed files with 118 additions and 215 deletions
+63
View File
@@ -0,0 +1,63 @@
import { get, put } from './cache';
const isURL = (request: URL | RequestInfo): request is URL => (request as URL).href !== undefined;
const isRequest = (request: RequestInfo): request is Request => (request as Request).url !== undefined;
const assertResponse = (response: Response) => {
if (!(response instanceof Response)) {
throw new TypeError('Fetch did not return a valid Response object');
}
};
const getCacheKey = (request: URL | Request) => {
if (isURL(request)) {
return request.toString();
}
if (isRequest(request)) {
return request.url;
}
throw new Error(`Invalid request: ${request}`);
};
const pendingRequests = new Map<string, AbortController>();
export const handleRequest = async (request: URL | Request) => {
const cacheKey = getCacheKey(request);
const cachedResponse = await get(cacheKey);
if (cachedResponse) {
return cachedResponse;
}
try {
const cancelToken = new AbortController();
pendingRequests.set(cacheKey, cancelToken);
const response = await fetch(request, { signal: cancelToken.signal });
assertResponse(response);
put(cacheKey, response);
return response;
} catch (error) {
console.log(error);
return new Response(undefined, {
status: 499,
statusText: `Request canceled: Instructions unclear, accidentally interrupted myself (${error})`,
});
} finally {
pendingRequests.delete(cacheKey);
}
};
export const cancelRequest = (url: URL) => {
const cacheKey = getCacheKey(url);
const pending = pendingRequests.get(cacheKey);
if (!pending) {
return;
}
pending.abort();
pendingRequests.delete(cacheKey);
};