渐进式网络应用程序Uncaught(承诺)TypeError:无法获取

Ku3*_*u3a 12 javascript service-worker progressive-web-apps

我开始学习PWA(渐进式Web应用程序),我遇到问题,控制台"抛出"错误未捕获(在承诺中)TypeError:无法获取.

任何人都知道原因可能是什么?

let CACHE = 'cache';

self.addEventListener('install', function(evt) {
    console.log('The service worker is being installed.');
    evt.waitUntil(precache());
});

self.addEventListener('fetch', function(evt) {
    console.log('The service worker is serving the asset.');
    evt.respondWith(fromCache(evt.request));
});
function precache() {
    return caches.open(CACHE).then(function (cache) {
        return cache.addAll([
            '/media/wysiwyg/homepage/desktop.jpg',
            '/media/wysiwyg/homepage/bottom2_desktop.jpg'
        ]);
    });
}
function fromCache(request) {
    return caches.open(CACHE).then(function (cache) {
        return cache.match(request).then(function (matching) {
            return matching || Promise.reject('no-match');
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

Bla*_*ard 16

我认为这是因为你没有后备策略.event.respondWith有一个承诺,如果有一些错误,你必须抓住.

所以,我建议你改变你的代码:

self.addEventListener('fetch', function(evt) {        
    console.log('The service worker is serving the asset.');
    evt.respondWith(fromCache(evt.request));
});                   
Run Code Online (Sandbox Code Playgroud)

对于这样的事情:

addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        if (response) {
          return response;     // if valid response is found in cache return it
        } else {
          return fetch(event.request)     //fetch from internet
            .then(function(res) {
              return caches.open(CACHE_DYNAMIC_NAME)
                .then(function(cache) {
                  cache.put(event.request.url, res.clone());    //save the response for future
                  return res;   // return the fetched data
                })
            })
            .catch(function(err) {       // fallback mechanism
              return caches.open(CACHE_CONTAINING_ERROR_MESSAGES)
                .then(function(cache) {
                  return cache.match('/offline.html');
                });
            });
        }
      })
  );
});          
Run Code Online (Sandbox Code Playgroud)

注意:有许多缓存策略,我在这里展示的是脱机第一种方法.欲了解更多信息是一个必须阅读.


小智 16

我找到了同一错误的解决方案,在我的情况下,错误显示当服务工作者找不到文件时,通过在chrome会话的开发工具中跟踪网络来修复它,并确定服务工作者找不到的不存在的文件并删除要注册的文件数组.

  '/processos/themes/base/js/processos/step/Validation.min.js',
  '/processos/themes/base/js/processos/Acoes.min.js',
  '/processos/themes/base/js/processos/Processos.min.js',
  '/processos/themes/base/js/processos/jBPM.min.js',
  '/processos/themes/base/js/highcharts/highcharts-options-white.js',
  '/processos/themes/base/js/publico/ProcessoJsController.js',
 // '/processos/gzip_457955466/bundles/plugins.jawrjs',
 // '/processos/gzip_N1378055855/bundles/publico.jawrjs',
 // '/processos/gzip_457955466/bundles/plugins.jawrjs',
  '/mobile/js/about.js',
  '/mobile/js/access.js',
Run Code Online (Sandbox Code Playgroud)

  • 哇,我花了几天时间来解决这个问题!它就像“.png”而不是“.jpg”SMH 一样简单。谢谢! (2认同)

小智 7

我遇到了同样的错误,在我的情况下,Adblock阻止了对以“ ad”开头的网址的提取(例如/adsomething.php)

  • 哈哈哈刚刚遇到同样的问题。使用 gitea 并有一个以“Ad”开头的回购。几乎所有页面都以这个异常结束,在我的 gitea 站点中停用 Adblock 解决了这个问题! (3认同)