服务人员在获取数据时出现问题

莱昂内尔·科斯塔(Lionel dcosta)

我正在使用下面的脚本从服务工作者那里获取bootstrap.min.js文件,但是在网络面板中它显示了1个bootstrap.min.js请求,其他所有请求显示了2个请求我已经附上了屏幕截图,有人可以吗?帮我解决这个问题。

var CACHE_VERSION = 8;

var CURRENT_CACHES = {
  prefetch: 'prefetch-cache-v' + CACHE_VERSION,
  font: 'font-cache-v' + CACHE_VERSION
};

//安装服务人员

self.addEventListener('install', function(event) {
  var now = Date.now();

    var urlsToPrefetch = [
    '/themes/2.0/fluid/public/responsive/js/bootstrap.min.js'
    ];

    console.log('Handling install event. Resources to prefetch:', urlsToPrefetch);

    event.waitUntil(
        caches.open(CURRENT_CACHES.prefetch).then(function(cache) {
            var cachePromises = urlsToPrefetch.map(function(urlToPrefetch) {
                console.log("comes here23");
            var url = new URL(urlToPrefetch, location.href);
            url.search += (url.search ? '&' : '?') + 'cache-bust=' + now;

            var request = new Request(url, {mode: 'no-cors'});

            return fetch(request).then(function(response) {
              if (response.status >= 400) {
                throw new Error('request for ' + urlToPrefetch +' failed with status ' + response.statusText);
              }
              // Use the original URL without the cache-busting parameter as the key for cache.put().
              return cache.put(urlToPrefetch, response);
            }).catch(function(error) {
              console.error('Not caching ' + urlToPrefetch + ' due to ' + error);
            });
          });

          return Promise.all(cachePromises).then(function() {
            console.log('Pre-fetching complete.');
          });
        }).catch(function(error) {
        //  console.error('Pre-fetching failed:', error);
        })

    );
});

//从服务端获取请求

self.addEventListener('fetch', function(event) {

  event.respondWith(


    caches.match(event.request).then(function(response) {
      if (response) {
        console.log("comes here", event.request);
        return response;
      }
      console.log("no cache exists");

      return fetch(event.request).then(function(response) {
        return response;
      }).catch(function(error) {

        throw error;
      });

    })

  );
});

网络

莱昂内尔·科斯塔(Lionel dcosta)

遇到上述问题的任何人都可以使用此代码,这可能对某人有所帮助

var CACHE_NAME = 'cache-v1';

var REQUIRED_FILES = [
  '/themes/2.0/fluid/public/responsive/js/bootstrap.min.js'
];

安装

self.addEventListener('install', function(event) {
  // Perform install step:  loading each required file into cache
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        // Add all offline dependencies to the cache
        return cache.addAll(REQUIRED_FILES);
      })
      .then(function() {
        // At this point everything has been cached
        return self.skipWaiting();
      })
  );
});

启用

self.addEventListener('activate', function(event) {
  // Calling claim() to force a "controllerchange" event on navigator.serviceWorker
  event.waitUntil(self.clients.claim());
});

拿来

self.addEventListener('fetch', function(event) {
      const requestURL = new URL(event.request.url);
  if (/^(\/2.0\/)/.test(requestURL.pathname)) {
    event.respondWith(
        caches.open(CACHE_NAME)
        .then(function(cache) {
            return cache.match(event.request)
                .then(function(response) {
                    if (response) {
                        return response;
                    }
                    return fetch(event.request);
                });
        })
    );
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章