Page 1 of 1
fileTime - un-cache the un-cachable with out a name change
Posted: Thu Sep 24, 2009 3:31 pm
by JeremyBASS
Hello, here is a quick tip. If you're using best practices, you undoubtedly have something like
Code: Select all
Header unset ETag
FileETag None
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
Header set Cache-Control "max-age=7257600"
</FilesMatch>
In your .htaccess file. Now 2 year is a long time if you make a mistake on a js script or want to change your CSS styles.... the other option is to rename you scripts and change that name in you CMSMS templates.
This is what I do to overcome it...? in a UDT named "fileTime"
Code: Select all
$files=$params['File'];
$fileTimeIS=filemtime($files);
echo $files."?".$fileTimeIS;
then I replace the file path in the tag. ie...
becomes
and I never have to go in the admin area just to change a file name across the bored. a 5 min task becomes 0. Great for more then CSS files and JS files, you can use it on images and what not too...
Note this is light weight for a reason... no math, no if's blaa blaa, you know... remember it checks the files each time and you should be using best practices on you CSS and JS construction so you don't have 5 CSS files and 5 JS files... etc...
Cheers
Jeremy
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 05, 2009 3:17 am
by pgwalsh
Okay... What is UDT.
and where do I put:
Code: Select all
$files=$params['File'];
$fileTimeIS=filemtime($files);
echo $files."?".$fileTimeIS;
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 05, 2009 3:18 am
by JeremyBASS
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 05, 2009 4:22 am
by pgwalsh
Now I feel dumb. It was right in front of me.
So how does this work with non static sytlesheets?
If they're kept in the database and retrieve with {stylesheet}, then I'm a bit confused on how to change the expiration date.
YSlow = There are 4 static components without a far-future expiration date.
By the way, I checked and once I turned off CDN I scored a 96.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 05, 2009 11:55 am
by kendo451
Master.css is a file you put in the lib right? It's not automatically constructed from the stylesheets in the db.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 05, 2009 3:00 pm
by JeremyBASS
JeremyBASS wrote:
then I replace the file path in the tag. ie...
becomes
yes.... in this case of this css file it's static.... on I put in the lib folder...
on how to change the "expiration date" or the max-age it's a .htaccess thing..
Code: Select all
Header unset ETag
FileETag None
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css)$">
Header unset Last-Modified
Header set Cache-Control "max-age=7257600"
</FilesMatch>
all these file ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css get catched for a year or so... which is why this littl UDT is so powerfull in breaking the catched item.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 05, 2009 11:39 pm
by pgwalsh
Ok Chieftons...
I finally found a solution that works. Not sure if it's proper, but it works.
Code: Select all
Header unset ETag
FileETag None
# Deny access to config.php
ExpiresActive On
<FilesMatch "\.(ico|css|js|gif|jpg|jpeg|png|flv|pdf|mp3|wav)$">
Header unset Last-Modified
Header set Cache-Control "max-age=7257600"
ExpiresDefault "access plus 3 days"
ExpiresByType text/css "access plus 1 day
# expires in 1 hour
<FilesMatch "\.(pl|php|cgi|spl)$">
Header unset Cache-Control
Header unset Expires
Header unset Last-Modified
FileETag None
Header unset Pragma
</FilesMatch>
For some reason I had to add, ExpiresByType text/css "access plus 1 day, or it wouldn't work with the style sheets.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Wed Nov 18, 2009 12:23 am
by pgwalsh
Well the expire dates isn't working for me with dynamic css. After a couple days they don't load and the page looks funky. Should I just remove css from the cache?
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Wed Nov 18, 2009 3:03 pm
by JeremyBASS
if you meant that the stylesheets that are in the admin area, yes you'd want to make them a static style sheet. Otherwise remove them from the caching... but by no means are they dynamic

that in it's self is the best resaon to make them static IMHO... Cheers
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 19, 2009 12:25 am
by pgwalsh
So I implemented your solution and it's great. Only problem I have is yslow says their not compressed and gives me an F. Any way around it? Not a big deal, but I honestly feel the site loads faster.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 19, 2009 6:08 pm
by pgwalsh
Well I've tried various methods, but cannot gzip the files in lib/css.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 19, 2009 7:42 pm
by JeremyBASS
well if you can't turn it on with the server (something to ask you host)... you can try a back-up .htaccess way...
Code: Select all
# CMSMS Safari gzip fix
AddEncoding gzip .gz
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari|!playstation\ 3 [NC]
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
what this does is if you take you file.. the css/js file it'll look for a version that you gziped and put in the folder with them... also it accounts for Safari and PS3 which don't like gzip...
I'd talk to your host and have them turn on the mod_deflate or mod gzip or what have you... that is the best solution...
Cheers
Jeremy
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 19, 2009 7:57 pm
by pgwalsh
JeremyBASS wrote:
well if you can't turn it on with the server (something to ask you host)... you can try a back-up .htaccess way...
Code: Select all
# CMSMS Safari gzip fix
AddEncoding gzip .gz
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari|!playstation\ 3 [NC]
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
what this does is if you take you file.. the css/js file it'll look for a version that you gziped and put in the folder with them... also it accounts for Safari and PS3 which don't like gzip...
I'd talk to your host and have them turn on the mod_deflate or mod gzip or what have you... that is the best solution...
Cheers
Jeremy
PHP Info shows zlib enabled and in the htaccess file, the gzip/deflate variables.
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Thu Nov 19, 2009 9:15 pm
by JeremyBASS
what it the site?
Re: fileTime - un-cache the un-cachable with out a name change
Posted: Sun Nov 22, 2009 5:27 am
by JeremyBASS
Now in a plug-in!!!
So I'v taken this and added it to my
modslack plug-in to make it easier and cut out the db call...
Exmaple Usage: