即使离线,抓取也会在服务工作者中返回200

时间:2019-10-30 11:44:18

标签: caching fetch service-worker progressive-web-apps offlineapps

如果没有网络或互联网服务,我希望Angularjs(1.x)应用程序具有脱机版本作为后备。

我想要服务人员的fetch事件监听器,尝试从网络中获取主页(当request.mode =='navigate'时),如果失败则返回一个缓存版本。

由于某些原因,即使我断开了wifi的连接或以“飞机模式”运行,提取操作始终返回StatusCode 200 OK 。但是,如果我打开Chrome的DevTools的“网络>离线”模式,确实可以正常工作

...我尝试过的事情:

我尝试使用“ navigator.onLine”功能检测离线模式,但不可靠。

也试图清除缓存,但是什么也没有。仍然返回原始的“在线” html文档。

还尝试将“缓存控制:无存储,无缓存”标头传递给提取,结果相同。

self.addEventListener('fetch', function (event) {
  if (event.request.mode === 'navigate') {    
    event.respondWith(homeNetworkThenCache(event.request));
  } 
});

function homeNetworkThenCache(request){
  return fetch(request)
          .then(e => e)
          .catch(() => caches.match(offlineHomepageUrl));
}

预期的行为:

如果没有互联网连接,我希望获取的内容会进入“ catch”块,但始终会进入“ .then(e => e)” ...

..有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您正在寻找一个示例服务工作者,其 just 实现了脱机后备页面,则可以use this one,它还会执行启用导航预加载的功能(如果支持):

const OFFLINE_VERSION = 1;
const CACHE_NAME = 'offline';
// Customize this with a different URL if needed.
const OFFLINE_URL = 'offline.html';

self.addEventListener('install', (event) => {
  event.waitUntil((async () => {
    const cache = await caches.open(CACHE_NAME);
    // Setting {cache: 'reload'} in the new request will ensure that the response
    // isn't fulfilled from the HTTP cache; i.e., it will be from the network.
    await cache.add(new Request(OFFLINE_URL, {cache: 'reload'}));
  })());
});

self.addEventListener('activate', (event) => {
  event.waitUntil((async () => {
    // Enable navigation preload if it's supported.
    // See https://developers.google.com/web/updates/2017/02/navigation-preload
    if ('navigationPreload' in self.registration) {
      await self.registration.navigationPreload.enable();
    }
  })());

  // Tell the active service worker to take control of the page immediately.
  self.clients.claim();
});

self.addEventListener('fetch', (event) => {
  // We only want to call event.respondWith() if this is a navigation request
  // for an HTML page.
  if (event.request.mode === 'navigate') {
    event.respondWith((async () => {
      try {
        // First, try to use the navigation preload response if it's supported.
        const preloadResponse = await event.preloadResponse;
        if (preloadResponse) {
          return preloadResponse;
        }

        const networkResponse = await fetch(event.request);
        return networkResponse;
      } catch (error) {
        // catch is only triggered if an exception is thrown, which is likely
        // due to a network error.
        // If fetch() returns a valid HTTP response with a response code in
        // the 4xx or 5xx range, the catch() will NOT be called.
        console.log('Fetch failed; returning offline page instead.', error);

        const cache = await caches.open(CACHE_NAME);
        const cachedResponse = await cache.match(OFFLINE_URL);
        return cachedResponse;
      }
    })());
  }

  // If our if() condition is false, then this fetch handler won't intercept the
  // request. If there are any other fetch handlers registered, they will get a
  // chance to call event.respondWith(). If no fetch handlers call
  // event.respondWith(), the request will be handled by the browser as if there
  // were no service worker involvement.
});