Skip to content

Instantly share code, notes, and snippets.

@jsolid
Last active December 14, 2015 21:18
Show Gist options
  • Save jsolid/5149655 to your computer and use it in GitHub Desktop.
Save jsolid/5149655 to your computer and use it in GitHub Desktop.
How to cache/refresh static resources (server side)
# Default cache values
ExpiresActive ON
ExpiresByType text/html "access plus 1 minute"
ExpiresByType text/css "access plus 1 day"
ExpiresByType text/javascript "access plus 7 days"
ExpiresByType application/x-javascript "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
ExpiresByType application/x-shockwave-flash "access plus 7 days"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/x-icon "access plus 2 months"
# Rules for versioned static files, ignoring the version number
# Example: assets/js/common.13453234.js
RewriteRule ^(.*)(js|css)/(.+)\.(.+)\.(js|css)$ $1/$2/$3.$5 [L]
<!-- Method 01 A & B -->
<script type="text/javascript" src="<?php echo site_url(autoVer('assets/js/views/schedule.js')); ?>"></script>
<!-- Method 02 -->
<script type="text/javascript" src="<?php echo site_ver_url('assets/js/views/common.js'); ?>"></script>
<?php
if ( ! function_exists('autoVer')) {
/**
* Method 01 A:
* Append last modified datetime to filename, .htaccess needs to have the corresponding rule
* E.g. "assets/css/style.css" converts to "assets/css/style.1363054031.css"
* @param $url String Relative path
* @return String Modified relative path
*/
function autoVer($url){
$path = pathinfo($url);
$ver = '.' . filemtime($_SERVER['DOCUMENT_ROOT'] . $url) . '.';
return $path['dirname'].'/'.str_replace('.', $ver, $path['basename']);
}
/**
* Method 01 B:
* If your Codeigniter has branch name, e.g. http://www.example.com/dev/assets/css/style.css
* There'll be an extra step to retrieve the branch name
* @param $url String Relative path
* @return String Modified relative path
*/
function autoVer($url){
$CI =& get_instance();
// Get branch name from base_url config, remove any '/' characters
$parts = parse_url($CI->config->item('base_url'));
$branch = strtok($parts['path'], '//');
// Build new file path with version number attached
$path = pathinfo($url);
$ver = '.' . filemtime($_SERVER['DOCUMENT_ROOT'] . $branch . '/' . $url) . '.';
return $path['dirname'].'/'.str_replace('.', $ver, $path['basename']);
}
}
/**
* Method 02:
* Integrate auto-versioning to site_url() of CodeIgniter, cleaner code
* Supports branch name
*/
if ( ! function_exists('site_ver_url')) {
function site_ver_url($url) {
$CI =& get_instance();
// Get branch name, remove any '/' characters
$parts = parse_url($CI->config->item('base_url'));
$branch = strtok($parts['path'], '//');
// Build new file path with version number attached
$path = pathinfo($url);
$ver = '.' . filemtime($_SERVER['DOCUMENT_ROOT'] . $branch . '/' . $url) . '.';
return site_url($path['dirname'].'/'.str_replace('.', $ver, $path['basename']));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment