Pedro Posada | How to make stylesheet refresh automatically

How to make stylesheet refresh automatically

Posted on October 13, 2008 - 3:06pm
<< 27 of 50 >>

When I build a site, I always make changes to the style.css file. I always add, modify or remove styles, but there was a problem. When someone came to see my site they would see an old version of the site because their browser would cache the style.css file. In order to avoid this, I came up with this drupal recipe or theme snippet.

Create a new stylesheet file in your theme's folder. Call it "uptodate.css".

Add a new block and put the code below inside. Select "php code" as input format.

<?php

drupal_add_css
(path_to_theme()."/uptodate.css?".time(), $type = 'theme', $media = 'all', $preprocess = TRUE)

?>

Enable the block.

Now you can add styles to "uptodate.css" file and they will take effect automatically. This is a great way to overwrite styles and to make sure all your visitors will see the latest version of your site's layout.

Submitted by Kim Pepper (not verified) on October 29, 2008 - 9:55pm.

This solution will force browsers to load the style sheet on every request, regardless of whether it has changed or not.

A better solution is to just increment a version number as part of the filename and have the html point to the newer version. This will force the style to be downloaded once, but then cached in browsers, proxy servers etc. from then on.

Submitted by pedro on October 30, 2008 - 1:05am.

Yes you are right, I use this only in development phase. I guess its better to use your solution when the website is more stable.

Post new comment

The content of this field is kept private and will not be shown publicly.
Please solve the math question. This way we will know you are not a robot.