service-worker.js:1 GET http://localhost:8080/service-worker.js net::ERR_FILE_EXISTS
这是我在注册 service worker 后每次刷新时得到的错误。我已确保 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);
});
}
最佳答案
我遇到了同样的问题。可以安全地忽略它。
此错误跟踪从 Chrome 中移除噪音:https://code.google.com/p/chromium/issues/detail?id=541797
它应该从 Chrome 50 开始上线。
来自线程:
Improve error code for service worker bailing due to no update found
ServiceWorkerWriteToCacheJob is the URLRequestJob responsible for fetching and writing the updated script. It fails with network error when it wants to abort the update because the new script is the same as the old one.
Currently that results in ERR_FAILED errors appearing in the DevTools console and netlog, which is confusing and hard to debug because that error also occurs for actual network errors. This patch changes the error to FILE_EXISTS, so it's more clear why the job "failed".
关于javascript - Service Worker 抛出 net::ERR_FILE_EXISTS 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328579/