简体   繁体   中英

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

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)

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM