Using the script
Step 1: Start by setting the correct mime type for the files you want to merge.
Step 2: Modify the
$aFiles array to include the paths to the files you want to merge. These should be relative to the server document root.
$aFiles = array(
Step 3: Set the location the script should write the archive files to. When first run it will automatically create the folder you specify if it doesn't already exist. For this to work you'll need to make sure that the parent directory, in this case "js", is owned (or is writable) by the user your web server runs as.
When included via
require the script returns the latest version number rather than the source. When rendered it will look like this:
I've used a
.htaccess file containing the following mod_rewrite rules to map this filename to the script.
RewriteRule js/site_([0-9]+).js js/combine.php?version=$1 [L]
If your host doesn't support
.htaccess files you can rewrite your code to:
That's it for the set up. When you make changes to your source files the script will now take care of updating both the code served and the corresponding filename in the HTML source.
CaveatsIf you subsequently add files to the script which have older last-modified dates than those already included they won't trigger a new version. I could have added code to support this but it would have significantly increased the complexity of the script. To trigger a new version simply touch or re-save one of the files.
Thanks to Mike Davies for suggesting the archive functionality and for generally acting as a great sounding board.
The following links were really helpful whilst putting together this script.
- HTTP/1.1: Header Field Definitions
- Ned Martin - Caching
- Supporting Conditional GET in PHP
- HTTP Conditional Get for RSS Hackers
- Keeping your Dynamic Pages Dynamic
- Caching Tutorial
- Cacheability Engine
The script is still very much work in progress. I'd really like to hear what you think of it - suggestions for improvements, problems etc.