question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Blazor WASM] Caching problem leading to service worker update problems

See original GitHub issue

Describe the bug

Experimenting with the PWA option for Blazor WASM projects i was discovering problems with the default service worker implementation regaring the update mechanism:

Starting with a service worker version v1 installed and working, i am deploying a new service worker version v2. Opening the PWA, the onInstall event gets invoked, starting the caching/fetching process for the assets of v2.

During this process i was regularly observing integrity check errors for the files that had actually changed with the new version. I observed, that the integrity hashes shown in the error messages were actually from the old version v1. Checking the browser dev tools, i saw the AssetsManifest file “service-worker-assets.js” is being served by disk cache, inevitably leading to this error:

Screenshot 2021-12-14 091230

The specific project i am observing this behaviour was published with dotnet 5.0.7. I tried to reproduce the issue with a fresh project, but am not able to do so. This seems to be a general problem that can arise under certain conditions (which are currently unclear to me), so i suppose the problem is not actually specific to a dotnet version. From what i can see, for dotnet 6.0 the only thing that changed in the service worker implementation was how the requests are defined during onInstall, seemingly to avoid browser caching problems as well.

The underlying problem seems to be, that importing the AssetsManifest in the service worker is not guaranteed to be served the required file.

The question is, how would you solve this problem?

To Reproduce

As described, hard to reproduce. But it should be obvious how this problem can arise, although unclear under what specific conditions.

Further technical details

  • ASP.NET Core version: .Net 5.0
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version: Microsoft Visual Studio Professional 2019 Version 16.11.7
  • Include the output of dotnet --info: .NET SDK: Version: 6.0.100 Commit: 9e8b04bbff

Laufzeitumgebung: OS Name: Windows OS Version: 10.0.19041 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support): Version: 6.0.0 Commit: 4822e3c3aa

.NET SDKs installed: 5.0.102 [C:\Program Files\dotnet\sdk] 5.0.103 [C:\Program Files\dotnet\sdk] 5.0.403 [C:\Program Files\dotnet\sdk] 6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

dotnet --info Output

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
dvallmencommented, Dec 21, 2021

I did some further research on this issue. According to Fresher service workers, by default, the default behaviour (in this case for chrome) is to serve requests for “importScripts” inside a service worker (or any worker) from cache.

To change this behaviour, ServiceWorkerContainer.register() can be called with the “updateViaCache” option this way:

navigator.serviceWorker.register('/service-worker.js', {updateViaCache: 'none'});

Doing this, all requests for “importScripts” inside the service worker will be served from network. Finally this will resolve the issue described above.

I suggest, this should be the default way of registering the Blazor WASM PWA service worker.

0reactions
msftbot[bot]commented, Aug 15, 2022

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor Wasm PWA not updating - asp.net core
I know that the application cache should be updated by the service worker, so it could be that you need to visit the...
Read more >
Blazor Wasm PWA not updating - Microsoft Q&A
This issue is likely caused by the browser caching the old version of your Blazor WASM app. To fix this issue, you can...
Read more >
Blazor caching issues
I recently started noticing some caching issues on the WASM side, and I'm not talking about CSS or JS caching, but actual code...
Read more >
Handling Service Worker updates – how to keep the app ...
Not applying the Service Worker update might mean that our outdated Service Worker runs for ages and serves our users with assets from...
Read more >
Telerik UI for Blazor in a PWA—Easy as Pie
Curious about using Blazor WebAssembly in Progressive Web Apps? ... but the service worker caching gives you the old version without the fix...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found