我正在尝试从Service Worker中获取缓存的文件但是Fetch事件没有在最新版本的chrome中触发。我正在获取"获取处理程序存在:EXISTS"。 这是我使用chrome:// serviceworker-internals /
获得的响应Scope: https://me.development.symphoni.io:8443/resources/ Registration ID: 3 Active worker: Installation Status: ACTIVATED Running Status: RUNNING Fetch handler existence: EXISTS Script: https://me.development.symphoni.io:8443/resources/swa.js Version ID: 4 Renderer process ID: 9704 Renderer thread ID: 6912 DevTools agent route ID: 22 Log: Console: {"lineNumber":50,"message":"Opened cache","message_level":1,"sourceIdentifier":3,"sourceURL":"https://me.development.symphoni.io:8443/resources/swa.js"}
以下是我在Windows上加载的app-sw.js
if ('serviceWorker' in navigator) {
debugger;
navigator.serviceWorker.register('resources/swa.js').then(function(reg) {
if(reg.installing) {
console.log('Service worker installing');
} else if(reg.waiting) {
console.log('Service worker installed');
} else if(reg.active) {
console.log('Service worker active');
}
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
下面是一个swa.js代码,其中Fetch事件是有的 控制台日志
// Declare the Cache names with version
var CACHE_NAME = 'site-cache-v1';
debugger;
// We can have multiple sets of files to cache
var urlsToCache = [
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.js"
];
self.addEventListener('fetch', function(event) {
debugger;
console.log('Handli`enter code here`ng fetch event for', event.request.url);
});
// Add
// Add or install the files into Service Workers
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
//Update
//Delete all Service Workers except those in the whiteList
self.addEventListener('activate', function(event) {
debugger;
var cacheWhitelist =['my-site-cache-v1','pages-cache-v1', 'blog-posts-cache-v1'];
//var cacheWhitelist = ['pages-cache-v1', 'blog-posts-cache-v1'];
event.waitUntil(self.clients.claim(),
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});