Blog2011 ≫ Remove query strings from static resources

From the Google page speed:

Resources with a "?" in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:

* http://beta.holidayextras.co.uk/javascripts/cache/application.js?1297952648

So we're using ruby on rails and it puts that ?1297952648 on the end of the resources to force them to reloaded if we've changed the file since you last saw it. On other sites, including this one and holidayextras.co.uk we're doing it by mangling the name of the file as we output the page the file is used in, and then using an Apache .htaccess rule to make the file name right again... so if I have an image called valid.png, instead of putting it in the page with

<img src=valid.png />

I put this fake number in the file name:

<img src=valid.98979879.png />

valid.98979879.png does not exist, I'm never actually renaming the image. So your browser requests valid.98979879.png which doesn't exist, but I have this rule in the .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\.[0-9]+\.(\w+)$ $1.$2 [C,E=VERSIONED_FILE:1]
</IfModule>

which sends you the right file. And then if I chuck this in too:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/png "access plus 2 years"
</IfModule>

Then I get nice fast page speed skills!

Why doesn't rails have that built in by default?

js: Programming language of the internets, mostly how I earn a wage.

⬅️ :: ➡️

Paul Clarke's weblog - I live in A small town. Wed to Clare + dad to 2, I am a full-stack web developr, and I do js / nodejs, some ruby, other languages etc. I like pubbing, parkrun, restaurants, home-automation and other diy jiggery-pokery, history, family tree stuff, TV, squirrels, pirates, lego, + time travel.