我的爱好网站上有一个 service worker,它有一个 install
事件处理程序,看起来像这样(在 TypeScript 中):
/**
* When the service worker is being intalled, download the required assets into a temporary cache
* @param e The intall event
*/
function installHander(e: ExtendableEvent): void {
"use strict";
// Put updated resources in a new cache, so that currently running pages
// get the current versions.
e.waitUntil(caches.delete("core-waiting").then(() => {
return caches.open("core-waiting").then((core) => {
const resourceUrls = [
"/",
"/loading/",
"/offline/",
"/css/site.css",
"/js/site.js"
];
return Promise.all(resourceUrls.map((key) => {
// Make sure to download fresh versions of the files!
return fetch(key, { cache: "no-cache" })
.then((response) => core.put(key, response));
}))
// Don't wait for the client to refresh the page (as this site is designed not to refresh)
.then(() => (self as ServiceWorkerGlobalScope).skipWaiting());
});
}));
}
在 Firefox 中这工作得很好,但 Chrome 65 从 caches.open("core-waiting")
返回 undefined
,所以当我尝试时它抛出错误在 core
上调用 put
。 According to the docs ,这应该是不可能的。知道这里发生了什么吗?
最佳答案
原来Visual Studio打开的Google Chrome窗口无法使用缓存API,只是默默返回undefined。如果您手动打开 Chrome 窗口,它就可以正常工作。
真的应该有一个关于这个的控制台警告,这样开发人员就不会浪费时间为此挠头......
关于javascript - CacheStorage.open() 在 Chrome 中返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49774541/