【问题标题】:How to make the Service Worker cache data from API and update the cache when needed如何让 Service Worker 从 API 缓存数据并在需要时更新缓存
【发布时间】:2020-07-11 17:55:52
【问题描述】:

我将我的 React 应用程序转换为 PWA,它部分工作正常。

我遵循了这个教程:https://medium.com/@toricpope/transform-a-react-app-into-a-progressive-web-app-pwa-dea336bd96e6

但是这篇文章只展示了如何缓存静态数据,我还需要存储来自服务器的数据,我可以按照这篇文章的第一个答案的说明进行操作:How can I cache data from API to Cache Storage in React PWA? 并在其中插入 firebase 地址数据存储在数组urlsToCache 中,由应该存储到缓存中的文件填充。

到目前为止一切顺利,但是在将数据存储到缓存中之后,应用程序停止从服务器获取数据并仅使用缓存中的数据加载页面,即使服务器已更新。这是我需要解决的问题。

简而言之,我需要从服务器获取数据,将其存储到缓存中,以便在应用程序离线时使用它,并在每次到达服务器时更新缓存。

我正在尝试遵循本指南,但没有成功:https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#serving-suggestions

这是我的 worker.js 文件:

var CACHE_NAME = 'pwa-task-manager';
var urlsToCache = [
  '/',
  '/completed',
  '/index.html',
  '/static/js/main.chunk.js',
  '/static/js/0.chunk.js',
  '/static/js/1.chunk.js',
  '/static/js/bundle.js',
  '/calculadora',
  'https://calc-marmo.firebaseio.com/clientes.json',
  'https://calc-marmo.firebaseio.com/adm.json',
];

// Install a service worker
self.addEventListener('install', event => {
  // Perform install steps
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
      })
  );
});

// Cache and return requests
self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        // Cache hit - return response
        if (response) {
          return response;
        }
        return fetch(event.request);
      }
    )
  );
});

// Update a service worker
self.addEventListener('activate', event => {
  var cacheWhitelist = ['pwa-task-manager'];
  event.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(
        cacheNames.map(cacheName => {
          if (cacheWhitelist.indexOf(cacheName) === -1) {
            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript reactjs progressive-web-apps service-worker


    【解决方案1】:

    这听起来像是你需要一个网络优先策略,这在食谱中没有提到。此策略类似于网络回退到缓存,但额外将响应始终存储在缓存中。

    解释:https://developers.google.com/web/tools/workbox/modules/workbox-strategies#network_first_network_falling_back_to_cache

    代码示例(如果您不使用工作箱):https://gist.github.com/JMPerez/8ca8d5ffcc0cc45a8b4e1c279efd8a94

    【讨论】:

    • 我一直在挖掘,我可以看到,我将视角更改为Network First,它在 localhost 上运行良好,但是在真实主机上我只获取静态数据,来自 Firebase 的数据是没有被缓存,也许问题是我如何配置它,我不知道。我也试过你发布的这个例子,但我遇到了同样的问题。作为一个选项,我将静态数据存储到缓存中,而我正在使用indexedDB 存储动态数据。如果您知道我为什么会遇到这个问题,我很想听听,因为在这种情况下,我宁愿使用缓存而不是 IDB,如果没有,无论如何,谢谢您的宝贵时间。
    • Firebase 的实时数据库使用 websocket 与客户端进行通信。据我所知,服务工作者不支持 WebSocket。因此,您必须将 Firebase 数据存储在您提到的 IndexedDB 中。 (我就是这样做的)
    • 你可以分享一个代码sn-p @你是怎么做到的
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多