I'm working on a Blazor 8 application and I'm having trouble with browser caching of my static files. When I make changes to the CSS files, the changes are not immediately visible because the browser serves the old version of the file from cache.
In ASP.NET Core, I would use the asp-append-version
tag helper to append a version query string to the URL of the CSS or JS file. This version query string changes whenever the file changes, forcing the browser to download the new version of the file instead of serving the old one from cache.
In Blazor and .NET 8, the general page format is defined in the App.razor
file. Here's what my App.razor
file looks like:
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="stylesheet" href="fonts/font.css">
<link rel="stylesheet" href="css/bargeh.css">
<link rel="stylesheet" href="css/header.css">
<link rel="stylesheet" href="css/footer.css">
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/header.js" defer></script>
<script src="js/bargeh.js" defer></script>
<HeadOutlet />
</head>
<body>
<Routes />
@* ReSharper disable Html.PathError *@
<script src="_framework/blazor.web.js"></script>
@* ReSharper restore Html.PathError *@
</body>
</html>
As you can see, I'm referencing several CSS and JS files in the head
section of the HTML document. I tried manually appending a version query string to the URLs of these files, like this:
<link rel="stylesheet" href="css/bargeh.css?v=1.0.0">
But this approach requires me to manually increment the version number each time I make changes to the CSS file, which is not ideal.
Moreover, my application might use server-side rendering (SSR), server, WebAssembly (WASM), or auto render mode. So, I need a workaround that works for all of these.
Is there a way to automatically append a version query string to the URLs of CSS files in the App.razor
file of a Blazor.NET 8 application, similar to how asp-append-version
works in ASP.NET? Any help would be greatly appreciated. Thank you!
builder.Services.AddMvc()
is okay in case of performance? Can you explicitly mention the service that you need to add? – Stalky