Forum Moderators: open

Message Too Old, No Replies

Dynamic stylesheets

Will they be cached well?

         

tata668

11:53 am on Apr 24, 2006 (gmt 0)

10+ Year Member



I'm using PHP to generate my stylesheets (But this is not a PHP question as any server-side language could be used).

html:


<style type="text/css">@import url("css.php?css=normal&lang=en");</style>

css.php:


<?php
header('Content-type: text/css');
header('Cache-control: private') ;
header('Pragma: private');
?>
body{...}
h1{...}
etc

Do you think this kind of dynamic stylesheet will be cached efficiently on the client? Will they be cached as well as regular, static, ".css" stylesheets?

I ask that because regular stylesheets are cached for a long period of time by the browsers and that's what I want for mine too.

Does the fact that the stylesheet's path ends with ".php" (+ some parameters) will remove that extra caching behavior or do the browsers only look at the Content-type?

BlobFisk

11:45 am on Apr 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting question. I'm inclined to say no as they are technically being generated by the server side script and are fetched as a query string to the script.

encyclo

12:27 pm on Apr 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From my experience, PHP-generated stylesheets are not cached by default - you must use the PHP header [php.net] function to set an
expires
header:

<?php 
header("Cache-Control: must-revalidate");
$offset = 60 * 60 * 24 * -1;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($ExpStr);
?>

(example from php.net for caching for one day.)

A related thread is here:

[webmasterworld.com...]

tata668

1:40 pm on Apr 25, 2006 (gmt 0)

10+ Year Member



All right, thanks a lot! The suggested thread was a great read.

tata668

2:36 pm on Apr 25, 2006 (gmt 0)

10+ Year Member




$offset = 60 * 60 * 24 * -1;

This example you took from php.net was to *disable* cache!

To cache a file, I think "1" should be used instead of "-1"!

:-P

encyclo

3:33 pm on Apr 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This example you took from php.net was to *disable* cache!

Yes, I was, um, just trying to keep you on your toes. ;)

Sorry, I didn't check before posting!

hakre

12:15 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



per definition any GET request can be cached by the client (other then a POST request).

normally (as with the flip in the expires example), you want to disable the cache not enable. with the provided headers at the top (per default php might add headers to prevent caching), there should be no problem. it's not the querystring which is disabling the browser cache, these are the headers (if this file is not cached anyways!).

the expires one provides a hint to the client when a request "should" be done again. so this is a kind of add-on to a normal request of css file. in other words: i doubt that even that header is necessary and suggest to remove the cache-related header commands as well.

just compare the headers of a css file and a php file with each other to checkout and maybe eliminate the difference. after that, you browser can not even guess a difference between the css file and the php file.

--hakre