0

Let's say there is a static web site. I want to use service worker and cache responses from each request. When a user clicks every possible link, a website is fully cached and it doesn't have a connection with a server. Everything is ok, but if I change some files and change cache name to say service worker to update the cache, I should wait 24 hours before service worker is updated. How can I force update the service worker? I know about this method:

    SWRegistration.update();

But I don't know when my files will be changed. I can set interval and update sw every 1 hour for example, but I don't think it's a good idea. And I can't pass any parameter from a server, cause the site doesn't have access to a server (all data are in cache). Is there some practice how can it be resolved?

Alexandr Rypun
  • 143
  • 2
  • 10
  • Possible duplicate of [Service worker JavaScript update frequency (every 24 hours?)](https://stackoverflow.com/questions/38843970/service-worker-javascript-update-frequency-every-24-hours) – Jeff Posnick Sep 11 '17 at 13:09
  • Not completely sure I follow you. You mean: on day 1 user opens the website and SW is registered + whole site is cached. On day 17 the user opens the site again being offline - now the cached version is shown to the user from the SW. Between these two visits, say on day 9, you update the site with new content. Are you looking for a way to have the client get the new stuff from day 9 and show it to the user on day 17 *without* the user ever opening the site between days 1 and 17? – pate Sep 11 '17 at 17:02
  • No, service worker will be updated from a server after 24h and all content will be re-cached, if it's changed. But I want to force update sw, after the content is changed (early than in 24h). – Alexandr Rypun Sep 12 '17 at 11:27
  • No, I don't think that's possible. You could leverage the Web Push API to message the clients since the SW is responsible for handling the messages. Then the message handler could do some sort of cache busting logic instead of showing a notification to the user. Haven't tried this myself but I feel it could be possible - this of course adds a bunch of completely unrelated complexity to the app. – pate Sep 12 '17 at 13:52

1 Answers1

0

You can have a look at these or use SW Toolbox. It handles the offline strategy of your static files to whether its network first or cache first. This way it solves how your SW updates

oninross
  • 989
  • 6
  • 23