I am using React+Service Worker+offline-plugin to create a Service Workerwith persistent caching for the website.
I want to tell the user when a new version is stored in cach and suggest to refresh the page but I don't know how to reference the Service Workerand what event I should listen to (Service Workeris created automatically by npm "offline-plugin").
Standard flow today:
- Webmaster publish a site (V1)
- User visits the website
- He sees the website (V1) while Service Worker stores pages in persistent cache
- Webmaster publishes a new version (V2)
- User revisits the site seeing old version from persistent cache while web worker loading new version in the background (V2).
- When the user will refresh the page he will see website version 2
New flow should be:
- V2 loaded done in the background
- A popup message tels the user to refresh the page to get the new version.
Service worker is a specialized form of a webworker.
When you registry a sw, like for example in serviceWorkerRegistry.js, you'll have access to several events.
e.g.
Now the problem with offline plugin, is that it creates the sw for you, and in that way is harder to understand what the sw is doing.
I would say, it's better to create your own sw, this way you can understand better the functionality. this plugin can help you with the cache.