Fix single asset fetch

This commit is contained in:
SukkaW 2024-10-21 18:03:03 +08:00
parent 98d75888cb
commit f2ec6508c8

View File

@ -358,28 +358,65 @@ export class Cache<S = string> {
return value; return value;
} catch (e) { } catch (e) {
if (e && typeof e === 'object' && 'errors' in e && Array.isArray(e.errors)) {
const deserializer = 'deserializer' in opt ? opt.deserializer : identity as any; const deserializer = 'deserializer' in opt ? opt.deserializer : identity as any;
for (let i = 0, len = e.errors.length; i < len; i++) { const on304 = (error: Custom304NotModifiedError) => {
const error = e.errors[i];
if ('name' in error && (error.name === 'CustomAbortError' || error.name === 'AbortError')) {
continue;
}
if ('digest' in error) {
if (error.digest === 'Custom304NotModifiedError') {
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl)); console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC); this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
return deserializer(error.data); return deserializer(error.data);
} };
if (error.digest === 'CustomNoETagFallbackError') {
const onNoETagFallback = (error: CustomNoETagFallbackError) => {
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl)); console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
return deserializer(error.data); return deserializer(error.data);
};
if (e && typeof e === 'object') {
if ('errors' in e && Array.isArray(e.errors)) {
for (let i = 0, len = e.errors.length; i < len; i++) {
const error = e.errors[i];
if ('name' in error) {
if (error.name === 'CustomAbortError' || error.name === 'AbortError') {
continue;
}
if (error.name === 'Custom304NotModifiedError') {
return on304(error);
}
if (error.name === 'CustomNoETagFallbackError') {
return onNoETagFallback(error);
}
}
if ('digest' in error) {
if (error.digest === 'Custom304NotModifiedError') {
return on304(error);
}
if (error.digest === 'CustomNoETagFallbackError') {
return onNoETagFallback(error);
} }
} }
console.log(picocolors.red('[fetch error]'), picocolors.gray(error.url), error); console.log(picocolors.red('[fetch error]'), picocolors.gray(error.url), error);
} }
} else {
if ('name' in e) {
if (e.name === 'Custom304NotModifiedError') {
return on304(e as Custom304NotModifiedError);
}
if (e.name === 'CustomNoETagFallbackError') {
return onNoETagFallback(e as CustomNoETagFallbackError);
}
}
if ('digest' in e) {
if (e.digest === 'Custom304NotModifiedError') {
return on304(e as Custom304NotModifiedError);
}
if (e.digest === 'CustomNoETagFallbackError') {
return onNoETagFallback(e as CustomNoETagFallbackError);
}
}
console.log(picocolors.red('[fetch error]'), picocolors.gray(primaryUrl), e);
}
} }
console.log({ e, name: (e as any).name }); console.log({ e, name: (e as any).name });