How to prevent js/css caching in iis?

Member

by lottie , in category: Third Party Scripts , a month ago

How to prevent js/css caching in iis?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jasen , a month ago

@lottie 

To prevent caching of JavaScript (JS) and Cascading Style Sheets (CSS) in IIS (Internet Information Services), you can use the cache-control headers in your web.config file or in your HTML markup.

  1. Add cache-control headers in web.config file: You can add the following code in your web.config file to prevent caching of JS and CSS files:
1
2
3
4
5
6
7
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="DisableCache" />
    </staticContent>
  </system.webServer>
</configuration>


  1. Add cache-control headers in HTML markup: You can also prevent caching by adding cache-control headers directly in your HTML markup using meta tags. For example, you can add the following code in the section of your HTML file:
1
2
3
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">


By adding these cache-control headers, you can instruct the browser not to cache the JS and CSS files, ensuring that the latest version is always loaded from the server. This can help to prevent issues with outdated or cached files causing problems with your website.

by haylee.mertz , a month ago

@lottie 

If you want to prevent caching for only specific files (rather than all JavaScript and CSS), you can set the cache-control headers in the HTTP response headers for those specific files. Here's how to do this:

  1. Open IIS Manager.
  2. Navigate to the site or application where the files are located.
  3. Select the specific JS or CSS file for which you want to prevent caching.
  4. In the Features View, double-click on "HTTP Response Headers."
  5. Click on "Set Common Headers" in the Actions pane.
  6. Check the box for "Expire Web content" and set the value to "Immediately."
  7. Check the box for "Enable content expiration" and set the value to "After 0 seconds of being accessed."
  8. Click OK to save the changes.


By setting these headers for specific files, you can ensure that they are not cached by the browser and are always fetched directly from the server, thus preventing caching issues for those particular files.