我遵循一个简单的PWA教程,但是当我完成它时,我会得到以下控制台错误,
Uncaught (in promise) TypeError: Failed to execute 'Cache' on 'addAll': Request failed
这是我的服务生档案
const staticDevCoffee = "dev-coffee-site-v1"
const assets = [
"/",
"/test.html",
"/csstest/style.css",
"/jstest/app.js",
"/coffee.png",
]
self.addEventListener("install", installEvent => {
installEvent.waitUntil(
caches.open(staticDevCoffee).then(cache => {
cache.addAll(assets)
})
)
})
当我做灯塔测试时,我得到了这个,
start_url does not respond with a 200 when offlineThe start_url did respond, but not via a service worker.
这是我第一次看PWAs,所以我有点卡住了。我已经尝试过几种解决办法,但都没有用。
发布于 2021-03-10 05:48:28
TypeError:未能在‘addAll:请求失败的上执行'Cache’
当缓存列表中提到的任何文件返回404响应时,就会得到此异常。所以要确保所有的资源都投入了200英镑。
第二个错误的
当start_url offlineThe start_url做出响应时,不会以200作为响应。
在您的情况下,由于文件没有被缓存(由于第一个异常),您将得到此错误,并确保在缓存列表中缓存根索引文件。
发布于 2022-11-19 07:57:43
我也有这样的问题:在我的应用程序中,我在assets[]
数组中有几十个文件要加载到缓存中,手动选择(也就是说,缓存loading.gif
没有意义,因为加载后不需要它)。然后,任何被遗忘的更改都会使缓存变为空,错误的原因将被隐藏,正如公认的答案所解释的那样。在反复搜索丢失的内容之后,我对“方便”的cache.addAll
失去了耐心,并切换回了cache.add
,因此,没有使用无用的TypeError
,而是报告了导致故障的文件:
...
const filesUpdate = cache => {
const stack = [];
assets.forEach(file => stack.push(
cache.add(file).catch(_=>console.error(`can't load ${file} to cache`))
));
return Promise.all(stack);
};
installEvent.waitUntil(caches.open(staticDevCoffee).then(filesUpdate));
...
我希望这也能节省一些人对cache.addAll()
的紧张。
发布于 2022-05-27 06:17:00
self.addEventListener("install", (event) => {
console.log("Service Worker : Installed!")
event.waitUntil(
(async() => {
try {
cache_obj = await caches.open(cache)
cache_obj.addAll(caching_files)
}
catch{
console.log("error occured while caching...")
}
})()
)
} )
这个密码对我有用!
https://stackoverflow.com/questions/66529102
复制相似问题