Client reloads CSS and JS files automatically everytime the files are
modified
I came across this elegant solution to guarantee that the client browser
always loads the CSS and JS files from the cache, except when the files
are updated.
.htaccess
RewriteEngine on
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
PHP
function auto_version($file)
{
if(strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] .
$file))
return $file;
$mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
return preg_replace('{\\.([^./]+)$}', ".$mtime.\$1", $file);
}
HTML
<link rel="stylesheet" type="text/css" href="echo
auto_version('/css/screen.css');" />
It works, and it works pretty good... but i'm having doubts regarding the
constant calling of the file's modification time with the filemtime($file)
function. If I were to have 1 million visits per day, that means 1 million
of -apparently- unnecessary filemtime calls.
What do you think? Any other alternatives for this issue?
No comments:
Post a Comment