How to make stylesheet refresh automatically

How to make stylesheet refresh automatically

Categories:

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.

This solution will force

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.

Yes you are right, I use

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.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.