Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force CSS Recache

Tags:

css

stylesheet

Is there any way to determine or force a CSS file to be re-cached if the user is operating off an old file?

like image 334
nsearle Avatar asked Jun 24 '09 20:06

nsearle


3 Answers

The single surest way to ensure that everyone reloads your new file is to change its URL. A reasonable way to do this without actually renaming it is to tack on a random string:

<link rel='stylesheet' href='/mystyles.css?random=87365835'>

That's using a sledgehammer to crack a nut, and bypassing all the HTTP mechanisms that exist to do this, but it will absolutely work everywhere, whereas the other techniques all have caveats.

Edit: @jitter: You don't have to change the URL automatically for every request - you could base it on the timestamp of the CSS file, or even just change it by hand when you want to ensure that the CSS gets reloaded.

like image 74
RichieHindle Avatar answered Sep 28 '22 16:09

RichieHindle


You can try editing the server etags: http://developer.yahoo.com/performance/rules.html#etags

But the preferred method is probably to rename your CSS file, such as with the date or the build number, so that users will always have the latest.

like image 35
Jourkey Avatar answered Sep 28 '22 15:09

Jourkey


Check this solution instead of using xx.css?rand=213213213

What is an elegant way to force browsers to reload cached CSS/JS files?

Or google for "auto versioning" plus adding javascript or css. Or "automatic asset versioning"

It uses an automatic approach which changes the path of the URL part no by appending get variables.

like image 45
jitter Avatar answered Sep 28 '22 17:09

jitter