繁体   English   中英

Progressive Web App Uncaught (in promise) TypeError: Failed to fetch

[英]Progressive web app Uncaught (in promise) TypeError: Failed to fetch

我开始学习 PWA(渐进式 Web 应用程序),但遇到问题,控制台“抛出”错误未捕获(承诺)类型错误:无法获取。

有谁知道可能是什么原因?

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');
        });
    });
}

我认为这是因为您没有后备策略。 event.respondWith带有一个承诺,如果出现错误,您必须捕获该承诺。

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

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

对于这样的事情:

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');
                });
            });
        }
      })
  );
});          

注意:缓存的策略有很多,我在这里展示的是离线优先的方法。 欲了解更多信息是一个必须阅读。

我找到了相同错误的解决方案,在我的情况下,当服务工作者找不到文件时显示错误* ,通过在 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',

*我为我加粗了解决方案......我从一个缓存文件开始,然后添加另一个......直到我得到一个错误的路径,同时定义范围 {scope: '/'} 或 {scope: ' ./'} - 由 lawrghita 编辑

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

就我而言,未找到要缓存的文件(检查网络控制台),这与相对路径有关,因为我使用的是 localhost,并且站点位于子目录中,因为我在 XAMPP 服务器上开发了多个项目。

所以我改变了

let cache_name = 'Custom_name_cache';

let cached_assets = [
    '/',
    'index.php',
    'css/main.css',
    'js/main.js'
];

self.addEventListener('install', function (e) {
    e.waitUntil(
        caches.open(cache_name).then(function (cache) {
            return cache.addAll(cached_assets);
        })
    );
});

到下面:注意 cached_assets 上的“./”

let cache_name = 'Custom_name_cache';

let cached_assets = [
    './',
    './index.php',
    './css/main.css',
    './js/main.js'
];

self.addEventListener('install', function (e) {
    e.waitUntil(
        caches.open(cache_name).then(function (cache) {
            return cache.addAll(cached_assets);
        })
    );
});

在添加或获取任何路径(如/offline.html/main.js之前尝试使用/

缓存文件引用应该是正确的,否则获取将失败。 即使一个引用不正确,整个提取也会失败。

let cache_name = 'Custom_name_cache';


let cached_files = [
    '/',
    'index.html',
    'css/main.css',
    'js/main.js'
]; 
// The reference here should be correct. 

self.addEventListener('install', function (e) {
    e.waitUntil(
        caches.open(cache_name).then(function (cache) {
            return cache.addAll(cached_files);
        })
    );
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM