fix: ignore abort errors (#21262)

This commit is contained in:
Jason Rasmussen 2025-08-25 16:42:30 -04:00 committed by GitHub
parent 63088b22e0
commit a3e0c6cef5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { cancelRequest, handleRequest } from './request';
import { handleCancel, handlePreload } from './request';
export const installBroadcastChannelListener = () => {
const broadcast = new BroadcastChannel('immich');
@ -7,12 +7,19 @@ export const installBroadcastChannelListener = () => {
if (!event.data) {
return;
}
const urlString = event.data.url;
const url = new URL(urlString, event.origin);
if (event.data.type === 'cancel') {
cancelRequest(url);
} else if (event.data.type === 'preload') {
handleRequest(url);
const url = new URL(event.data.url, event.origin);
switch (event.data.type) {
case 'preload': {
handlePreload(url);
break;
}
case 'cancel': {
handleCancel(url);
break;
}
}
};
};

View File

@ -1,5 +1,7 @@
import { get, put } from './cache';
const pendingRequests = new Map<string, AbortController>();
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;
@ -21,11 +23,16 @@ const getCacheKey = (request: URL | Request) => {
throw new Error(`Invalid request: ${request}`);
};
const pendingRequests = new Map<string, AbortController>();
export const handlePreload = async (request: URL | Request) => {
try {
return await handleRequest(request);
} catch (error) {
console.error(`Preload failed: ${error}`);
}
};
export const handleRequest = async (request: URL | Request) => {
const cacheKey = getCacheKey(request);
const cachedResponse = await get(cacheKey);
if (cachedResponse) {
return cachedResponse;
@ -41,23 +48,26 @@ export const handleRequest = async (request: URL | Request) => {
return response;
} catch (error) {
console.log(error);
return new Response(undefined, {
status: 499,
statusText: `Request canceled: Instructions unclear, accidentally interrupted myself (${error})`,
});
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;
} finally {
pendingRequests.delete(cacheKey);
}
};
export const cancelRequest = (url: URL) => {
export const handleCancel = (url: URL) => {
const cacheKey = getCacheKey(url);
const pending = pendingRequests.get(cacheKey);
if (!pending) {
const pendingRequest = pendingRequests.get(cacheKey);
if (!pendingRequest) {
return;
}
pending.abort();
pendingRequest.abort();
pendingRequests.delete(cacheKey);
};