我正在构建一个渐进式Web应用程序,服务工作者代码如下:
谢谢您的帮助。
CURRENT_CACHE = 'V3'; FILES_TO_CACHE = [ '/index.html', '/js/bcheck.js', '/js/mss.js', '/js/vendor.js' ];我注意到在安装后,所有文件都已被缓存。但是,当我将服务器脱机并重新加载页面时,似乎服务脱机了,没有任何内容被加载。
console.info('in file'); self.addEventListener('install', function (event) { console.info('installed'); event.waitUntil(caches.open(CURRENT_CACHE).then(function(cache){ return cache.addAll(FILES_TO_CACHE); })); }); self.addEventListener('activate', function (event) { console.info('activated'); event.waitUntil(caches.keys().then(function (cachesNames) { return Promise.all(cachesNames.map(function (cacheName) { if (cacheName !== CURRENT_CACHE) { return caches.delete(cacheName); } })) })); });
self.addEventListener('fetch', function(event) { event.respondWith( caches.match(event.request) .then(function(response) { // Cache hit - return response if (response) { return response; }
return fetch(event.request); } ) ); });
谢谢您的帮助。
- 我使用本地HTTP服务器localhost。