Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the weather samples in FetchData seem to get cached for the sample Blazor app?

The Blazor app in Visual Studio uses a Http.GetFromJsonAsync call to get the data for Weather Forecasts from a json file in wwwroot.

When I change the data in the file, I still see the same data in the table?

When I copy the file, and change the code to use the new filename, I get the changed results.

Is there some caching happening with wwwroot files? I've tried hard refresh, that doesn't make a difference, but changing browser does. I know that Blazor caches the framework files...but is this happening to all wwwroot, how do I change this behaviour?

Thanks in advance.

like image 504
gabs Avatar asked Oct 14 '25 08:10

gabs


1 Answers

The fetchdata sample page (from new blazorwasm) retrieves data on initialize component:

protected override async Task OnInitializedAsync()
{
    forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
}

When you go out of this page and come back, initialize is running again and a request is done.

But, because this is a GET request, the browser can deliver answer from cache:

enter image description here

They are some ways to avoid cache on Blazor GET requests, learn about it here: Bypass HTTP browser cache when using HttpClient in Blazor WebAssembly

Also, you can use the simple trick to add a random string to query string:

protected override async Task OnInitializedAsync()
{
    var randomid = Guid.NewGuid().ToString();
    var url_get = $"sample-data/weather.json?{randomid}";
    forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>(url_get);
}

enter image description here

In short, it seems to get cached because a get request can be cached by browser and is the browser who retrieve the data.

like image 64
dani herrera Avatar answered Oct 18 '25 00:10

dani herrera