Search code examples
angularangular-service-workerangular16

ask user to refresh after deploy build


i am using angular to create a SPA after every deploy i have to hard refresh the page to get the latest content I want to show a popup to user that new version is available you can refresh the browser I try to use service worker but stuck when user hard refresh the browser in this case I can't access my service worker functions like checkforUpdate() and versionUpdates() after hard refresh swUpdate.Enables gives me true but not able to access the functions how can I achieve this


Solution

  • Most likely a very temporary solution until someone else suggests something better.

    But you could add a file to your assets, like a JSON of some sorts with { "version": "xxx" } And have a interval running in your app component checking a local variable set at initial page load and check that against the remote URL of that file through the HttpClient. If there is a difference you can use a modal, alert or snackbar to display a notification to the user asking to refresh.

    As I said there are probably better ways, but if you need to have it implemented quickly this should do the trick. You will have to manually increment the version number each time you rebuild :(

    You could also maybe automate some of this through generating a UUID during build instead of a version number? Not sure how to do that though...