6

Here is the scenario:

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.

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

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
bcardarella
  • 4,667
  • 4
  • 29
  • 45

1 Answers1

10

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

There are two options:

  • 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.)
Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167