0
无可否认,我在这件事上很绿。我试图得到一个服务人员,以显示离线页面,我得到这个错误,当我把浏览器离线:服务人员在Chrome中获取Cachebuster错误
不能得到/offline.html?cachebust=1485461215845
我根据我的对这个职位代码:
我的index.html脚本有这个在它:
<script type="text/javascript">
// Register the service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
</script>
我的服务人员的代码(sw.js )在它有这样的:
'use strict';
var cacheVersion = 1;
var currentCache = {
offline: 'offline-cache' + cacheVersion
};
const offlineUrl = 'offline.html';
this.addEventListener('install', event => {
event.waitUntil(
caches.open(currentCache.offline).then(function(cache) {
return cache.addAll([
offlineUrl
]);
})
);
});
this.addEventListener('fetch', event => {
// request.mode = navigate isn't supported in all browsers
// so include a check for Accept: text/html header.
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
event.respondWith(
fetch(event.request.url).catch(error => {
// Return the offline page
return caches.match(offlineUrl);
})
);
}
else{
// Respond with everything else if we can
event.respondWith(caches.match(event.request)
.then(function (response) {
return response || fetch(event.request);
})
);
}
});
调试代码看起来一切正常,除了(最后一行执行是返回caches.match(offlineurl)),但不是呈现离线网页我得到的缓存无效错误。关于cachebuster的谷歌并不多。任何洞察力将不胜感激。
'不能得到/offline.html?cachebust= 1485461215845' - 你试图从一个'offline.html'得到这个主机吗? –
是的,我可以通过直接的URL浏览文件 – user3253156