我无法弄清楚为什么当一条路由的网络中断时,Workbox不能从缓存中检索,而对所有其他路由都有效吗?

时间:2019-06-07 21:01:31

标签: workbox workbox-webpack-plugin

我正在使用workbox-webpack-plugin,并且已经成功设置了我的服务工作者,因为它在角度前端和python后端的不同缓存中缓存了图像,css,js和我的大多数api响应

有一种路由永远无法从缓存中正确检索api响应。我不断收到错误消息

Uncaught (in promise) no-response: The strategy could not generate a response for 'http://127.0.0.1:8080/challenges/?is_exclusive=false&is_published=true&consolidate_groups_with_location=true&groups=9413,9404,9413&location_scope=-1&location_continent_id=5&location_country_id=235&location_state_province_id=44&location_region_id=194&location_city_id=14009&ends_at=2019-06-07%2020:54:38'. The underlying error is TypeError: Failed to fetch.
at StaleWhileRevalidate.makeRequest (https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-strategies.dev.js:1005:15)

该页面的api响应存储在缓存存储中,并具有呈现信息所需的信息。我的所有其他页面都可以工作,我不知道为什么。对于处理该错误需要做的事情,我也一无所知。

我尝试使用此代码,但事件监听器似乎没有触发。

self.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("api-local-cache")
              .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('api-local-cache')
              .then(function(cache) {
                return cache.match('/users/me');
              });
          });
      }
    })
);

});

这是我的工作箱策略

workbox.routing.registerRoute(
     new RegExp('http://127.0.0.1:8080'),
     new workbox.strategies.StaleWhileRevalidate({
      cacheName: "api-local-cache"
    })
  );

我希望看到我的服务人员检索存储的api响应,但没有,并说无法获取。

0 个答案:

没有答案