如何使用 Workbox 实施 'Cache then network' 策略

How to implement a 'Cache then network' strategy using Workbox

我正在尝试使用 Workbox 实施 'cache then network' 策略。

我已将 Workbox 设置为使用 staleWhileRevalidate runtimeCaching 处理程序,但我不确定如何最好地更新页面 once/if 我们从网络更新的缓存。

文档说:

In addition to updating the appropriate caches, it will also trigger any appropriate plugins defined in the underlying RequestWrapper.

那有用吗?是否有使用 Workbox 完成此策略的示例(顺便说一句,这是一个很棒的工具,所以感谢它的维护者)?

https://workbox-samples.glitch.me/examples/workbox-broadcast-cache-update/

中有一个以 "standalone" 方式使用 BroadcastCacheUpdate 功能的示例

要通过 WorkboxSW 的路由将其与 staleWhileValidate 一起使用,您可以执行以下操作:

workboxSW.router.registerRoute(
  new RegExp('/some/path/prefix'),
  workboxSW.strategies.staleWhileRevalidate({
    cacheName: 'my-cache',
    broadcastUpdate: {
      channelName: 'my-update-channel'
    },
  })
);

您可以在 the docs 中查看完整示例。