Static files such as CSS, JS, and even images can be cached on the client’s disk. However, it is important that the web server says that it should cache or not, and for how long. Many web servers have the specific configuration for each file type.
In the case of IIS express you must use web.conf to make such settings so that the server delivers the headers correctly to the client(browser) to cache accordingly. This cache is controlled by the Cache-Control header.
If you want to know more go to https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching ( link in English).
To set up the cache via the local Web.config file, which is found in the content directory you must check the tag <caching>
.
Below is a sample of the configuration required for the js and css files for 7 days, which means instructing the browser to cache all js and css files for 7 days.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<caching enabled="true" enableKernelCache="true">
<profiles>
<add extension=".css" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="07:00:00"/>
<add extension=".js" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="07:00:00"/>
</profiles>
</caching>
</system.webServer>
</configuration>
It is still possible to set a cache for all static files without distinction:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>
</configuration>
The best policy for caching files is to only modify the file when it is changed. This increases performance because it does not cause the file to be downloaded again, the server will control it internally. For that we will use the policy
CacheUntilChange
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<caching enabled="true" enableKernelCache="true">
<profiles>
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange"/>
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange"/>
</profiles>
</caching>
</system.webServer>
</configuration>
To know the variations of each key, combinations of attributes and what is allowed and other examples access the documentation: https://www.iis.net/configreference/system.webserver/caching
You could include what Google said about your site in the question ("Setting an expiration date or maximum age in HTTP headers for static resources instructs the browser to load previously downloaded resources from the local disk and not over the network") I would do that, but I always consider it best that the author himself put information of this kind.
– Oralista de Sistemas
Renan, that’s basically the point of the question, I can’t find where to set it up.
– Leonardo Bonetti
There are several ways to get what you want. I found some in the ONLY.
– Oralista de Sistemas
My suggestion: browsers currently already do this control if you don’t give any indication in your code, so I wouldn’t worry about it now - unless you have traffic or cargo issues that you need to resolve now.
– Oralista de Sistemas
Regardless of my suggestion above, +1 because I would like to have a canonical answer here. It is worth reward to those who answer (remind me).
– Oralista de Sistemas
Possible duplicate of Save or not cache browser?
– UzumakiArtanis
I want to do this through ASP.NET and not directly in HTML as in the question you quoted.
– Leonardo Bonetti
Which is your web server and which vesão it?
– LeonanCarvalho
@Leonancarvalho IIS Express, I’m using the . net framework 4.5
– Leonardo Bonetti