To prevent caches to not update, when a new version of the Blazor Server app .NET 8 is deployed, we use the created version number. In the app.razor file, the includes look like this:
<link href="@($"_content/Blazor.Bootstrap/blazor.bootstrap.css{CacheVersionParam}")" rel="stylesheet" />The idea is taken from here: https://www.peug.net/en/blazor-cache-busting-tip-for-my-css-files/
This works great, but we use a lot of isolated javascript, loaded directly in the component, which (after some testing we found out) is still very much cached. What I found so far:
Microsoft answers
Unfortunately, the Microsoft documentation https://learn.microsoft.com/en-gb/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-8.0 (chapter "Cached JavaScript files") is basically non-existent, as it only speaks about development time and generically response headers. I've also found this issue https://github.com/dotnet/aspnetcore/issues/49130, but this seems to be on a per-component-basis.
Cache-busting of blazor.web.js
Some sources (like f.e. https://docs.telerik.com/blazor-ui/knowledge-base/common-browser-cache-buster#standalone-blazor-webassembly-apps-and-hybrid-apps) indicate to me, that it might be enough to just have the cache-buster on the main file _framework/blazor.web.js. Some other sources (also ChatGPT) indicate, that this doesn't work for isolated javascript.
Cache-busting on per javascript-file via loading
ChatGPT insists, that the way to go is to add a query param when loading the javascript in the component. Like this:
@code { protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { // Dynamically load JavaScript with cache busting await JSRuntime.InvokeVoidAsync("import", $"./_content/YourAssemblyName/your-script.js?v={DateTime.Now.Ticks}"); } }}This would be quite reasonable, as loading the file path could be easily done in a central manner. But I didn't find any other source, which goes with that way nor any official documentation regarding that topic.
Is this topic is quite tricky to track down, I'm on a dead end regarding the wisdom of the internet. Is there any official (or well tested) source, which has a solution for cache busting with isolated javascript? I would guess this problem comes up as soon as an app uses the isolated javascript features? To be clear: a lot of sources speak about SSR and web assembly, but we use Blazor Server without SSR.