Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor Wasm PWA not updating

I have a Blazor WASM PWA application that I have published on a hosting service on IIS. The problem that I have encountered is that sometimes the application doesn't update in the visitor's browser. I.e. they see an older version of the application.

I know that the application cache should be updated by the service worker, so it could be that you need to visit the website once, and then close the browser and visit it again to get the updated version. However, sometimes it looks like the cache just gets messed up and no matter what, the application will not update. The only solution I've found is to press F12, go to the Application tab and delete everything from there -> this will force the latest version of the application to be downloaded again.

Does anyone know what is going on and how can I fix this?

like image 588
ionrazvan Avatar asked Dec 11 '20 22:12

ionrazvan


People also ask

Is Blazor good for PWA?

Blazor WebAssembly is a standards-based client-side web app platform, so it can use any browser API, including PWA APIs required for the following capabilities: Working offline and loading instantly, independent of network speed.

Should I use Blazor server or Blazor WASM?

The Blazor Server hosting model offers several benefits: Download size is significantly smaller than a Blazor WebAssembly app, and the app loads much faster. -The app takes full advantage of server capabilities, including the use of . NET Core APIs.

Is Blazor WASM faster than JavaScript?

In essence, you can write C# code where you'd previously use JavaScript, and Blazor will compile that C# to WebAssembly to allow it to run in the browser. However, I was a little surprised to find that when I tested the speed of it versus the application written in JavaScript, it was noticeably slower.

Is Blazor a future?

Blazor, which conveniently is a short form of browser + razor, is a relatively new Microsoft web framework that was released in 2018. Similar to Angular, Blazor is both open source and free. The framework enables developers to build interactive and reusable web UI in existing web technologies like C# or HTML.


2 Answers

Declare a js file for registration in the navigator.serviceWorker object.

 /**
  * 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.

Update cache_version when the code changes to force the cache to refresh. Anytime the register *.js file changes, the browser updates the service worker to its new version.

 const CACHE_VERSION = 1.1;
like image 75
Michael Wang Avatar answered Oct 23 '22 03:10

Michael Wang


This answer is intended to complement Michael Wong's answer:

Blazor automatically updates to the newer version (at least in .NET 5, possibly earlier too). This happens when the application is first closed and restarted in ALL browser tabs including the installed app (which is another browser tab).

The following article helped me a lot:
https://learn.microsoft.com/en-us/aspnet/core/blazor/progressive-web-app?view=aspnetcore-5.0&tabs=visual-studio#installation-and-app-manifest-1

Also helpful:
https://www.eugenechiang.com/2021/05/22/forcing-reload-of-blazor-client-after-publishing-changes/

like image 20
Chris Berlin Avatar answered Oct 23 '22 03:10

Chris Berlin