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

  • From: "Oliver Schulze L." <oliver@xxxxxxxxxxxxx>
  • To: dokuwiki@xxxxxxxxxxxxx
  • Date: Thu, 18 May 2006 00:00:14 -0400

Hi,
I'm writting about bug #802
http://bugs.splitbrain.org/?do=details&id=802#comment1121

Here is the patch against 2006-05-18.
I can send algo individual file patches

About this patch:
DW always send to the user the css and js, creating un-necesary traffic.
Instead of sending the css and js files, an URL is added to the <meta>
html tag and the user's browser/proxy is the one that decides to download
or not that css or js file.
DW already checks if a css or js cache file is outdated and updates it. A cache file
is created in the data path. So, this file is used in the URL to the css and js files.
This patch also allows the files in the data path to be accessed if they ends
in css or js. This is modified in the data/.htaccess file/


Todo:
- use the /data directory or a new one for css and js files?
- will the htaccess modification break current installations?

My situation
I have this patch tested and in production. The results are:
screen css: 5.325 bytes
print css: 20.613 bytes
js: 40.183 bytes
css + jss total: 66.121 bytes
wiki page: 14.379 bytes
So, I have a total(page + css + js) of 80.500 bytes to download in every page.


This was an issue for me and my users. Here in my country, 64kbps
or less is the standard, so, at a theoretical 16kbyte/s speed, every page
takes like 5 seconds to load!
Now, it takes only 1 second!

HTH
Oliver

--
Oliver Schulze L.
<oliver@xxxxxxxxxxxxx>

diff -Naur dokuwiki-2006-05-18/data/.htaccess 
dokuwiki-2006-05-18-oliver/data/.htaccess
--- dokuwiki-2006-05-18/data/.htaccess  2005-10-05 03:58:07.000000000 -0400
+++ dokuwiki-2006-05-18-oliver/data/.htaccess   2006-05-17 23:38:34.000000000 
-0400
@@ -1,2 +1,7 @@
+<Files ~ "^.*\.(css|js)$">
+    Order allow,deny
+    Allow from all
+</Files>
+
 order allow,deny
 deny from all
diff -Naur dokuwiki-2006-05-18/inc/template.php 
dokuwiki-2006-05-18-oliver/inc/template.php
--- dokuwiki-2006-05-18/inc/template.php        2006-05-16 05:45:51.000000000 
-0400
+++ dokuwiki-2006-05-18-oliver/inc/template.php 2006-05-17 23:29:40.000000000 
-0400
@@ -159,6 +159,25 @@
 }
 
 /**
+ * Convert a full path into a URL path
+ *
+ * If you have the full path of a file, this function remove the DOKU_INC
+ * from the path and prefix it with DOKU_BASE.
+ * So you can use as a relative URL in an html page.
+ *
+ * @param  string $path_full The full path to be converted
+ * @author Oliver Schulze L. <oliver@xxxxxxxxxxxxx>
+ */
+function tpl_path_convert_to_relative($path_full){
+  // remove the full path and left only the relative path
+  $path_rel = str_replace(DOKU_INC, '', $path_full);
+  // prefix the installation directory
+  $path_rel = DOKU_BASE . "$path_rel";
+  // return the relative path of the file
+  return $path_rel;
+}
+
+/**
  * Print the correct HTML meta headers
  *
  * This has to go into the head section of your template.
@@ -204,8 +223,19 @@
   }
 
   // load stylesheets
-  ptln('<link rel="stylesheet" media="screen" type="text/css" 
href="'.DOKU_BASE.'lib/exe/css.php" />',$it);
-  ptln('<link rel="stylesheet" media="print" type="text/css" 
href="'.DOKU_BASE.'lib/exe/css.php?print=1" />',$it);
+  //ptln('<link rel="stylesheet" media="screen" type="text/css" 
href="'.DOKU_BASE.'lib/exe/css.php" />',$it);
+  //ptln('<link rel="stylesheet" media="print" type="text/css" 
href="'.DOKU_BASE.'lib/exe/css.php?print=1" />',$it);
+  require_once(DOKU_INC.'lib/exe/css.php');
+  require_once(DOKU_INC.'lib/exe/js.php');
+  $cached_file = "";
+
+  // obtain the path of the current cache css screen file
+  $cached_file = tpl_path_convert_to_relative(css_out(false));
+  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'. 
$cached_file .'" />',$it);
+
+  // obtain the path of the current cache css print file
+  $cached_file = tpl_path_convert_to_relative(css_out(true));
+  ptln('<link rel="stylesheet" media="print" type="text/css" href="'. 
$cached_file .'" />',$it);
 
   // load javascript
   $js_edit  = ($ACT=='edit' || $ACT=='preview' || $ACT=='recover') ? 1 : 0;
@@ -219,8 +249,10 @@
     }
     ptln('</script>',$it);
   }
-  ptln('<script type="text/javascript" charset="utf-8" src="'.
-       
DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&amp;write='.$js_write.'"></script>',$it);
+  //ptln('<script type="text/javascript" charset="utf-8" src="'.  
DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&amp;write='.$js_write.'"></script>',$it);
+  // obtain the path of the current cache js file
+  $cached_file = tpl_path_convert_to_relative(js_out($js_edit, $js_write));
+  ptln('<script type="text/javascript" charset="utf-8" src="'. $cached_file 
.'"></script>',$it);
 }
 
 /**
diff -Naur dokuwiki-2006-05-18/lib/exe/css.php 
dokuwiki-2006-05-18-oliver/lib/exe/css.php
--- dokuwiki-2006-05-18/lib/exe/css.php 2006-05-06 17:55:54.000000000 -0400
+++ dokuwiki-2006-05-18-oliver/lib/exe/css.php  2006-05-17 23:17:13.000000000 
-0400
@@ -13,24 +13,25 @@
 require_once(DOKU_INC.'inc/io.php');
 require_once(DOKU_INC.'inc/confutils.php');
 
-// Main (don't run when UNIT test)
-if(!defined('SIMPLE_TEST')){
-    header('Content-Type: text/css; charset=utf-8');
-    css_out();
-}
 
 
 // ---------------------- functions ------------------------------
 
 /**
- * Output all needed Styles
+ * Create a css cache file and checks if it should be updated
  *
+ * @param boolean $print //print mode?
+ * @return string the full path of the current css cache file
  * @author Andreas Gohr <andi@xxxxxxxxxxxxxx>
  */
