Blazor WebAssembly relies on the browser to execute web requests. In fact, every call you make using HttpClient are executed using the fetch API (documentation) provided by the browser.

By default, the browser uses the Cache-Control header to know if a response should be cached and how long it should be cached. When there is no header in the response, the browser has its own heuristic. Sometimes, people adds a parameter in the query string with a random value to ensure the URL cannot be served from the cache. A better way to do it is to use the fetch cache control API to control the cache behavior.

Blazor WebAssembly allows to set the value of the cache control when executing a request. This means you can bypass the cache if needed by setting the right value in the request options. Available options are exposed by the BrowserRequestCache enumeration. As the values are the same as the one exposed by the fetch API, the documentation is exactly the same as the one on MDN. Here’s a summary of the available options:

Default: The browser looks for a matching request in its HTTP cache.
ForceCache: The browser looks for a matching request in its HTTP cache. If there is a match, fresh or stale, it will be returned from the cache. If there is no match, the browser will make a normal request, and will update the cache with the downloaded resource.
NoCache: The browser looks for a matching request in its HTTP cache. If there is a match, fresh or stale, the browser will make a conditional request to the remote server. If the server indicates that the resource has not changed, it will be returned from the cache. Otherwise the resource will be downloaded from the server and the cache will be updated. If there is no match, the browser will make a normal request, and will update the cache with the downloaded resource.
NoStore: The browser fetches the resource from the remote server without first looking in the cache, and will not update the cache with the downloaded resource.
OnlyIfCached: The browser looks for a matching request in its HTTP cache. Mode can only be used if the request’s mode is “same-origin”. If there is a match, fresh or stale, it will be returned from the cache. If there is no match, the browser will respond with a 504 Gateway timeout status.
Reload: The browser fetches the resource from the remote server without first looking in the cache, but then will update the cache with the downloaded resource.

#blazor #.net #asp.net core #web #webassembly

Bypass HTTP browser cache when using HttpClient in Blazor WebAssembly
28.75 GEEK