我的 Service Worker 有问题。
我目前正在使用 offline.html 站点实现离线功能,以便在网络故障时显示。我已经实现了导航预加载,如下所述:https : //developers.google.com/web/updates/2017/02/navigation-preload#activation_navigation_preload
这是我安装的 EventListener是 skipWaiting() 并初始化新缓存
const version = 'v.1.2.3'
const CACHE_NAME = '::static-cache'
const urlsToCache = ['index~offline.html', 'favicon-512.png']
self.addEventListener('install', function(event) {
self.skipWaiting()
event.waitUntil(
caches
.open(version + CACHE_NAME)
.then(function(cache) {
return cache.addAll(urlsToCache)
})
.then(function() {
console.log('WORKER: install completed')
})
)
})
Run Code Online (Sandbox Code Playgroud)
这是我的激活 EventListener功能检测导航预加载并启用它。之后我检查旧缓存并删除它们
self.addEventListener('activate', event => {
console.log('WORKER: activated')
event.waitUntil(
(async function() {
// Feature-detect
if (self.registration.navigationPreload) {
// Enable navigation preloads!
console.log('WORKER: Enable navigation preloads') …Run Code Online (Sandbox Code Playgroud) google-chrome offline-caching offlineapps service-worker progressive-web-apps