45

Is there any way to diable the caching of a single javascript file in my ASP.NET applicaiton without disabling the caching of any other files in the application?

It is running on IIS 7 in Azure, so to me it seems that my only options of controlling this are within the webserver tags.

I currently use the folowwing config, but this is disabling cache on all files.

     <modules runAllManagedModulesForAllRequests="true"/>

    <staticContent>
      <clientCache cacheControlMode="DisableCache"/>
    </staticContent>

  </system.webServer>

I just want to disable cache of a single javascript file that changes quite often.

Is it possible?

jherax
  • 5,238
  • 5
  • 38
  • 50
Dean
  • 451
  • 1
  • 4
  • 3
  • Yes, it is possible, you can do it in your configurtion file: wrap the into , (see my post below) – jherax May 21 '14 at 16:29

6 Answers6

55

I just stumbled across this question; you can use the following to disable the cache on a specific file:

<configuration>
  <location path="path/to/the/file">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

(Note that the path is relative to the web.config file)

Alternatively, place the single file in a directory on it's own, and give that directory it's own web.config that disables caching for everything in it;

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Cache-Control" value="no-cache" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

[Both tested on IIS7.5 on Windows 7, but you'll have to confirm that it works OK on Azure]

gdt
  • 1,822
  • 17
  • 19
  • 4
    Works perfectly for static context published to an Azure Web App through source control or continuous integration. In my case an Angular CLI built app where the production build takes care of creating cache busting names for the bundle files and all I want the no-caching on is the index.html. Thanks! – Brian Noyes Jun 21 '17 at 09:17
  • Can you elaborate on why your second approach of placing a Web.config into a directory uses an entirely different approach? What's the difference between clientCache and Cache-Control=no-cache? – Rudey Feb 11 '19 at 22:50
  • There was no rhyme or reason for choosing different mechanisms. In fact I hadn't noticed I had until you just raised it! I'm guessing either mechanism would work. – gdt Feb 12 '19 at 15:00
  • I know it's been a while, but if I have more than one file, and don't want the whole folder, do I have to make that whole block for each or can we separate other files with commas or something? – drichardson Dec 11 '20 at 21:01
8

Looks like the above answer is missing a "profiles" tag

<caching>
  <profiles>
    <add extension=".js" kernelCachePolicy="DontCache" policy="DontCache"/>
  </profiles>
</caching>
What Would Be Cool
  • 6,204
  • 5
  • 45
  • 42
7

You're going to want to look at the System.WebServer/Caching class, where you can apply a caching profile to particular extensions. This will at least enable you to control it for all Javascript files ending with the .js.

<system.webServer>
...

   <caching>
      <add extension=".js" policy="DontCache" kernelCachePolicy="DontCache" />
   </caching>

</system.webServer>

That should disable .js caching on both process and kernel caching from the cloud.

I think you can create this web.config in a folder that contains just your file and it will disable caching for .js only at that folder level. I honestly have not tried that myself, so just a suggestion you could test.


Beyond that, take a look at the documentation for IIS related to caching config:

/Caching: http://www.iis.net/ConfigReference/system.webServer/caching

/Caching/Profiles: http://www.iis.net/ConfigReference/system.webServer/caching/profiles

/Caching/Profiles/Add: http://www.iis.net/ConfigReference/system.webServer/caching/profiles/add

Hopefully that, plus some research on those config tags will help.

If not, I'd recommend looking into implementing a custom HTTP Module that you can insert into the IIS request pipe, which can filter your caching control down to that particular file

** for what its worth this is just IIS behavior and won't be different in or out of Azure, so you could easily test this local without bothering with the Dev fabric or Azure testing.

Taylor Bird
  • 7,767
  • 1
  • 25
  • 31
6

You can disable output caching on folder level by removing an extension on folder level by including a web.config in that folder like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
        <caching>
            <profiles>
                <remove extension=".js" />
            </profiles>
        </caching>

  </system.webServer>
</configuration>

now this folder will not have output caching enabled for files ending with .js

Martijn
  • 404
  • 7
  • 16
1

Yes, it is possible, you can do it in the configuration file:
(See https://stackoverflow.com/a/4821328/2247494)

<configuration>
    <location path="cache.manifest">
      <system.webServer>
        <staticContent>
          <clientCache cacheControlMode="DisableCache" />
        </staticContent>
      </system.webServer>
    </location>
</configuration>
Community
  • 1
  • 1
jherax
  • 5,238
  • 5
  • 38
  • 50
1

Open IIS panel.

Open the related application from sites, then navigate to folder containing particular file.

Now from right pane, switch to content view

Select your desired file

Now right click on file to choose "Switch to Feature View".

From there choose HTTP Response Headers, click "Add" to add new header, Add "Cache-Control" header with "no-cache" value.

Rohit
  • 330
  • 1
  • 5
  • 16