service-worker.js:1 GET http://localhost:8080/service-worker.js net::ERR_FILE_EXISTS
这是我在注册服务人员后每次刷新时都会遇到的错误。我已确保 service-worker.js 文件存在于根目录中。服务人员也已注册并且工作正常。但我仍然不断收到这个错误。我也在本地主机上工作。
这是我的 service-worker.js 文件:
console.log("SW startup");
var CACHE_NAME = "my_cache";
var urlsToCache = [
'./',
'./css/style.css',
'./js/script.js'
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open(CACHE_NAME).then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request.clone()).then(function(response) {
console.dir(response);
console.log('hi');
cache.put(event.request.clone(), response.clone());
return response;
});
});
})
);
});
script.js 文件:
if (navigator.serviceWorker) {
console.log("ServiceWorkers are supported");
navigator.serviceWorker.register('service-worker.js')
.then(function(reg) {
console.log("ServiceWorker registered ◕‿◕");
console.dir(reg);
})
.catch(function(error) {
console.log("Failed to register ServiceWorker ಠ_ಠ");
console.dir(error);
});
}