Cache control for browser

Discussion corner for Developers of Serendipity.
Post Reply
annonymous
Regular
Posts: 6
Joined: Sat Aug 26, 2006 6:41 pm

Cache control for browser

Post by annonymous »

Hi developers, have you considered the cache control for browser? I mean that, currently, S9Y doesn't allow browser to cache its pages. This is not quite convenient when we want to surf over a blog and push the Back/Forward buttons of the browser, we'll have to wait the browser to reload the pages.

I think that this would be very simple with the header(...) funtion of PHP.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Cache control for browser

Post by garvinhicking »

Hi!

Do you know the precise contents to send?

The problem is that many s9y plugins are dynamic, and thus the output of a page would differ very easily (comment count, recent comment sidebars, even a "random quote" or something like that).

I must admit I don't know much about browser Cache-Control headers, so I'd appreciate any help on this.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
annonymous
Regular
Posts: 6
Joined: Sat Aug 26, 2006 6:41 pm

Post by annonymous »

Hi! I found that there is an efficient cache control code in the famous phpBB package (you would find this code at the end of the file phpBB\includes\page_header.php):

===================================================
// Work around for "current" Apache 2 + PHP module which seems to not
// cope with private cache control setting
if (!empty($HTTP_SERVER_VARS['SERVER_SOFTWARE']) && strstr($HTTP_SERVER_VARS['SERVER_SOFTWARE'], 'Apache/2'))
{
header ('Cache-Control: no-cache, pre-check=0, post-check=0');
}
else
{
header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
}
header ('Expires: 0');
header ('Pragma: no-cache');
===================================================

In my opinion, this code is reasonable for most of cases. When we surf over pages by clicking on the links, the browser reloads always even if we return on a page that we have seen before. But when we push the Back or Forward button on the browser's toolbar, it does not reload the page, but it will fetch directly from its cache. Personally I think this is more "lovely" than reloading each time we push the back button. :)

Moreover, by using this code, we will be able to review our pages in "Work Offline" mode.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Hm, that code to me looks more like to enforce no caching?

You could try it easily by patching serendipity's index.php file, in the first few lines where also the other header() calls are made. Care to try it out?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
annonymous
Regular
Posts: 6
Joined: Sat Aug 26, 2006 6:41 pm

Post by annonymous »

Yeap, thanks. I have put this code in two files: index.php and serendipity_admin.php, it works nicely for me like what I described above up to now.

I asked the question because I didn't want to modify the code myself, that would make me tired when I upgrade to a newer version. :(

Here is my blog, if anyone has intention to try this thing out: http://annonymous.online.fr/Blog/
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Thanks a lot; I now committed this patch to the SVN trunk, and made it configurable via $serendipity['CacheControl'] which you can set in your serendipity_config_local.inc.php file! In the future we might make this default or make a configuration option in the menu out of this; but I'll have to run some tests first.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply