javascript – 检测服务工作者中脱机状态的最佳实践

我有一个服务工作者,如果客户端没有网络连接,则应该缓存一个显示的offline.html页面.但是,它有时会认为导航器即使不是离线也是脱机的.也就是说,navigator.onLine === false.这意味着即使在线,用户也可能获得offline.html而不是实际内容,这显然是我想避免的.

这是我在main.js中注册服务工作者的方式:

// Install service worker for offline use and caching
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js', {scope: '/'});
}

我当前的service-worker.js:

const OFFLINE_URL = '/mysite/offline';
const CACHE_NAME = 'mysite-static-v1';

self.addEventListener('install', (event) => {
  event.waitUntil(
    // Cache the offline page when installing the service worker
    fetch(OFFLINE_URL, { credentials: 'include' }).then(response =>
      caches.open(CACHE_NAME).then(cache => cache.put(OFFLINE_URL, response)),
    ),
  );
});

self.addEventListener('fetch', (event) => {
  const requestURL = new URL(event.request.url);

  if (requestURL.origin === location.origin) {
    // Load static assets from cache if network is down
    if (/\.(css|js|woff|woff2|ttf|eot|svg)$/.test(requestURL.pathname)) {
      event.respondWith(
        caches.open(CACHE_NAME).then(cache =>
          caches.match(event.request).then((result) => {
            if (navigator.onLine === false) {
              // We are offline so return the cached version immediately, null or not.
              return result;
            }
            // We are online so let's run the request to make sure our content
            // is up-to-date.
            return fetch(event.request).then((response) => {
              // Save the result to cache for later use.
              cache.put(event.request, response.clone());
              return response;
            });
          }),
        ),
      );
      return;
    }
  }

  if (event.request.mode === 'navigate' && navigator.onLine === false) {
    // Uh-oh, we navigated to a page while offline. Let's show our default page.
    event.respondWith(caches.match(OFFLINE_URL));
    return;
  }

  // Passthrough for everything else
  event.respondWith(fetch(event.request));
});

我究竟做错了什么?

解决方法:

当您想要更新UI以指示您处于脱机状态时,navigator.onLine和相关事件非常有用,例如,只显示缓存中存在的内容.

但是我会避免编写依赖于检查navigator.onLine的服务工作者逻辑.相反,尝试无条件地进行fetch(),如果失败,则提供备份响应.这将确保您的Web应用程序按预期运行,无论fetch()由于脱机(由于lie-fi而导致)或由于您的Web服务器遇到问题而失败.

// Other fetch handler code...

if (event.request.mode === 'navigate') {
  return event.respondWith(
    fetch(event.request).catch(() => caches.match(OFFLINE_URL))
  );
}

// Other fetch handler code...
上一篇:javascript – Service Worker’fetch’事件当前正在提供的页面的URL


下一篇:JavaScript – 禁用互联网连接