【发布时间】:2019-03-01 03:25:51
【问题描述】:
我正在尝试在现有的 asp mvc 应用程序中使用服务工作者。一般来说,它工作正常:我可以缓存文件等等。问题是,有很多文件要缓存,我正在尝试将路径数组返回给服务工作者,以便可以将文件添加到缓存而无需手动添加它们。
这是我目前所拥有的:
控制器:
public ActionResult GetFilesToCache()
{
string[] filePaths = Directory.GetFiles(Server.MapPath(@"~\Content"), "*", SearchOption.AllDirectories);
string[] cuttedFiles = new string[filePaths.Length];
int i = 0;
foreach (var path in filePaths)
{
cuttedFiles[i] = path.Substring(path.IndexOf("Content"));
i++;
}
return Json(new { filesToCache = cuttedFiles }, JsonRequestBehavior.AllowGet);
}
这给了我一个字符串数组,其中包含“Content\image1.png”等条目。
服务工作者:
self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(cacheName).then(function (cache) {
console.log('[ServiceWorker] Caching app shell');
return fetch('Home/GetFilesToCache').then(function (response) {
return response;
}).then(function (files) {
return cache.addAll(files);
});
})
);
});
我得到的错误是:
Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.
调用该动作就可以了,服务工作者接收到数据,但没有添加到缓存中。
【问题讨论】:
标签: asp.net-mvc service-worker