diff --git a/src/background.js b/src/background.js index d023cb1..bf5fd7d 100644 --- a/src/background.js +++ b/src/background.js @@ -50,7 +50,11 @@ browser.runtime.onStartup.addListener(async () => { // 清除缓存 const setting = await getSetting(); if (setting.clearCache) { - caches.delete(CACHE_NAME); + try { + caches.delete(CACHE_NAME); + } catch (err) { + console.log("[clean caches]", err); + } } // 同步订阅规则 diff --git a/src/libs/fetch.js b/src/libs/fetch.js index 5a941aa..829a15b 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -121,12 +121,12 @@ export const fetchData = async ( { useCache, usePool, translator, token, ...init } = {} ) => { const cacheReq = await newCacheReq(new Request(input, init)); - const cache = await caches.open(CACHE_NAME); let res; // 查询缓存 if (useCache) { try { + const cache = await caches.open(CACHE_NAME); res = await cache.match(cacheReq); } catch (err) { console.log("[cache match]", err); @@ -148,6 +148,7 @@ export const fetchData = async ( // 插入缓存 if (useCache) { try { + const cache = await caches.open(CACHE_NAME); await cache.put(cacheReq, res.clone()); } catch (err) { console.log("[cache put]", err);