Alex Rivera | Logout

Rails time stamps on images in CSS

Asked 2009-09-08T13:56:03.923
10

So Rails time stamping is great. I'm using it to add expires headers to all files that end in the 10 digit timestamp. Most of my images however are referenced in my CSS. Has anyone come across any method that allows for timestamps to be added to CSS referenced images, or some funky re-write rule that achieves this? I'd love for ALL images in my site, both inline and in css to have this timestamp so I can tell the browser to cache them, but refresh any time the file itself changes.

I couldn't find anything on the net regarding this and I can't believe this isn't a more frequently discussed topic.

I don't think my setup will matter because the actual expiring will hopefully happen the same way, based on the 10 digit timestamp, but I'm using apache to serve all static content if that matters

Edit
Report

1 Answer

3

The best solution seems to be to use ERB to generate your stylesheets so you can use the Rails image_ helpers instead of direct image paths. Meaning, get rid of your public/stylesheets/application.css file and create app/views/stylesheets/application.css.erb. You'll also have to create a controller and enable caching, and setup the route.

Here is the full details: http://deaddeadgood.com/2009/9/28/far-future-expires-headers-for-css-images-in-rails

answered 2009-11-19T23:22:06.353

Your Answer