1

我尝试在我的服务人员上渲染一个离线页面,它在我的本地主机上运行良好,但显然它在我的实时服务器上不起作用,我没有收到任何错误

event.waitUntil(updateCache(event.request));

  event.respondWith(
    fetch(event.request).catch(function(error) {
      //console.log( ' Network request Failed. Serving content from cache: ' + error );

      //Check to see if you have it in the cache
      //Return error page if file is document file else return failed

      function fe(filename) {
         return filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2);
      }

      // Fetch the request extention 
      let extention = fe(event.request.url)

      if(extention === 'php' || extention === ''){
        console.log('rendering Offline')
        // match offline only if request is for a page not an asset
        return caches.match(offlineUrl);
      }else{
        console.log('Still searching: ' + extention)
        return caches.open('saving-nest').then(function (cache) {
          return cache.match(event.request).then(function (matching) {
            var report =  !matching || matching.status == 404?Promise.reject('no-match'): matching;
            return report
          });
        });  
      }      
    })
  );

在实时服务器上,不呈现离线页面,而是呈现当前请求

4

1 回答 1

0

确保最后一个'.' 实际上是 '.php' 它可能是 site.com/site 和 com/site 不匹配

if(extention === 'php' || extention === '')

所以改用这个,对我有用

if(extention === /php/.test(extention) || extention == 'htaccess' || extention === '' || /com/.test(extention)) 
于 2019-08-10T22:25:26.290 回答