[Blazor WASM] Caching problem leading to service worker update problems
See original GitHub issueDescribe 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:
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:
- Created 2 years ago
- Reactions:3
- Comments:6 (4 by maintainers)
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.
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.