Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache CSS and JS files

When I refresh my website in less than 2-3 minutes, Firebug shows these nice requests:

1. /core.css          304 Not modified
2. /core.js           304 Not modified
3. /background.jpg    304 Not modified

BUT when I refresh after >3 minutes, I get:

1. /core.css          200 OK
2. /core.js           200 OK
3. /background.jpg    304 Not modified

Why my CSS and JS files are downloaded again and images aren't?

I'm using ASP.NET MVC 3, I DON'T use [OutputCache], and in my /Content folder (where all css, js and img files live in subfolders) I have this Web.config:

<configuration>
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
        </staticContent>
    </system.webServer>
</configuration>

which sets HTTP header Cache-Control: max-age=86400 ONLY. So basically CSS, JS and images are treated the same way, but somehow CSS and JS don't get cached for a longer period... why is that?

like image 886
Darmak Avatar asked Sep 05 '10 02:09

Darmak


People also ask

Do JS files get cached?

JavaScript and CSS files are usually cached in the browser after the first access. The browser cache is used to store web application assets like images, CSS, and JS code on your computer's hard drive.

Do CSS files get cached?

Unless you've messed with your server, yes it's cached. All the browsers are supposed to handle it the same. Some people (like me) might have their browsers configured so that it doesn't cache any files though. Closing the browser doesn't invalidate the file in the cache.

What are .JS files and .CSS files?

Generally, CSS files are called stylesheets, and Javascript files are called scripts. Javascript and CSS that other javascript relies on to run (for instance the js library jQuery, or the js/css library Bootstrap) is called a library.

Where are JS files cached?

Cached assets such as JavaScript will typically be served from the browser's cache instead of making another request for a resource that has already been retrieved.


2 Answers

Hopefully this will help: http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

The <clientCache> element of the <staticContent> element specifies cache-related HTTP headers that IIS 7 and later sends to Web clients, which control how Web clients and proxy servers will cache the content that IIS 7 and later returns...

like image 58
Yannis Avatar answered Sep 29 '22 11:09

Yannis


This occurs with IIS or with the Visual Studio web server? for some time perceived this behavior while developing (using the VS web server), but when publish it in IIS this not occur anymore.

like image 40
bodee Avatar answered Sep 29 '22 10:09

bodee