Disable browser caching for specific files or folders

Search for a command to run...

No comments yet. Be the first to comment.
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or lots of other things. It's probably most often used around HTTP requests. If you haven't heard about it you defi...

This is part 2 of my series on unit testing in .NET (C#) and will be about my conventions regarding naming unit tests, how to structure them and which parts to test. And lastly I will help you increase readability of assertions and potential error me...

I feel like unit testing is very important and very beneficial in a lot of ways. It helps with not breaking existing functionality and thereby also making you more confident in changing code without fear of breaking important stuff. Often it also mak...

I've been working with Swagger through NSwag a lot recently and also needed to get it to work when hidden behind a reverse proxy - i.e. another service forwarding a request to the service exposing the Swagger UI. Some of the issues I've had along the...

The other day I was writing a unit test to verify what was supposed to happen when a Task would throw an exception when awaited. It wasn't as straight forward as I first assumed, but I quickly found this Stack Overflow post that helped me: https://st...

It’s not uncommon needing to disable browser caching of some specific files or entire folders on your website or in your web application. Sometimes you need the user’s browser to not cache some specific files and always try to retrieve them from the server. I recently needed to do this while working with the HTML5 application cache to ensure that the browser updated all files listed in the manifest file when the manifest was updated.
There are two simle ways to achieve this.
If you want to disable caching of all files in a folder and its subfolders you can add a Web.config in the root folder you want to disable browser caching for. Say you wanted to disable browser caching for the files in the folder js/nocache and all subfolders, then you would place the Web.config here: js/nocache/Web.config.
Add the following to the Web.config to disable browser caching.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</configuration>
The content of the Web.config file is inherited for all subfolders.
Another way is to add the settings directly to the default Web.config of your project. This also enables you to control the browser caching for single files.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Specific file -->
<location path="manifest.appcache">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
<!-- Folder including subfolders -->
<location path="js/nocache">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
</configuration>
The second part of this solution has the exact same results as the first solution.
By setting the <clientCache cacheControlMode="DisableCache" /> the server will add a few headers to the HTTP response instructing the browser to not cache the responses.
Here’s an example of an HTTP response after our changes:
HTTP/1.1 200 OK
Cache-Control: no-cache
Last-Modified: Wed, 01 Oct 2014 14:18:11 GMT
ETag: "b198e28782ddcf1:0"
Content-Type: text/css
Accept-Ranges: bytes
Date: Wed, 01 Oct 2014 14:25:43 GMT
Content-Length: 26685
The three headers, Cache-Control, Last-Modified and ETag, have now been added and tells the browser to not cache the response.