mirror of
https://github.com/immich-app/immich
synced 2025-10-17 18:19:27 +00:00
fix: ignore abort errors (#21262)
This commit is contained in:
parent
63088b22e0
commit
a3e0c6cef5
2 changed files with 35 additions and 18 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { cancelRequest, handleRequest } from './request';
|
import { handleCancel, handlePreload } from './request';
|
||||||
|
|
||||||
export const installBroadcastChannelListener = () => {
|
export const installBroadcastChannelListener = () => {
|
||||||
const broadcast = new BroadcastChannel('immich');
|
const broadcast = new BroadcastChannel('immich');
|
||||||
|
|
@ -7,12 +7,19 @@ export const installBroadcastChannelListener = () => {
|
||||||
if (!event.data) {
|
if (!event.data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const urlString = event.data.url;
|
|
||||||
const url = new URL(urlString, event.origin);
|
const url = new URL(event.data.url, event.origin);
|
||||||
if (event.data.type === 'cancel') {
|
|
||||||
cancelRequest(url);
|
switch (event.data.type) {
|
||||||
} else if (event.data.type === 'preload') {
|
case 'preload': {
|
||||||
handleRequest(url);
|
handlePreload(url);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'cancel': {
|
||||||
|
handleCancel(url);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
import { get, put } from './cache';
|
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 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 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}`);
|
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) => {
|
export const handleRequest = async (request: URL | Request) => {
|
||||||
const cacheKey = getCacheKey(request);
|
const cacheKey = getCacheKey(request);
|
||||||
|
|
||||||
const cachedResponse = await get(cacheKey);
|
const cachedResponse = await get(cacheKey);
|
||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
|
|
@ -41,23 +48,26 @@ export const handleRequest = async (request: URL | Request) => {
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
if (error.name === 'AbortError') {
|
||||||
return new Response(undefined, {
|
// dummy response avoids network errors in the console for these requests
|
||||||
status: 499,
|
return new Response(undefined, { status: 204 });
|
||||||
statusText: `Request canceled: Instructions unclear, accidentally interrupted myself (${error})`,
|
}
|
||||||
});
|
|
||||||
|
console.log('Not an abort error', error);
|
||||||
|
|
||||||
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
pendingRequests.delete(cacheKey);
|
pendingRequests.delete(cacheKey);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cancelRequest = (url: URL) => {
|
export const handleCancel = (url: URL) => {
|
||||||
const cacheKey = getCacheKey(url);
|
const cacheKey = getCacheKey(url);
|
||||||
const pending = pendingRequests.get(cacheKey);
|
const pendingRequest = pendingRequests.get(cacheKey);
|
||||||
if (!pending) {
|
if (!pendingRequest) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pending.abort();
|
pendingRequest.abort();
|
||||||
pendingRequests.delete(cacheKey);
|
pendingRequests.delete(cacheKey);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue