【发布时间】:2018-07-27 11:43:43
【问题描述】:
我希望你能帮助我解决我的问题。 目前,我与服务人员构建了一个 PWA。它注册成功,但安装有问题。
“caches.open”-promise 导致错误:“TypeError: Request failed at”。您可以在 Chrome 中看到缓存已注册,但为空。 我已经检查过缓存网址一千次了..
这是我的 Service-worker 代码
var CACHE_NAME = 'surv-cache-1';
var resourcesToCache = [
'/',
'/index.html',
'/jquery-3.2.1.min.js',
'/pouchdb.min-6.4.1.js',
'/styles/inline.css',
'/scripts/app.js'
];
self.addEventListener('install', function(event) {
event.waitUntil(
// open the app browser cache
caches.open(CACHE_NAME).then(function(cache) {
console.log("Install succesfull");
// add all app assets to the cache
return cache.addAll(resourcesToCache);
}).then(function(out){
console.log(out);
}).catch(function(err){
console.log(err);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
// try to find corresponding response in the cache
caches.match(event.request)
.then(function(response) {
if (response) {
// cache hit: return cached result
return response;
}
// not found: fetch resource from the server
return fetch(event.request);
}).catch(function(err){
console.log(err);
})
);
});
还有我的注册码:
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js').then(function(registration) {
console.log('Service worker registered:'+registration.scope);
}).catch(function(e) {
console.log(e);
});
};
我没听懂..我希望你有一个想法:)
编辑:我想我现在知道为什么它不起作用了。我有我的域的身份验证,所以不是每个人都可以访问它。 当我的服务人员想要缓存数据时,它得到了 401。所以这似乎是身份验证的问题。
也许有人已经遇到过同样的问题?
【问题讨论】:
-
同样的问题,还没找到解决办法。
标签: html browser-cache basic-authentication service-worker http-status-code-401