Skip to content

Instantly share code, notes, and snippets.

@willmorgan
Last active August 29, 2015 14:12
Show Gist options
  • Save willmorgan/f9b6f0125476ea3a9b04 to your computer and use it in GitHub Desktop.
Save willmorgan/f9b6f0125476ea3a9b04 to your computer and use it in GitHub Desktop.
git-hash.php
<!-- Step 0: Ensure your server configuration is correct (see sample-apache.conf) -->
<!-- Step 1: Include the hash script (see git-hash.php) -->
<?php $hash = require_once 'git-hash.php'; ?>
<html>
<!-- Step 2: Prepend the hash to any combined assets -->
<link rel="stylesheet" href="assets/_combined-<?=$hash?>.css" />
<script src="assets/_combined-<?=$hash?>.js" />
<?php
/**
* Get the first 8 characters of the git sha1 hash
* Useful for quick and dirty cache busting for assets
*/
$version = exec('git rev-parse HEAD');
$hash = substr($version, 0, 8);
return $hash;
# Redirects all assets/_combined-<HASH>.css or JS files to _combined.css or JS
# Example:
# assets/_combined-ad4efc12.css => assets/_combined.css
# assets/_combined-ad4efc12.js => assets/_combined.js
# Customise to your needs - it is likely your directory structure will be different.
# Note: mod_rewrite MUST be enabled
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^assets/_combined-[\@a-z0-9\.]+.(js|css)$ assets/_combined.$1 [L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment