【发布时间】:2019-07-10 13:47:06
【问题描述】:
我的网站的 pwa 有问题。我需要使用服务人员更新缓存,我在互联网上找到了很多代码,但没有一个有效。我必须错过一些东西。
我希望在简单重新加载页面后完成更新,同样当我们点击 PWA 的快捷方式时,所有内容都会自动更新。这是我的清单和我的服务人员以及我使用的代码。
你怎么看?
提前谢谢你
//Manifest -----
{
"name": "Test",
"short_name": "Test",
"lang": "en",
"start_url": "./index.html",
"display": "standalone",
"background_color": "white",
"theme_color": "white",
"icons": [{
"src": "/img/icon-128.png",
"sizes": "128x128",
"type": "image/png"
}, {
"src": "/img/icon-144.png",
"sizes": "144x144",
"type": "image/png"
}, {
"src": "/img/icon-152.png",
"sizes": "152x152",
"type": "image/png"
}, {
"src": "/img/icon-192.png",
"sizes": "192x192",
"type": "image/png"
}, {
"src": "/img/icon-256.png",
"sizes": "256x256",
"type": "image/png"
}, {
"src": "/img/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}]
}
-
//Service-Worker -----
var cacheName = 'Test';
var filesToCache = [
'/',
'/index.html',
'/css/style.css',
...
];
/* Start the service worker and cache all of the app's content */
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(filesToCache);
})
);
});
/* Serve cached content when offline */
self.addEventListener('fetch', function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
});
self.addEventListener('activate', function(event) {
var cacheKeeplist = ['cacheName'];
event.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (cacheKeeplist.indexOf(key) === -1) {
return caches.delete(key);
}
}));
})
);
});
【问题讨论】:
标签: javascript web service-worker progressive-web-apps