Is there an easier way to determine that a Blazor App (PWA) has an update available?

394 Views Asked by At

When I look for examples for determining that a Blazor App (PWA) has an update available I find many lengthy examples focusing on service-worker.js rewrites. Is there a easier way? Or a way that leaves the service-worker.js intact?

2

There are 2 best solutions below

0
On

I have used an approach for the similar purpose, which is to associate a version number with your application (both Client and Server API applications). You can write some code in the MainLayout.razor page's Initialize method to check the current version from the server (API) application. If the current version from the server doesn't matches the version on the Client (Blazor WebAssembly) application, that means the application needs to be updated.

0
On

So, my understanding of a Blazor application is you are downloading a compiled application, which is a response that you would cache using the service worker.

So as with any response object, HTML, JS, CSS, image, Blazor blob, does not matter, I generally have some sort of logic in my service worker to check for updates.

A general technique I use in my PWAs is to trigger some sort of workflow to make HEAD requests to see if stale objects have been updated on the server. If they have been updated then I fetch and replace the cached asset.

In your case, since the UX is a black box that would not communicate directly to the service worker you would either wait till the user closes the current session and comes back before the new version is used. Or, send a message to the actual UI layer you catch with client-side JavaScript to display some sort of prompt to trigger a page refresh once the user confirms they want to use the newer version.