Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enforce a timestamp on all CSS/JS files attached in a Joomla template
<?php
defined('_JEXEC') or die;
/* --- Add this at the very top of index.php (but right below the "defined..." line) to control the timestamp --- */
// Set a timestamp for CSS/JS files
if (isset($_GET['nocache'])) {
// Timestamp for testing
// When ?nocache is appended in a URL, enforce a timestamp of up to the current second
// so the CSS and JS files are re-fetched by the browser, even behind a CDN
define('TIMESTAMP', date('Ymd_His'));
} else {
// Timestamp for production
define('TIMESTAMP', '20181104_2300');
}
/* **************************************** */
/* index.php Joomla template code goes here */
/* **************************************** */
/* --- Add the following at the very bottom of index.php to update all CSS/JS files with a timestamp --- */
// API
$document = JFactory::getDocument();
// Add a timestamp to CSS files
$cssFiles = array();
foreach ($document->_styleSheets as $path => $attributes) {
if(strpos($path, '?') === false){
$path = $path.'?t='.TIMESTAMP;
} else {
$path = $path.'&amp;t='.TIMESTAMP;
}
$cssFiles[$path] = $attributes;
}
unset($document->_styleSheets);
$document->_styleSheets = $cssFiles;
// Add a timestamp to JS files
$jsFiles = array();
foreach ($document->_scripts as $path => $attributes) {
if(strpos($path, '?') === false){
$path = $path.'?t='.TIMESTAMP;
} else {
$path = $path.'&amp;t='.TIMESTAMP;
}
$jsFiles[$path] = $attributes;
}
unset($document->_scripts);
$document->_scripts = $jsFiles;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.