How can I make sure that static content (images, css, javascript) is cached? What is the best approach?
2 Answers
Will recommend you to go through this tutorial to understand how caching happens on web (HTTP) in general.
Simply speaking, the web server needs to generate appropriate HTTP headers while sending the content to the client in order to control client-side caching. In ASP.NET/IIS environment, its IIS that typically handles the static file contents and therefore, you must configure IIS appropriately to control caching static files as per you needs. See below links for more information about configuring IIS caching for static content:
http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache
How to configure static content cache per folder and extension in IIS7?
EDIT: As you have asked about the best approach, the most prevalent approach that I see now days is to version static content (say by appending some version identifier at the end of file or URL). Once version-ed, you can treat it as immutable and then emit cache headers for caching it for infinite duration. In ASP.NET application, you can probably append the assembly version (or product version) to each static content URL. So essentially, you will invalidating the cache for every build (or every product release).
-
@VinayC What about the issue on [How to cache js and css files of ASP.NET MVC application served on IIS](http://stackoverflow.com/questions/41391542/how-to-cache-js-and-css-files-of-asp-net-mvc-application-served-on-iis/)? Thanks in advance... – Jack Dec 30 '16 at 08:56
-
@ClintEastwood, as long as you are serving static files, emitting cache headers using IIS configuration will work. To handle updates to static content, you have to use versioned urls (e.g. /image/a.png?v=2) so that browser will request again whenever you change the version. – VinayC Jan 05 '17 at 11:13
-
@VinayC Why do not try to post an example code instead of just writing comments??? – Jack Jan 05 '17 at 11:15
You can also make use of the HTML5 Offline web applications manifest. It allows you to set up a manifest where you define which files will be cached locally.
It is a nice, clear to understand broadly implemented, way of avoiding having to learn about IIS
and HTML
Caching.
http://www.w3schools.com/html/html5_app_cache.asp
(you should totally read up about those things)

- 423
- 2
- 14