Dabblernl
Dabblernl

Reputation: 16101

Blazor WebAsssembly client app does not refresh and does not use the latest code

How can I make sure that the users of a Blazor client app always load the latest version of the code? I now have to instruct them to clear their browser caches every time a new version is published. This hampers the "release early and often" approach and I simply cannot believe that any serious blazor development is at all possible without checking for new versions at start up (what click once applications do).

I found this solution on https://learn.microsoft.com/en-us/answers/questions/214424/blazor-wpa-not-updating.html

/**
      * Register Service Worker
      */
     navigator.serviceWorker
         .register('/***.js', { scope: '/' })
         .then(() => {
             console.log('Service Worker Registered');
         });

This js file can contain a cache version like below.

 const CACHE_VERSION = 1.0

But that is too cryptic for me? How can I implement the solution stated above as in "for dummies"?

Upvotes: 2

Views: 2385

Answers (1)

Dabblernl
Dabblernl

Reputation: 16101

The answer is (once you know it) quite simple: In the index.html under the wwwrootfolder you will find:

  <script>navigator.serviceWorker.register('service-worker.js');</script>

Below this write:

 <script>navigator.serviceWorker.register('version.js');</script>

And create a "version.js" file in this folder with as single content:

const CACHE_VERSION = 1.0

Increment the 1.0 value each time you want your clients to load the new version.

It looks like this:

Once your clients have managed to clear their cache and load the version that contains this code, a simple reload or shut down and restart of the browser will make the new version active.

Upvotes: 4

Related Questions