【问题标题】:PWA: Chrome warning "Service worker does not have the 'fetch' handler"PWA:Chrome 警告“Service Worker 没有 'fetch' 处理程序”
【发布时间】:2018-12-19 20:27:38
【问题描述】:

我目前未能成功安装我的 PWA。我已经注册了一个 SertviceWorker 并链接了一个清单,并且我正在监听 beforeInstallPromt 事件。

我的 ServiceWorker 正在监听任何 fetch 事件。

我的问题是,创建的 beforeInstall 横幅仅显示在 Chrome 桌面上,但在移动设备上,我在“清单”部分的 Chrome 检查选项卡“应用程序”中收到警告:

Installability
Service worker does not have the 'fetch' handler

您可以查看https://dev.testapp.ga/的消息

window.addEventListener('beforeinstallprompt', (e) => {
// Stash the event so it can be triggered later.
deferredPrompt = e;
mtShowInstallButton();
});

manifest.json

{"name":"TestApp","short_name":"TestApp","start_url":"https://testapp.ga/loginCheck","icons":[{"src":"https://testapp.ga/assets/icons/launcher-ldpi.png","sizes":"36x36","density":0.75},{"src":"https://testapp.ga/assets/icons/launcher-mdpi.png","sizes":"48x48","density":1},{"src":"https://testapp.ga/assets/icons/launcher-hdpi.png","sizes":"72x72","density":1.5},{"src":"https://testapp.ga/assets/icons/launcher-xhdpi.png","sizes":"96x96","density":2},{"src":"https://testapp.ga/assets/icons/launcher-xxhdpi.png","sizes":"144x144","density":3},{"src":"https://testapp.ga/assets/icons/launcher-xxxhdpi.png","sizes":"192x192","density":4},{"src":"https://testapp.ga/assets/icons/launcher-web.png","sizes":"512x512","density":10}],"display":"standalone","background_color":"#ffffff","theme_color":"#0288d1","orientation":"any"}

服务工作者:

//This array should NEVER contain any file which doesn't exist. Otherwise no single file can be cached.
var preCache=[
  '/favicon.png',
  '/favicon.ico',
  '/assets/Bears/bear-standard.png',
  '/assets/jsInclude/mathjax.js',
  '/material.js',
  '/main.js',
  'functions.js',
  '/material.css',
  '/materialcolors.css',
  '/user.css',
  '/translations.json',
  '/roboto.css',
  '/sw.js',
  '/'
];
//Please specify the version off your App. For every new version, any files are being refreched.
var appVersion="v0.2.1";
//Please specify all files which sould never be cached
var noCache=[
  '/api/'
];

//On installation of app, all files from preCache are being stored automatically.
self.addEventListener('install', function(event) {
  event.waitUntil(
      caches.open(appVersion+'-offline').then(function(cache) {
          return cache.addAll(preCache).then(function(){
            console.log('mtSW: Given files were successfully pre-cached')
          });
      })
  );
});

function shouldCache(url) {
    //Checking if url is market as noCache
    var isNoCache=noCache.includes(url.substr(8).substr(url.substr(8).indexOf("/")))||noCache.includes((url.substr(8).substr(url.substr(8).indexOf("/"))).substr(0,(url.substr(8).substr(url.substr(8).indexOf("/"))).indexOf("?")));
    //Checking of hostname of request != current hostname
    var isOtherHost=url.substr(8).substr(0,url.substr(8).indexOf("/"))!=location.hostname&&url.substr(7).substr(0,url.substr(7).indexOf("/"))!=location.hostname;
    return((url.substr(0,4)=="http"||url.substr(0,3)=="ftp") && isNoCache==false && isOtherHost==false);
}
//If any fetch fails, it will look for the request in the cache and serve it from there first
self.addEventListener('fetch', function(event) {
  //Trying to answer with "online" version if fails, using cache.
  event.respondWith(
    fetch(event.request).then(function (response) {
      if(shouldCache(response.url)) {
        console.log('mtSW: Adding file to cache: '+response.url);
        caches.open(appVersion+'-offline').then(function(cache) {
        cache.add(new Request(response.url));
    });
      }
      return(response);
    }).catch(function(error) {
      console.log( 'mtSW: Error fetching. Serving content from cache: ' + error );

      //Check to see if you have it in the cache
      //Return response
      //If not in the cache, then return error page
      return caches.open(appVersion+'-offline').then(function (cache) {
        return cache.match(event.request).then(function (matching) {
          var report =  !matching || matching.status == 404?Promise.reject('no-match'): matching;
          return report
        });
      });
    })
  );
})

我检查了mtShowInstallButton 函数。它完全可以在桌面上运行。

这是什么意思?在桌面上,我从来没有收到过这个警告,只是在使用手持设备/模拟器时。

【问题讨论】:

  • 你能编辑你的问题并添加清单和服务工作者代码吗?这对任何想帮助你的人都很有用。

标签: google-chrome service-worker progressive-web-apps lighthouse


【解决方案1】:

Fetch 函数用于获取 JSon 清单文件。再次尝试阅读谷歌文档。 要在 Mobile 中添加 PWA,您需要获取清单文件,该文件是使用 service-worker 使用 fetch 函数获取的。

代码如下:

    fetch('examples/example.json')
.then(function(response) {
  // Do stuff with the response
})
.catch(function(error) {
  console.log('Looks like there was a problem: \n', error);
});

有关获取和清单的更多信息try this

【讨论】:

  • 好主意,但我的清单是通过数据 URI 加载的 base64 编码的。所以它实际上不是由我的 Service Worker 获取的。
  • 为什么是base64 uri?您必须将清单文件与 sw.js 保存在同一文件夹中。这意味着服务工作者没有任何清单文件。尝试与服务人员保持身体接触。 js。如果你没有得到这个,也试试我关于 pwa 的文章。 medium.com/@amitgandole
  • 清单是客户端 JS 生成的,因为它取决于用户的设备和偏好。
  • @JasMich.de 你有没有找到解决方案?我在完全相同的情况下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 2018-07-19
  • 2023-04-01
  • 2017-02-05
相关资源
最近更新 更多