# Prevent Browser Caching of Telerik CSS and JavaScript Files

## Environment

<table>
    <tbody>
        <tr>
            <td>Product</td>
            <td>UI for Blazor</td>
        </tr>
    </tbody>
</table>

## Description

This KB article answers the following questions:

* How to prevent browser caching issues with the Telerik CSS theme and JSInterop (JavaScript) file when using static assets from the Telerik NuGet packages?
* How to implement a version-dependent cache buster for the `all.css` and `telerik-blazor.js` files?
* How to disable the browser cache for Telerik client assets when upgrading the Telerik UI for Blazor components?

## Solution

To avoid browser caching issues when upgrading the Telerik UI for Blazor version, consider one of the following options:

* Serve the Telerik CSS and JS files through the so-called [Map Static Assets routing endpoint conventions](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/map-static-files) which requires:
  * .NET 9 or later
  * `app.MapStaticAssets();` in `Program.cs`
  * [`@Assets["..."]` in `App.razor`](https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-9.0#deliver-assets-with-map-static-assets-routing-endpoint-conventions)
* Use the so-called [cache busting](https://www.telerik.com/blazor-ui/documentation/knowledge-base/common-browser-cache-buster#cache-busting). Add the components' version number to the Telerik client asset URLs as a query string. In this way, the browser will always load the correct version of the CSS stylesheet and the JSInterop file. Browsers will still use cached Telerik client assets as long as the components version stays the same.

Using the correct client assets [avoids Telerik-related JavaScript errors](https://www.telerik.com/blazor-ui/documentation/troubleshooting/js-errors).

> caption Map Static Telerik Assets in .NET 9 or later

```RAZOR.skip-repl
<link href="@Assets["_content/Telerik.UI.for.Blazor/css/kendo-theme-default/all.css"]" rel="stylesheet" />

<script src="@Assets["_content/Telerik.UI.for.Blazor/js/telerik-blazor.js"]"></script>
```

## Cache Busting

Cache busting varies depending on the Blazor application:

* [Blazor Web Apps and legacy Blazor Server apps](https://www.telerik.com/blazor-ui/documentation/knowledge-base/common-browser-cache-buster#blazor-web-apps-and-legacy-blazor-server-apps)
* [Standalone Blazor WebAssembly apps and Hybrid apps](https://www.telerik.com/blazor-ui/documentation/knowledge-base/common-browser-cache-buster#standalone-blazor-webassembly-apps-and-hybrid-apps)

Normally, there is no need for cache busting when [using the Telerik CDN](https://www.telerik.com/blazor-ui/documentation/common-features/cdn), because the client asset URLs are unique for every Telerik UI for Blazor version.

### Blazor Web Apps and Legacy Blazor Server Apps

You can use reflection to get the Telerik UI for Blazor version at runtime.

1. Pick a type (class) from the Telerik UI for Blazor product. A good candidate is a component that exists in both old and new product versions, such as the [`TelerikRootComponent`](https://www.telerik.com/blazor-ui/documentation/components/rootcomponent/overview).
2. Get the component type with `typeof(TelerikRootComponent)`. You may need to use `typeof(Telerik.Blazor.Components.TelerikRootComponent)` if:
   * The [`Telerik.Blazor.Components` namespace is not registered in `_Imports.razor` as it should](https://www.telerik.com/blazor-ui/documentation/getting-started/workflow-details#namespaces).
   * The Telerik CSS and JS assets are placed in a `.cshtml` file instead of `App.razor`, for example, in legacy Blazor apps.
3. Use the [`Assembly.GetName()` method](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getname?view=net-8.0) and the [`AssemblyName.Version` property](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assemblyname?view=net-8.0#properties) to extract the Telerik UI for Blazor version.

> caption Adding a cache buster for the Telerik CSS and JavaScript files through reflection

```HTML.skip-repl
@{ var telerikUiForBlazorVersion = typeof(TelerikRootComponent).Assembly.GetName().Version; }

<link href="_content/Telerik.UI.for.Blazor/css/kendo-theme-default/all.css?@telerikUiForBlazorVersion" rel="stylesheet" />

<script src="_content/Telerik.UI.for.Blazor/js/telerik-blazor.js?@telerikUiForBlazorVersion"></script>
```

### Standalone Blazor WebAssembly Apps and Hybrid Apps

If the Telerik CSS theme and JavaScript file reside in the `index.html` file, you can hard-code the Telerik UI for Blazor version. In this case, it is crucial to update the query string manually every time when upgrading.

> caption Adding a cache buster for the Telerik CSS and JavaScript files in index.html

```HTML.skip-repl
<link href="_content/Telerik.UI.for.Blazor/css/kendo-theme-default/all.css?16.0.0" rel="stylesheet" />

<script src="_content/Telerik.UI.for.Blazor/js/telerik-blazor.js?16.0.0"></script>
```

## Notes

You can also [use a `defer` attribute to load the `telerik-blazor.js` file asynchronously](https://www.telerik.com/blazor-ui/documentation/getting-started/workflow-details#javascript-file) and improve the client-side app performance.

## See Also

* [Adding the Telerik CSS and JavaScript files to a Blazor app](https://www.telerik.com/blazor-ui/documentation/getting-started/workflow-details#css-theme-and-javascript-files)