-function css_out(){
+function css_out($print){
     global $conf;
     global $lang;
-    $print = (bool) $_REQUEST['print'];   //print mode?
+
+    // Main (don't run when UNIT test)
+    if(defined('SIMPLE_TEST')){
+        return "";
+    }
 
     // The generated script depends on some dynamic options
     $cache = getCacheName('styles'.$print,'.css');
@@ -72,8 +73,8 @@
     header('Pragma: public');
     if(css_cacheok($cache,array_keys($files))){
         http_conditionalRequest(filemtime($cache));
-        readfile($cache);
-        return;
+        // reuse the css cache
+        return $cache;
     } else {
         http_conditionalRequest(time());
     }
@@ -105,8 +106,8 @@
     // save cache file
     io_saveFile($cache,$css);
 
-    // finally send output
-    print $css;
+    // finally send the path of the current css cache
+    return $cache;
 }
 
 /**
diff -Naur dokuwiki-2006-05-18/lib/exe/js.php 
dokuwiki-2006-05-18-oliver/lib/exe/js.php
--- dokuwiki-2006-05-18/lib/exe/js.php  2006-05-07 12:55:20.000000000 -0400
+++ dokuwiki-2006-05-18-oliver/lib/exe/js.php   2006-05-17 23:16:15.000000000 
-0400
@@ -13,25 +13,25 @@
 require_once(DOKU_INC.'inc/pageutils.php');
 require_once(DOKU_INC.'inc/io.php');
 
-// Main (don't run when UNIT test)
-if(!defined('SIMPLE_TEST')){
-    header('Content-Type: text/javascript; charset=utf-8');
-    js_out();
-}
-
 
 // ---------------------- functions ------------------------------
 
 /**
  * Output all needed JavaScript
  *
+ * @param boolean $edit edit or preview mode?
+ * @param boolean $write writable?
+ * @returns string The full path of the current cache js file
  * @author Andreas Gohr <andi@xxxxxxxxxxxxxx>
  */
-function js_out(){
+function js_out($edit, $write){
     global $conf;
     global $lang;
-    $edit  = (bool) $_REQUEST['edit'];   // edit or preview mode?
-    $write = (bool) $_REQUEST['write'];  // writable?
+
+    // Main (don't run when UNIT test)
+    if(defined('SIMPLE_TEST')) {
+        return "";
+    }
 
     // The generated script depends on some dynamic options
     $cache = getCacheName('scripts'.$edit.$write,'.js'); 
@@ -61,8 +61,8 @@
     header('Pragma: public');
     if(js_cacheok($cache,array_merge($files,$plugins))){
         http_conditionalRequest(filemtime($cache));
-        readfile($cache);
-        return;
+        // reuse the current js cache file
+        return $cache;
     } else {
         http_conditionalRequest(time());
     }
@@ -148,8 +148,8 @@
     // save cache file
     io_saveFile($cache,$js);
 
-    // finally send output
-    print $js;
+    // finally return the current js cache file
+    return $cache;
 }
 
 /**

Other related posts: