Static Content Caching

We use IIS7 and dotnet 3.5 in our company to create various web applications used by our internal and external clients. It was recommended to start static caching of content to cache images and html files. I understand that in the web.config file we can set cachecontrolmaxage to indicate how many days the files should be cached. It was further recommended that these static pages be called passing the argument myjsfile.js? Verfile = 1234, which can be changed whenever a new version of the file is created.

Hope i come back right? Now, what they are looking for is the best way to reach the second part, instead of passing a new version number each time a new file is created, we can deploy it in some other way, so that all new changes in the static file in the production environment. I think search is an easy way to promote multiple images and static files during production without worrying about changing the version number.

+4
source share
3 answers

Since IIS7 the entire IIS configuration can be applied through the web.config file, more specifically the system.webServer section . In this case, you should check the caching section and create a custom profile (be sure to set the variableByQueryString attribute to true to do the job "? Version = xxx").

During the deployment process, it is necessary that the URL change, since the client will not check for a new version of the file while the cache is valid (and you say that you can assign days as the duration of the cache). One common pattern is to automatically generate a url based on the modification date if your original string is:

<script src='functions.js' />

You can change it to:

<script src='<%=GetFilenameWithModificationDate("functions.js")%>' />

The function should get the modification of the DateTime file and add it to the file, so if the file was last modified from 2010-01-01 at 10:12:34, it should generate something like this:

<script src='functions.js?version=20100101101234' />

Thus, whenever you modify a file, a new query line will be added and the cache will be updated.

Since you work in caching your static file files, I assume that performance is a consideration, so you should consider the penalty for checking the change date for each file, and you may want to use caching in an auxiliary function, control, or which mechanism you decide to use.

NTN

+1
source

Yes, the bit about using the query string with the version is correct and works. However, I'm not sure if changing the settings in web.config will really apply to static content. Caching for static content is managed in IIS using the IIS administration base as recommended by the developer where I work.

Currently, I manage the version number in one place and I can control it using the application's admin interface. The disadvantage is that after changing the version number, the version number of all static files changes for the entire application.

Alternatively, each static file is called a version as part of the file name, so nothing needs to be changed after the content is deployed. However, this also means that code that refers to static content must reference them using the full file name, and must also be changed when the static content changes.

Hope this helps.

0
source

Keep in mind that IIS will only call the .NET runtime for files mapped to asp.net ISAPI. Thus, static content, such as .html, .jpg, etc., does not depend on the configuration of web.config. You can display them, but you will lose performance in this way. This is probably not the best route.

For script code, I usually use a system like airmanx mentioned above. A global setting in .config, which is added to the URL and updated when a new version is available. You can automate this by capturing the # construction of the application and passing it on.

For static content, you might consider using all content from another domain (e.g. content.domain.com) and managing headers in IIS for caching, content expiration, etc. This is a fairly simple solution, t will require too much overhead, although you will need to involve your opponents if you need to finish the content. This is not a fully automated solution, but I found that it works very well on most of the large sites I worked on.

0
source

Source: https://habr.com/ru/post/1314151/


All Articles