Pedro Posada | How to make stylesheet refresh automatically

How to make stylesheet refresh automatically

Posted on October 13, 2008 - 3:06pm
<< 31 of 54 >>

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.

Submitted by stunning boy (not verified) on July 19, 2011 - 1:09pm.

wow this is very amazing and creative way to make stylesheet refresh automatically, thumbs up :)
css templates

Submitted by Maylene (not verified) on August 22, 2011 - 2:52am.

Nice blog! Thanks for sharing this very informative article about the process of learning. Read on for more information especially the four stages of competence at www.vssm.org

Maylene
www.vssm.org

Submitted by darrenmartin (not verified) on October 21, 2011 - 9:53am.

Now I know how to create style sheet, I have been looking for this.
best ipad case

Submitted by fred (not verified) on November 8, 2011 - 5:46am.

Thanks for your marvelous posting! I certainly enjoyed reading it, you could be a great author.I will ensure that I bookmark your blog and will come back in the future. I want to encourage you to definitely continue your great work, have a nice day.
Estudiar ingles en el extranjero

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.