jwalkerjr
jwalkerjr

Reputation: 1809

Browser Caching of CSS files

Quick question regarding CSS and the browser. I tried searching SO and found some similar posts, but nothing definitive.

I use one or two CSS files in my web projects. These are referenced in the HEAD of my web pages. Once I hit one of my pages, does the CSS get cached so that it's not re-downloaded with each request? I hope so. Do IE, Firefox and Safari handle this differently? If the browser is closed, is the CSS refreshed on the first visit when a new browser instance is opened?

Upvotes: 76

Views: 137023

Answers (7)

Voytas
Voytas

Reputation: 1

Browser cache is one thing.

But second thing is server(s) cache(s). If your server support CDN service (Content Delivery Network), your webpage files could be cached on one or more servers. Even if you clear browser cache, add version or other parameters to linkrel url, your and other users' browsers will be still loading page from the server cache, not from the updates files.

I have this problem with my server which has turned on CDN by default. Every time I update some css files need clear server cache otherwise browser still loading old version. There is a special button in server control panel called "CLEAN CDN CACHE". After that page is loading updated and I am sure the same is for other users. Check control panel of your server for similar option or write to server support team how to turn of CDN or not allow for caching.

It can be also helpful a .htaccess file with settings:

Header set Cache-Control "no-cache, no-store, private, must-revalidate"

It not allow for caching files. But I dont know it will work with every server type (with Apache should).

Upvotes: 0

Már Örlygsson
Már Örlygsson

Reputation: 14606

Your file will probably be cached - but it depends...

Different browsers have slightly different behaviors - most noticeably when dealing with ambiguous/limited caching headers emanating from the server. If you send a clear signal, the browsers obey, virtually all of the time.

The greatest variance by far, is in the default caching configuration of different web servers and application servers.

Some (e.g. Apache) are likely to serve known static file types with HTTP headers encouraging the browser to cache them, while other servers may send no-cache commands with every response - regardless of filetype.

...

So, first off, read some of the excellent HTTP caching tutorials out there. HTTP Caching & Cache-Busting for Content Publishers was a real eye opener for me :-)

Next install and fiddle around with Firebug and the Live HTTP Headers add-on , to find out which headers your server is actually sending.

Then read your web server docs to find out how to tweak them to perfection (or talk your sysadmin into doing it for you).

...

As to what happens when the browser is restarted, it depends on the browser and the user configuration.

As a rule of thumb, expect the browser to be more likely to check in with the server after each restart, to see if anything has changed (see If-Last-Modified and If-None-Match).

If you configure your server correctly, it should be able to return a super-short 304 Not Modified (costing very little bandwidth) and after that the browser will use the cache as normal.

Upvotes: 70

Andy Ford
Andy Ford

Reputation: 8480

It's probably worth noting that IE won't cache css files called by other css files using the @import method. So, for example, if your html page links to "master.css" which pulls in "reset.css" via @import, then reset.css will not be cached by IE.

Upvotes: 3

Deniss Kozlovs
Deniss Kozlovs

Reputation: 4841

To the first part of your question - yes, browsers cache css files (if this is not disabled by browser's configuration). Many browsers have key combination to reload a page without a cache. If you made changes to css and want users to see them immediately instead of waiting next time when browser reloads the files without caching, you can change the way CSS ir served by adding some parameters to the url like this:

/style.css?modified=20012009

Upvotes: 51

DanSingerman
DanSingerman

Reputation: 36502

It does depend on the HTTP headers sent with the CSS files as both of the previous answers state - as long as you don't append any cachebusting stuff to the href. e.g.

<link href="/stylesheets/mycss.css?some_var_to_bust_cache=24312345" rel="stylesheet" type="text/css" />

Some frameworks (e.g. rails) put these in by default.

However If you get something like firebug or fiddler, you can see exactly what your browser is downloading on each request - which is expecially useful for finding out what your browser is doing, as opposed to just what it should be doing.

All browsers should respect the cache headers in the same way, unless configured to ignore them (but there are bound to be exceptions)

Upvotes: 9

Al W
Al W

Reputation: 7713

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. Changing the file on the server should cause a refresh of the file however.

Upvotes: 0

Jan Hančič
Jan Hančič

Reputation: 53930

That depends on what headers you are sending along with your CSS files. Check your server configuration as you are probably not sending them manually. Do a google search for "http caching" to learn about different caching options you can set. You can force the browser to download a fresh copy of the file everytime it loads it for instance, or you can cache the file for one week...

Upvotes: 1

Related Questions