Bob Wheeler
Bob Wheeler

Reputation: 21

Disable Caching For A Single JS File Using Web.config

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

Upvotes: 2

Views: 4798

Answers (3)

gdt
gdt

Reputation: 1903

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)

Upvotes: 6

slolife
slolife

Reputation: 19870

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>

Upvotes: 2

Aliostad
Aliostad

Reputation: 81660

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.

Upvotes: 1

Related Questions