【发布时间】:2019-04-25 05:06:26
【问题描述】:
我正在尝试下载一个 json 文件并将其缓存,但出现错误。 代码如下:
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
// With the cache opened, load a JSON file containing an array of files to be cached
return fetch('resource').then(function(response) {
// Once the contents are loaded, convert the raw text to a JavaScript object
return response.json();
}).then(function(files) {
// Use cache.addAll just as you would a hardcoded array of items
console.log('[install] Adding files from JSON file: ', files);
return cache.addAll(files);
});
})
.then(function() {
// Message to simply show the lifecycle flow
console.log(
'[install] All required resources have been cached;',
'the Service Worker was successfully installed!'
);
// Force activation
return self.skipWaiting();
})
);
mysw.js:42 Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
cache.addAll(文件);似乎在制造问题。
【问题讨论】:
-
你作为
files传递的类型是什么。根据documentation,它必须是(我引用)“你想要获取并添加到缓存中的字符串URL数组。”