Service-Worker, "TypeError:Request failed at <anonymous>"

11

我希望你能帮助我解决问题。目前我正在构建一个使用service-worker的PWA应用程序。它已经成功注册,但安装时出现了一些问题。

"caches.open"的Promise返回了一个错误:"TypeError: Request failed at"。在Chrome中可以看到缓存已经注册,但是为空。我已经检查了缓存的URL无数次...

这是我的Service-worker代码:

var CACHE_NAME = 'surv-cache-1';

var resourcesToCache = [
    '/',
    '/index.html',
    '/jquery-3.2.1.min.js',
    '/pouchdb.min-6.4.1.js',
    '/styles/inline.css',
    '/scripts/app.js'
];

self.addEventListener('install', function(event) {
  event.waitUntil(
    // open the app browser cache
    caches.open(CACHE_NAME).then(function(cache) {
        console.log("Install succesfull");
        // add all app assets to the cache
        return cache.addAll(resourcesToCache);
    }).then(function(out){
        console.log(out);
    }).catch(function(err){
        console.log(err);
    })
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    // try to find corresponding response in the cache
    caches.match(event.request)
      .then(function(response) {
        if (response) {
          // cache hit: return cached result
          return response;
        }

        // not found: fetch resource from the server
        return fetch(event.request);
      }).catch(function(err){
          console.log(err);
      })
  );
});

我的注册码是:

<script>
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('service-worker.js').then(function(registration) {
            console.log('Service worker registered:'+registration.scope);
        }).catch(function(e) {
            console.log(e);
        });
    };

我不理解...希望你有想法:)

编辑:我现在知道为什么它不起作用了。我为我的域名设置了身份验证,所以不是每个人都可以访问它。当我的serviceworker要缓存数据时,它会收到401错误。因此,这似乎是一个身份验证问题。

也许有人已经遇到过同样的问题?


同样的问题,还没有找到解决方案。 - Sumama Waheed
3个回答

12

当你的resourcesToCache包含返回404响应的内容时会发生这种情况。请确保你输入的所有内容都是正确的。同时,请确保作用域正确。你可以使用以下方式检查你的worker作用域:

if("serviceWorker" in navigator) {
  navigator.serviceWorker
  .register(`worker.js`)
  .then(registration => {
    console.log("SW scope:", registration.scope);
  });
}

如果您的项目不在服务器域根目录中,做类似以下的操作可能会有帮助:

//your main js
if("serviceWorker" in navigator) {
  navigator.serviceWorker
  .register(`${window.location.pathname}worker.js`)
  .then(registration => {
    //do your thing
  });
}


//your worker js
let resourcesToCache = [
  './',
  './index.html',
  './jquery-3.2.1.min.js',
  './pouchdb.min-6.4.1.js',
  './styles/inline.css',
  './scripts/app.js',
];
//...

作为一则旁注,你应该从CDN加载你的库(jQuery,pouchdb),以提高性能。这些也可以被缓存:

let resourcesToCache = [
  './',
  './index.html',
  'https://code.jquery.com/jquery-3.3.1.min.js',
  'https://cdnjs.cloudflare.com/ajax/libs/pouchdb/6.4.3/pouchdb.min.js',
  './styles/inline.css',
  './scripts/app.js',
];

关于404响应的那一部分对我来说完全正确! - Aankhen

2

当我在Windows机器上本地开发并在Linux服务器上部署时,我遇到了这个问题,问题出在路径上。您需要在路径前面添加一个“.”以使其像“./”一样,具体如下:

var resourcesToCache = [
'./',
'./index.html',
'./jquery-3.2.1.min.js',
'./pouchdb.min-6.4.1.js',
'./styles/inline.css',
'./scripts/app.js'

];


1
当我引用一个指定路径中不存在的文件进行缓存时,我遇到了这个问题。当我更新了文件路径后,问题得到了解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接