javascript - Service Worker 抛出 net::ERR_FILE_EXISTS 错误?

标签 javascript google-chrome service-worker

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/

相关文章:

javascript - 在 PHP 中记录动态输入值

html - 网站在 Firefox 中运行良好,但在 Chrome 中运行不正常

javascript - 在后台运行持久的 Web Worker 而不会受到限制

javascript - 如何将网络通知内容复制到剪贴板

javascript - Logo 图像无法在 React 应用程序中呈现。模块解析失败

javascript - 如果父元素只有 2 个子元素,则执行某些操作,否则执行其他操作

javascript - 谷歌浏览器新内容行为

javascript - Jquery 滚动必须更频繁地触发

node.js - 无法为 Vue Webpack 构建的应用程序加载工作脚本

javascript - TypeError : app. get 不是函数