Javascript 如何使用URL删除服务工作者缓存
我有一个服务人员文件(Javascript 如何使用URL删除服务工作者缓存,javascript,service-worker,service-worker-events,Javascript,Service Worker,Service Worker Events,我有一个服务人员文件(sw.js),是我从互联网上获得的: const PRECACHE = 'precache-v1.1'; const RUNTIME = 'runtime'; // A list of local resources we always want to be cached. const PRECACHE_URLS = [ /* index page */ 'index.html', './', /* stylesheets */ './assets/c
sw.js
),是我从互联网上获得的:
const PRECACHE = 'precache-v1.1';
const RUNTIME = 'runtime';
// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
/* index page */
'index.html', './',
/* stylesheets */
'./assets/css/bootstrap.min.css', './assets/css/style.css',
/* javascripts */
'./assets/js/scripts.js',
/* images */
'./assets/images/logo.png'
];
// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
const currentCaches = [PRECACHE, RUNTIME];
event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});
// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
// Skip cross-origin requests, like those for Google Analytics.
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}
return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone()).then(() => {
return response;
});
});
});
})
);
}
});
在我网站的每一页上,比如index.html
,about.html
,contact.html
,我都有以下代码:
if('serviceWorker' in navigator) { navigator.serviceWorker.register('sw.js'); }
我之所以在每个页面上都有此代码,是因为我的网站上有很多页面,如果用户登录到我网站的任何页面,我希望浏览器缓存所有文件
例如,当用户访问about.html
时,服务人员会缓存sw.js
中列出的所有文件,并且也会缓存当前页面,即使该页面未在sw.js
中列出。这正是我想要的,因为我的网站上有数百个页面,我不想在sw.js
文件中手动列出所有页面。问题是,当我更新sw.js
中的版本号时,浏览器会删除sw.js
中列出的所有旧缓存,并重新缓存文件,除了about.html
。每当用户再次访问此页面时,都会显示旧的about.html
,并且所有其他文件都是新的
我如何克服这个问题?我想绝对删除我的网站缓存的所有文件,而不仅仅是
sw.js
中列出的文件,而且由于未列出about.html
,此缓存页面不会得到更新。问题是about.html文件保存在名为runtime的缓存中,该缓存未进行版本控制。所以这个缓存仍然是一样的,即使您部署了一个新的sw.js和一个递增的PRECACHE缓存。(如precache-v1.2)
解决方案是将版本控制添加到运行时缓存:
const PRECACHE = 'precache-v1.1';
const RUNTIME = 'runtime-v1.1';
或在激活时清理运行时缓存:
self.addEventListener('activate', event => {
// only PRECACHE (which is already the new version, setup on install event) should not be deleted.
const currentCaches = [PRECACHE];
...
问题是about.html文件保存在名为runtime的缓存中,该缓存没有版本控制。所以这个缓存仍然是一样的,即使您部署了一个新的sw.js和一个递增的PRECACHE缓存。(如precache-v1.2) 解决方案是将版本控制添加到运行时缓存:
const PRECACHE = 'precache-v1.1';
const RUNTIME = 'runtime-v1.1';
或在激活时清理运行时缓存:
self.addEventListener('activate', event => {
// only PRECACHE (which is already the new version, setup on install event) should not be deleted.
const currentCaches = [PRECACHE];
...
见