Ivan Koshelev
Ivan Koshelev

Reputation: 4260

Asp Mvc development, selectively disable cache only for JS / CSS / HTML files i'm currently working on

I'm developing an ASP MVC Single Page Application with Visual Studio 2012. It uses components made of static JS / CSS / HTML files and loaded per need. The approach works great without even needing bundling in production, since statics is cached by the browser, but there is a problem in dev, since cache has to be disabled to refresh files I'm working on at the moment, and that means about 100 small static files are loaded with each page refresh taking about 40 seconds.

I'm currently lookin into Chrome Workspaces, but I think that a more universal viable solution is to specifically disable cache for files with modification date within last 30 minutes.

I'm looking for alternative solutions or an existing component to disable cache for recently modified files in VS / ASP (a custom HTTP handler?).

Upvotes: 0

Views: 1816

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

Assuming you are using IIS or IIS Express, the simplest solution is just to disable caching entirely in your development environment by adding settings to your web.config file.

NOTE: If you have no web.config file, you can create one in your website's root directory.

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

Alternatively, you can disable caching of files in a specific location of your website:

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

In either case, you can use web config transformation during your release in order to remove these sections in your test/production environments.

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <!-- Use this section if you are disabling caching site-wide -->
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Cache-Control" value="no-cache" xdt:Transform="Remove" xdt:Locator="Match(name)" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>

    <!-- Use this section if you are disabling per folder (duplicate if necessary) -->
    <location path="path/to/the/file" xdt:Transform="Remove" xdt:Locator="Match(path)">
        <system.webServer>
            <staticContent>
                <clientCache cacheControlMode="DisableCache" />
            </staticContent>
        </system.webServer>
    </location>
</configuration>

Reference: How do I disable caching of an individual file in IIS 7 using weserver config settings

Upvotes: 0

Related Questions