2025-12-05 15:19:44 +00:00
|
|
|
import { replyIsImageUrlCached } from './broadcast-channel';
|
2025-08-25 12:52:57 -04:00
|
|
|
import { get, put } from './cache';
|
|
|
|
|
|
2025-08-25 16:42:30 -04:00
|
|
|
const pendingRequests = new Map<string, AbortController>();
|
|
|
|
|
|
2025-08-25 12:52:57 -04:00
|
|
|
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}`);
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-25 16:42:30 -04:00
|
|
|
export const handlePreload = async (request: URL | Request) => {
|
|
|
|
|
try {
|
|
|
|
|
return await handleRequest(request);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Preload failed: ${error}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-08-25 12:52:57 -04:00
|
|
|
|
|
|
|
|
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);
|
2025-12-05 15:19:44 +00:00
|
|
|
await put(cacheKey, response);
|
2025-08-25 12:52:57 -04:00
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
} catch (error) {
|
2025-08-25 16:42:30 -04:00
|
|
|
if (error.name === 'AbortError') {
|
|
|
|
|
// dummy response avoids network errors in the console for these requests
|
|
|
|
|
return new Response(undefined, { status: 204 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('Not an abort error', error);
|
|
|
|
|
|
|
|
|
|
throw error;
|
2025-08-25 12:52:57 -04:00
|
|
|
} finally {
|
|
|
|
|
pendingRequests.delete(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-25 16:42:30 -04:00
|
|
|
export const handleCancel = (url: URL) => {
|
2025-08-25 12:52:57 -04:00
|
|
|
const cacheKey = getCacheKey(url);
|
2025-08-25 16:42:30 -04:00
|
|
|
const pendingRequest = pendingRequests.get(cacheKey);
|
|
|
|
|
if (!pendingRequest) {
|
2025-08-25 12:52:57 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 16:42:30 -04:00
|
|
|
pendingRequest.abort();
|
2025-08-25 12:52:57 -04:00
|
|
|
pendingRequests.delete(cacheKey);
|
|
|
|
|
};
|
2025-12-05 15:19:44 +00:00
|
|
|
|
|
|
|
|
export const handleIsUrlCached = async (url: URL) => {
|
|
|
|
|
const cacheKey = getCacheKey(url);
|
|
|
|
|
const isImageUrlCached = !!(await get(cacheKey));
|
|
|
|
|
replyIsImageUrlCached(url.pathname + url.search + url.hash, isImageUrlCached);
|
|
|
|
|
};
|