Blazor WASM PWA not returning static files from wwwroot on first request after publish due to service worker cache
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
I have a image placed inside wwwroot\images\image.png
in my blazor wasm
app. After publishing, when I request the image for the first time in browser with URL https://localhost/images/image.png
browser gets 404
response. Now if I press Shift
+ F5
the image is getting served in browser. I think blazor routing has some issues with explicit static file request handling. I’m not sure. Please can anyone assist me?
Expected Behavior
The requested static file from wwwroot\images\image.png
should be served instead of 404
or without pressing Shift
+ F5
Steps To Reproduce
- Create new
.net 6 blazor wasm
app. - Add a image file inside
wwwroot\images\image.png
. - Publish the app.
- Now run the published version and request the
image.png
viahttps://localhost:port/images/image.png
. - You should get
404
in browser. - Now press
Shift
+F5
, image will be served.
Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
.NET SDK (reflecting any global.json):
Version: 6.0.101
Commit: ef49f6213a
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.101\
Host (useful for support):
Version: 6.0.1
Commit: 3a25a7f1cc
.NET SDKs installed:
6.0.101 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:27 (15 by maintainers)
Top Results From Across the Web
Blazor wasm not serving static files from wwwroot after ...
This seems to be an issue with service-worker.published.js file coming with blazor wasm pwa template. This has been reported in ...
Read more >ASP.NET Core Blazor Progressive Web Application (PWA)
published.js service worker resolves requests using a cache-first strategy. This means that the service worker prefers to return cached content, ...
Read more >Blazor Wasm PWA not updating - Microsoft Q&A
To fix this issue, you can try the following solutions: Add a cache busting mechanism to your app: This involves appending a unique...
Read more >PWA static assets never refresh unless cache is cleared ...
This has been a persistent problem during the development of our PWA. The static assets do not refresh after a new version is...
Read more >UI/Blazor/Pwa Configuration | Documentation Center | ABP.IO
The service-worker.published.js file, which is used after the app is published. Caches certain file extensions and supports offline scenarios by ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@javiercn and @brischt I have made below changes in the service-worker.js
From this,
const shouldServeIndexHtml = event.request.mode === 'navigate';
to this,
const shouldServeIndexHtml = event.request.mode === 'navigate' && new URL(event.request.url).pathname === '/';
This now loads the correct content without force reload. @javiercn Please can you help to validate. The issue seems to be closed without resolution.
@fingers10 I would suggest you check the response here:
It seems that its coming from the service worker installed in the app, so I would check the service worker code to understand what’s happening.
It can be that somehow the image isn’t being correctly cached by the service worker or that a new version of the service worker needs to be updated on the browser and that’s why you are observing this behavior.
You might need to update your logic in https://github.com/dotnet/aspnetcore/blob/main/src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client/wwwroot/service-worker.published.js#L41-L59