2

I currently cache everything possible on my site (images, JS, CSS). There is only one JS file that I need to be loaded fresh every single time. How do I omit just one file from caching, using web.config, whilst leaving everything else cached?

Note that I tried another link here, and it didn't seem to stop the caching of my file: How do I disable caching of an individual file in IIS 7 using weserver config settings

Community
  • 1
  • 1
Bob Wheeler
  • 21
  • 1
  • 2

3 Answers3

6

How about:

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

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

gdt
  • 1,822
  • 17
  • 19
2

I don't think you can do it using web.config, but you could add a unique querystring parameter to the javascript url in order for it to be loaded every time:

If you are using ASP.NET

<script src="mycode.js?<%=System.Guid.NewGuid.ToString()%>"></script>
slolife
  • 19,520
  • 20
  • 78
  • 121
1

Set the path for it not as a static URL but get an ASPX page to serve the script. Inside your ASPX page just send back the text:

byte[] javascriptTextBuffer = GetMyJavascript();
Response.ContentType = "text/javascript";
Response.Write(javascriptTextBuffer);

Inside the page turn off caching.

Having said that, it seems to me that you are doing something wrong that have to load the JavaScript file everytime. Make scripts static but use parameters to drive versatility.

Community
  • 1
  • 1
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • I agree...if you need certain js data to be fesh everytime, it seems like you would be better served by having a a few dynamic elements defined in your page that your static scripts can use. – Matthew Nichols Mar 08 '11 at 16:43
  • Hmmmm... That will work, but is not ideal for many reasons. Hopefully a better solution exists :-). – Bob Wheeler Mar 08 '11 at 16:43
  • The reason that the JS isn't to be cached is that it's a plugin that goes onto a customer website. I would obviously like to be able to update that plugin and have it run the latest 'stuff' on that customer website. – Bob Wheeler Mar 08 '11 at 16:44
  • 1
    So you are explicitly making customer's site less performant for your convinience? Unless you are creating virus I'd recommned to have cahing set to some time (i.e. a day is likley enough to reduce performance impact and provide reasonably lates version of your script). – Alexei Levenkov Mar 08 '11 at 17:23