浏览代码

Fix single asset fetch

SukkaW 1 年之前
父节点
当前提交
f2ec6508c8
共有 1 个文件被更改,包括 52 次插入15 次删除
  1. 52 15
      Build/lib/cache-filesystem.ts

+ 52 - 15
Build/lib/cache-filesystem.ts

@@ -358,27 +358,64 @@ export class Cache<S = string> {
 
       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;
+
+      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 ('digest' in error) {
+              if (error.digest === 'Custom304NotModifiedError') {
+                return on304(error);
+              }
+              if (error.digest === 'CustomNoETagFallbackError') {
+                return onNoETagFallback(error);
+              }
+            }
 
-        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;
+            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 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);
+          if ('digest' in e) {
+            if (e.digest === 'Custom304NotModifiedError') {
+              return on304(e as Custom304NotModifiedError);
             }
-            if (error.digest === 'CustomNoETagFallbackError') {
-              console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
-              return deserializer(error.data);
+            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);
         }
       }