Forum: Ruby on Rails Numbers on stylesheet_link_tag and image_tag

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B2896675ee61d866e6a5a19407730602?d=identicon&s=25 toulax@gmail.com (Guest)
on 2007-02-18 18:30
(Received via mailing list)
Can anyone explain to me exactly what are those numbers that rails
appends to stylesheets and images when using stylesheet_link_tag or
image_tag? At first I thought it would be so the browser wouldn't
cache the stylesheets, but that doesn't make much sense for images,
plus it seems that the numbers are unique, maybe based on the session.
Any idea?

Thanks
C23baf394b5ce3ce27d2f74cbbe0add1?d=identicon&s=25 shotguncollective (Guest)
on 2007-02-19 00:10
(Received via mailing list)
The numbers in the url prevent the browser from caching images and
stylesheets. (Since it's a different URL each time the browser assumes
they are different files.)
6edd67c92a1dab5eb23fed79f3c18564?d=identicon&s=25 DHH (Guest)
on 2007-02-19 01:54
(Received via mailing list)
> The numbers in the url prevent the browser from caching images and
> stylesheets. (Since it's a different URL each time the browser assumes
> they are different files.)

Actually, the numbers are there to make expiration easy while still
retaining maximum cachability.

So you setup your web server to cache all js, css, and images for 1
year. Now your users don't have to spend precious sockets trying to
fetch all the assets on every page load. That means faster page views.

But how do you now let the user download new versions of the files you
just told his browser to cache for 1 year? That's where the asset
magic number comes in. It's actually a timestamp of the file.

So what you've done is say to the browser, cache forever the all.js
from 2006-12-12 (all.js?20061212). Now when you update all.js on
January 4th, the file will be linked up as all.js?20070104. To the
browser that's a new file, thus it'll fetch it again (and now save
_that_ version for 1 year).

If you for whatever (crazy) reason do not want your users to cache
assets like this, you can set ENV['RAILS_ASSET_ID'] = "". Then the
magic will be gone.
B2896675ee61d866e6a5a19407730602?d=identicon&s=25 toulax@gmail.com (Guest)
on 2007-02-19 07:04
(Received via mailing list)
>
> So what you've done is say to the browser, cache forever the all.js
> from 2006-12-12 (all.js?20061212). Now when you update all.js on
> January 4th, the file will be linked up as all.js?20070104. To the
> browser that's a new file, thus it'll fetch it again (and now save
> _that_ version for 1 year).
>
> If you for whatever (crazy) reason do not want your users to cache
> assets like this, you can set ENV['RAILS_ASSET_ID'] = "". Then the
> magic will be gone.

Thanks David, that makes a lot of sense. I was afraid the number was
random or fixed, and in both cases it would be no good, but using the
timestamp to refresh the file only when there's a change is really
handy!
This topic is locked and can not be replied to.