3 ms·
What I have done to accomplish this in the past is some filename shenaniganry with the file's last-modified timestamp: <img src="/path/to/file.[last modifi
by chias 5y ago
What I have done to accomplish this in the past is some filename shenaniganry with the file's last-modified timestamp:
<img src="/path/to/file.[last modified timestamp].png">
PHP code for this kind of thing might look like this:
function auto_version($urlpath) {
// get a filesystem path
$fspath = $_SERVER['DOCUMENT_ROOT'] . $urlpath;
// if file doesn't exist, don't mess with the url
if (!file_exists($fspath)) {
return $urlpath;
}
// insert last modified timestamp
$mtime = filemtime($fspath);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $urlpath);
}
Then in .htaccess or equivalent to strip the timestamp back off before serving the file:
RewriteRule ^(.*)\.[\d]{10}\.(css|js|png)$ $1.$2 [L]
Whenever the file is updated you automatically start serving a new link which points to the same file, and otherwise you can rely on the browser caching. You don't need to mess around with ?params, which also helps semantically because those incorrectly suggest that the resource is dynamic.