From f2ec6508c87ba0f7e06affc90698a2ecbd9ffb9a Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 21 Oct 2024 18:03:03 +0800 Subject: [PATCH] Fix single asset fetch --- Build/lib/cache-filesystem.ts | 69 +++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/Build/lib/cache-filesystem.ts b/Build/lib/cache-filesystem.ts index 99d00d91..62603871 100644 --- a/Build/lib/cache-filesystem.ts +++ b/Build/lib/cache-filesystem.ts @@ -358,27 +358,64 @@ export class Cache { return value; } 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 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)); - this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC); - return deserializer(error.data); + const on304 = (error: Custom304NotModifiedError) => { + console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl)); + this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC); + return deserializer(error.data); + }; + + const onNoETagFallback = (error: CustomNoETagFallbackError) => { + console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl)); + 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 (error.digest === 'CustomNoETagFallbackError') { - console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl)); - return deserializer(error.data); + 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); + } + } 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(error.url), error); + console.log(picocolors.red('[fetch error]'), picocolors.gray(primaryUrl), e); } }