3

For a long time I've been updating ASP.NET pages on the server and never find the correct way to make changes visible on files like CSS and images.

I know if a append something in the URL the browser will think the file is another one:

<img src="/images/myLogo.png?v=1"/>

or perhaps changing its name:

<img src="/images/myLogo.v1.png"/>

Unfortunately it does not look the correct way. In a case were I'm using App_Themes the files in this folder are automatically injected in the page in a way I can't easily change the URL.

So my question is:

When I'm publishing de ASP.NET Application on the server what is the correct way to signal to IIS (and it notify browser after that) that a file was changed? It is not automatic? Should I change some configuration in IIS or perhaps make some "decoration" in the code?

I've already tried many questions here in SO like "ASP.NET - Invalidate browser cache", "How to refresh the browser cache of an image?", "Handle cached images? How to get the browser to show the new version?", and even "What is an elegant way to force browsers to reload cached CSS/JS files?" but none of them actually take another aproach else in a way you must handle it manually in the code instead of IIS or ASP.NET configuration.

The closer I could find is "Asking browsers to cache our images (ASP.NET/IIS)" where they set expiration but not based on the fact the files were update. Instead they used days or hour to cache those file so they would updated even when no changes were made.

I'm want to know if IIS or ASP.NET offers something related to this, automatically send to the browser that the files was changed. Is it possible/built in?

Vitor Canova
  • 3,778
  • 4
  • 27
  • 55

1 Answers1

1

The options you have to update the browser side, cached item are:

  1. Change the file name
  2. Add url parameter
  3. Place it on cache for a limited time (eg for couple of hours)
  4. Compare the date-time of creation.
  5. Signaling with eTag.

With the three two you avoiding one server call for each item, but the third option load it again after some time.

With the others you have to make one call to the server to see if needs to be load it again.

So you can not have all here, there is not correct way, and you need to chose what is the best for you, and what you can do. The faster from client perspective is the (1) and (2) options.

The direct answer to your question is to use eTag, or date-time compare of the file creation, but you loose that way, a call to the server, you only win the size of what is travel back.

Some more links:
http eTag
How do I support ETags in ASP.NET MVC?
Configuring ETags with Http module in asp.net
How to control web page caching, across all browsers?
Jquery getScript caching

and you can find even more.

Community
  • 1
  • 1
Aristos
  • 63,580
  • 14
  • 112
  • 146