Skip to content

Instantly share code, notes, and snippets.

@jsolid
Last active December 14, 2015 23:29
Show Gist options
  • Save jsolid/5166454 to your computer and use it in GitHub Desktop.
Save jsolid/5166454 to your computer and use it in GitHub Desktop.
How to cache/refresh static resources (server and client side)
// Similar functionalities, same name as its PHP counterpart, located in Nest_url_helper.php
// @param url String: relative path of the source
function site_ver_url(url) {
if(baseURL === undefined || baseURL == '') {
return;
}
if(codeRevision === undefined) {
codeRevision = 100;
}
// retrieve filename
var filename = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
// retrieve file extension
var fileExt = url.split('.').pop();
// replace filename
return (baseURL + url.replace(filename, filename + '.' + codeRevision));
}
/*
|--------------------------------------------------------------------------
| Code Revision
|--------------------------------------------------------------------------
|
| This version number will be altered when the branch source code is updated.
| Make sure there is so special or space characters in between, else PHP and
| Javascript will have different values
| E.g. "62+ p.67" in PHP will be "62+%20p.67" in JS
|
*/
$config['code_revision'] = '62+p.67-100';
<script type="text/javascript">
// this global variable will be used in common.js
var codeRevision = '<?php echo $this->config->item('code_revision'); ?>';
</script>
<script type="text/javascript" src="<?= site_ver_url('assets/js/views/schedule.js'); ?>"></script>
<?php
/**
* Auto-versioning. Append last modified datetime to filename, .htaccess needs to have the corresponding rule
* Similar function, same name in javascript, common.js
* E.g. "assets/css/style.css" converts to "assets/css/style.1363054031.css"
* @param $url String Relative path
* @return String Modified relative path
*/
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 = '.' . $CI->config->item('code_revision') . '.';
return site_url($path['dirname'] . '/' . str_replace('.', $ver, $path['basename']));
}
}
// Load the chosen service config javascript dynamically, 5 secs time limit (YUI2)
YAHOO.util.Get.script(site_ver_url('assets/js/' + module + '.js'), {
onSuccess: formSuccess,
onFailure: formFailure,
onTimeout: formFailure,
timeout: 5000,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment