Search code examples
workbox

How to implement the "cache then network" strategy with workbox-cli?


How to implement the "cache then network" strategy with workbox-cli?

I need up to date content without leaving the website and revisiting it again (which is the behavior with the "stale while revalidate" strategy, which is the one used in workbox-cli if I understood well).

And I need workbox-cli because not only it handles the writing of the service worker file, but above all it takes care of the cache renaming after each build, which would be a hassle to rename manually.


Solution

  • There are a few different ways of using workbox-cli, depending on your configuration.

    Some of this boils down to the difference between precaching and runtime caching. There's some background on that distinction in this blog post.

    If you're using workbox-cli to precache all of your content, and you want to automatically reload your web app whenever there's an update detected to any of the precached content, you could follow the example in this "Offer a page reload for users" recipe, but call window.location.reload() unconditionally, instead of waiting for the user to confirm that they want to update.