本文介绍了缓存优先 Service Worker:如何绕过更新资产的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景如下:

您有一个当前通过软件缓存的站点.您部署了一个新版本,其中包含具有缓存破坏版本的更新软件.该公司随后宣布了新功能.然而,人们访问该站点,即使软件破坏它仍然在后台更新其缓存时提供先前的缓存.因此,为新功能而来的访问者看不到它们.

You have a site that currently cached via a SW. You deploy a new version that includes an updated SW with a cache busting version. The company then announces the new features. People visit the site, however, even though the SW busts it still serves up the previous cache while updating its cache in the background. So visitors that come for the new features don't see them.

这是 ServiceWorkers 的预期体验吗?解决此问题的推荐策略是什么?

Is this the expected experience with ServiceWorkers? What are the recommended strategies to get around this?

推荐答案

每当您使用缓存优先策略提供资源时,这是预期的行为,是的.

It's the expected behavior whenever you serve resources with a cache-first strategy, yes.

有两种选择:

  • 不要使用 缓存优先 策略.不幸的是,如果您使用 网络优先 策略.如果您能提供帮助,我不建议您先使用网络.
  • 采用 UX 模式,在屏幕上显示重新加载以获取最新更新"的 Toast 消息,让用户知道缓存内容已刷新,并允许他们采取措施查看最新内容.我认为,这是最好的方法.如果您使用的 Service Worker 会在您的缓存内容发生变化时更新(例如,由 生成的sw-precache),然后您可以通过侦听特定的 Service Worker 控制器事件来检测这些更新,并使用这些事件来触发消息.(这是一个例子.)
  • Don't use a cache-first strategy. Unfortunately, you lose out on most of the performance benefits of service workers if you use a network-first strategy. I wouldn't recommend going network-first if you can help it.
  • Adopt the UX pattern of displaying a "Reload for the latest updates" toast message on the screen letting the user know that the cached content has been refreshed, and allowing them to take action to see the latest content. This is, I think, the best approach. If you're using a service worker which gets updated whenever your cached content changes (e.g., one generated by sw-precache), then you can detect these updates by listening for specific service worker controller events, and use those to trigger the message. (Here's an example.)

这篇关于缓存优先 Service Worker:如何绕过更新资产的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:03