auto-versioning for js and css files. Needs .htaccess to be installed
authorArun Persaud <arun@nubati.net>
Wed, 9 May 2012 03:42:59 +0000 (20:42 -0700)
committerArun Persaud <arun@nubati.net>
Wed, 9 May 2012 03:42:59 +0000 (20:42 -0700)
The md5 of the file will be added to the filename of the .js or .css file.
.htaccess will remove these again, but the browser will be able to cache correctly now.

.gitignore
INSTALL
htaccess.template [new file with mode: 0644]
include/output.php

index 6a0fae2de564c459e79d86f614a160f47d89584e..73d57b1397a8ab52327480eafe97d592dcf5705f 100644 (file)
@@ -8,3 +8,4 @@ pics/arrow2.svg
 pics/arrow3.svg
 scoretest.txt
 testscoring.php
 pics/arrow3.svg
 scoretest.txt
 testscoring.php
+.htaccess
\ No newline at end of file
diff --git a/INSTALL b/INSTALL
index 1929213241253db014ba7c4b55d6ac0419a74152..886a94d8256a462e161f183acc6c8c84437d5b0f 100644 (file)
--- a/INSTALL
+++ b/INSTALL
@@ -55,6 +55,10 @@ Installation
        return;
     }
 
        return;
     }
 
-3) Log in and enjoy ;)
+3) install .htaccess: copy htaccess.template to .htaccess and edit it to set RewriteBase.
+   If your site is at http://example.org/~test/edoko you need to set
+   RewriteBase to /~test/edoko
+
+4) Log in and enjoy ;)
 
 Arun
 
 Arun
diff --git a/htaccess.template b/htaccess.template
new file mode 100644 (file)
index 0000000..351842c
--- /dev/null
@@ -0,0 +1,10 @@
+RewriteEngine on
+
+# need to set this to your web path
+RewriteBase   /<web path to your files>
+
+# hide git directory (and others)
+RedirectMatch 404 \.(git|cvs|svn|bzr|hg)(/.*|$)
+
+# use autoversioning of js and css files
+RewriteRule ^(.*)\.[[:xdigit:]]{32}\.(css|js)$ $1.$2 [L]
index 92c301fff98f0bd8559d8d4c3accf3c57e39921a..a075fb3f827e72a205142b667544ca7104ea6bce 100644 (file)
@@ -26,6 +26,21 @@ if(!isset($HOST))
 
 /* functions which only ouput html  */
 
 
 /* functions which only ouput html  */
 
+function autoversion($file)
+{
+  /* changes the file name of e.g. css/style.css to css/style.<md5>.css/js
+   * this way the browser can cache the file and will reload it if the file changed
+   * needs to have .htaccess set up correctly to link back to css/style.css */
+
+  /* only use it for file that have an absolut path */
+  if(!file_exists(dirname($_SERVER['SCRIPT_FILENAME']). '/' . $file))
+    return $file;
+
+  $md5 = md5_file(dirname($_SERVER['SCRIPT_FILENAME']). '/' . $file);
+  return preg_replace('{\\.([^./]+)$}', ".$md5.\$1", $file);
+}
+
+
 function output_ask_for_new_game($playerA,$playerB,$playerC,$playerD,$oldgameid)
 {
   global $RULES;
 function output_ask_for_new_game($playerA,$playerB,$playerC,$playerD,$oldgameid)
 {
   global $RULES;
@@ -410,13 +425,13 @@ function output_header()
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width; initial-scale=1.0;" />
      <link rel="shortcut icon" href="pics/edoko-favicon.png" />
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width; initial-scale=1.0;" />
      <link rel="shortcut icon" href="pics/edoko-favicon.png" />
-     <link rel="stylesheet" href="css/normalize.css?v=1" />
-     <link rel="stylesheet" href="css/standard.css?v=41" />
-     <link rel="stylesheet" href="css/dateinput.css?v=1"/>
-     <script type="text/javascript" src="include/jquery.js"> </script>
-     <script type="text/javascript" src="include/jquery.tablesorter.js"></script>
-     <script type="text/javascript" src="include/jquery.tools.min.js"></script>
-     <script type="text/javascript" src="include/game.js"> </script>
+     <link rel="stylesheet" href="<?php echo autoversion("css/normalize.css"); ?>" />
+     <link rel="stylesheet" href="<?php echo autoversion("css/standard.css"); ?>" />
+     <link rel="stylesheet" href="<?php echo autoversion("css/dateinput.css"); ?>"/>
+     <script type="text/javascript" src="<?php echo autoversion("include/jquery.js"); ?>"> </script>
+     <script type="text/javascript" src="<?php echo autoversion("include/jquery.tablesorter.js"); ?>"></script>
+     <script type="text/javascript" src="<?php echo autoversion("include/jquery.tools.min.js"); ?>"></script>
+     <script type="text/javascript" src="<?php echo autoversion("include/game.js"); ?>"> </script>
      <script type="text/javascript">
      </script>
   </head>
      <script type="text/javascript">
      </script>
   </head>