[dokuwiki] Re: Bug# 802: Patch for css and js cache

  • From: Ben Coburn <btcoburn@xxxxxxxxxxxxx>
  • To: dokuwiki@xxxxxxxxxxxxx
  • Date: Thu, 18 May 2006 10:58:31 -0700


On May 18, 2006, at 9:13 AM, Oliver Schulze L. wrote:

I have been reading and working with the current development code, as
of today.(2006-05-18)
Sorry, I didn't look carefully at the date and the patch did not appear to be in the darcs format so I assumed you were not working with the current code.

The problem this patch solves is that php is reading the file and output
it to the browser. This procedure does not allow the brower or a proxy to
cache its contents.
The solution is to print the css URL to the html page and let the browser
ask the webserver for the file. The css of js should *not* be readed by a php script.
[...]
If DW uses print() or readfile(), then this bug is not solved.
This is not true. PHP does not produce content that can be cached by default *only* because it does not make assumptions about the desired caching behavior. Scripts that want their content to be cached just have to send the HTTP cache-control headers (and ideally respond to some other headers). The print() and readfile() have nothing to do with this.

If you want to pick the code apart line by line... here are the lines you should be looking at. (The following is from "lib/exe/css.php", other scripts work the same way.)

71 header('Cache-Control: public, max-age=3600');
72 header('Pragma: public');
- First send the headers to enable client side caching that is verified every hour.


73 if(css_cacheok($cache,array_keys($files))){
74 http_conditionalRequest(filemtime($cache));
75 readfile($cache);
76 return;
- If the css has already been prepared...
- check if the client is making a conditional request and has the current file [L74]
- if the client already has the file a '304 Not Modified' HTTP response is sent, and execution stops inside http_conditionalRequest(). Note, no data is sent with the 304 response code.
- otherwise the script sends the prepared css and "stops" [L76]. (Actually returns, but nothing else happens.)


77 } else {
78 http_conditionalRequest(time());
79 }
- Else the css needs to be prepared, so send the new conditional headers [L78].
- Lines 80 to 107, prepare the css.


109 print $css;
- Send the css with the cache-control HTTP headers from line 71-72 and the headers to prepare for conditional requests from line 78.


See also "inc/pageutils.php" for the http_conditionalRequest() function (on lines 363-391). This handles sending the HTTP '304 Not Modified' response (and stopping the script) as needed.

For more details on caching see RFC2616 "Hypertext Transfer Protocol -- HTTP/1.1", particularly sections 14.9, 13, 10.3.5, 14.29, 14.19, 14.25, 14.26, 14.21.
http://www.w3.org/Protocols/rfc2616/rfc2616.html


I hope this is not more information than you wanted. I'm well acquainted with this code only because I happen to have submitted some of these patches.


Regards, Ben Coburn


------------------- silicodon.net -------------------

--
DokuWiki mailing list - more info at
http://wiki.splitbrain.org/wiki:mailinglist

Other related posts: